assembly: pointers, arrays & nested loops jbwyatt.com

.. A POINTER is simply a value that is an address

include emu8086.inc
   org 100h

   ; move the value stored at the address associated with the label, 'x'
      mov AX, x       ;AX holds the sum
      call PRINT_NUM
      PRINTN   
     
   ; mov the address associated with the label, 'x'
 
     ;; method 1
      lea AX, x      
      call PRINT_NUM
      PRINTN
      
      ;; method 2
      mov AX, offset x         
      call PRINT_NUM
      PRINTN
   
   ret
   
   x DW 5
      
   DEFINE_PRINT_NUM_UNS
   DEFINE_PRINT_NUM
   END

=================================================================
[LINE]     LOC: MACHINE CODE                          SOURCE
=================================================================
 
[   1]        :                                       include emu8086.inc
[   2]        :                                       org 100h
[   3]        :                                       
[   4]        :                                       ; move the value stored at the address associated with the label, 'x'
[   5]    0100: A1 61 01                              mov AX, x       ;AX holds the sum
[   6]    0103: E8 B1 00                              call PRINT_NUM
[   7]    0106: 50 56 EB 03 0D 0A 00 BE 0A 01 2E 8A   PRINTN
                04 3C 00 74 07 46 B4 0E CD 10 EB F2 
                5E 58                               
[   8]        :                                       
[   9]        :                                       ; mov the address associated with the label, 'x'
[  10]        :                                       
[  11]        :                                       ;; method 1
[  12]    0120: B8 61 01 (161 hex = 353 dec)          lea AX, x
[  13]    0123: E8 91 00                              call PRINT_NUM
[  14]    0126: 50 56 EB 03 0D 0A 00 BE 2A 01 2E 8A   PRINTN
                04 3C 00 74 07 46 B4 0E CD 10 EB F2 
                5E 58                               
[  15]        :                                       
[  16]        :                                       ;; method 2
[  17]    0140: B8 61 01  (161 hex = 353 dec)         mov AX, offset x
[  18]    0143: E8 71 00                              call PRINT_NUM
[  19]    0146: 50 56 EB 03 0D 0A 00 BE 4A 01 2E 8A   PRINTN
                04 3C 00 74 07 46 B4 0E CD 10 EB F2 
                5E 58                               
[  20]        :                                       
[  21]    0160: C3                                    ret
[  22]        :                                       
[  23]    0161: 05 00  (161 hex = 353 dec)            x DW 5
[  24]        :                                       
[  25]    0163: EB 50 50 53 51 52 B9 01 00 BB 10 27   DEFINE_PRINT_NUM_UNS
                3D 00 00 74 32 83 FB 00 74 35 83 F9 
                00 74 04 3B C3 72 14 B9 00 00 BA 00 
                00 F7 F3 04 30 50 8A C0 B4 0E CD 10 
                58 8B C2 50 BA 00 00 8B C3 2E F7 36 
                B3 01 8B D8 58 EB CE 50 B0 30 B4 0E 
                CD 10 58 5A 59 5B 58 C3 0A 00       
[  26]    01B5: EB 26 52 50 3D 00 00 75 0A 50 B0 30   DEFINE_PRINT_NUM
                B4 0E CD 10 58 EB 12 3D 00 00 79 0A 
                F7 D8 50 B0 2D B4 0E CD 10 58 E8 8B 
                FF 58 5A C3                         
[  27]        :                                       END
==============================================================  

.. An ARRAY is just a list

    include emu8086.inc
       org 100h
      
       mov  AX, array  
       PRINT "The number in the array is "
       call PRINT_NUM
       PRINTN  
      
       mov  AX, array + 2
       PRINT "The number in the array is "
       call PRINT_NUM
       PRINTN  
    
       mov  AX, array + 4
       PRINT "The number in the array is "
       call PRINT_NUM
       PRINTN  
       
       mov  AX, array + 6
       PRINT "The number in the array is "
       call PRINT_NUM
       PRINTN     
       
       ret
       
       array DW 12,26,43,13
          
       DEFINE_PRINT_NUM_UNS
       DEFINE_PRINT_NUM
       end  
    
    

    WHAT IF YOU DO THIS ?? include emu8086.inc org 100h mov AX, array PRINT "The number in the array is " call PRINT_NUM PRINTN mov AX, array + 1 PRINT "The number in the array is " call PRINT_NUM PRINTN mov AX, array + 2 PRINT "The number in the array is " call PRINT_NUM PRINTN mov AX, array + 3 PRINT "The number in the array is " call PRINT_NUM PRINTN ret array DW 12,26,43,13 DEFINE_PRINT_NUM_UNS DEFINE_PRINT_NUM end

    WHAT IF YOU DO THIS ?? include emu8086.inc org 100h ; what if you just want to move ONE BYTE of the data mov AL, array ; NO! NO COMPILE ; must do this mov AL, BYTE PTR array ; OK ret array DW 12,26,43,13 end

.. Summing and Nested Loops: POINTERS & ARRAYS


SUMMING ARRAY ELEMENTS
======================
include emu8086.inc
   org 100h

      mov AX, 0       ;AX holds the sum
      mov CX, count   ; holds counter for loop    
      
      ; EITHER of the following will make ESI point to Arr  
      lea SI, array   ; OR mov SI, offset array   

   L1:
      add AX, [SI]    ; de-reference the pointer (why not WORD PTR[si]?)
      PRINT "The current sum is "
      call PRINT_NUM
      PRINTN
      add SI, 2       ; go to the next WORD
   loop L1
   ret
      array DW 12,26,43,13,97,16,73,41 ; 321D = 141H
      count = ($ - array)/2 ; number of WORD elements   
      
   DEFINE_PRINT_NUM_UNS
   DEFINE_PRINT_NUM
   end

NESTED LOOP =========== To code a loop within a loop, you must save the outer loop's CX value. Example: outer loop executes 100 times, and the inner loop 20 times. count DW ? CODE: mov CX, 100 ; set outer loop count L1: outer loop code... outer loop code... mov count, CX ; save outer loop count mov CX, 20 ; set inner loop count L2: inner loop code... inner loop code... loop L2 ; repeat the inner loop mov CX, count ; restore outer loop count loop L1 ; repeat the outer loop