memory address access: LEA OFFSET jbwyatt.com

Variables have values. A pointer is a variable whose value is an address.


.. Operands Used in Instructions

Three basic types of DIRECT operands:
                     
 1. Immediate: a constant integer (8, 16, or 32 bits)
    =========
         mov sum, 55   ; immed to memory
         mov al, 34h   ; immed to reg

 2. Register: the name of a register
    ========
         mov al, bl    ; reg to reg
         mov al, 3*4   ; immed to reg
         mov al, sum   ; mem to reg

 3. Memory (RAM): reference to a location in memory
    ============         
         mov sum, 55     ; immed to memory
         mov al, sum     ; mem to reg (no mem to mem - must go to reg first)        
         mov ax, Val + 7 ; Memory with displacement
                         ; move 2 bytes that are 7 bytes beyond Val
        
memory address is encoded within instruction (direct) OR register holds address of a memory location (indirect)
ONLY ONE memory operand permitted can NOT: mov x, y

There are also INDIRECT operands

   LEA register, memory
   ===
       aMessage DB "Hello World!",0 ; data def
       ...
       lea SI, aMessage              ; address of message into SI reg
       
       The purpose of LEA is to load a register with a memory address.  (POINTERS!)
       
   OFFSET
   ======
      mov si, OFFSET aMessage
      
      an ASSEMBLER DIRECTIVE - does the same as LEA
 

.. EMU memory: variables & addresses

EMU Memory : variables - arrays - constants - addresses


 
        count DB 100
        wVal  DW 2
        size  DB 1
        ----------------------------
        mov bl, count  ; ram to reg
        mov ax, wVal   ; ram to reg
        mov count, al  ; reg to ram
        mov bx, ax     ; reg to reg
        mov ax, bx     ; reg to reg
        mov dl, ah     ; reg to reg
        mov al, 77     ; immed to reg        
       
         ;--------- ERRORS ----why??--------
         mov al, wVal     
         mov ax, count
         mov count, size
         mov ax, bl
         mov al, 1000
         mov 7, al

.. LEA instruction and OFFSET directive

   Load Effective Address

      lea SI, message   ; creates a POINTER, SI, to message
      message DB "Hello World", 0 

1. LOAD FOLLOWING CODE INTO EMU 2. EMULATE 3. view / variable 4. Examine "x" 5. Change # elements
include emu8086.inc org 100h jmp CODE ; ARRAY starting at address, x x db 10,9,8,7,6,5,4,3,2,1 count = $ - x CODE: ; init for later printing mov ax, 0 ; ADD all the elements of the array mov cx, count ; loop counter mov al, 0 ; init accumulator sum lea si, x ; set up pointer L1: add al, [si] ; add what si points to, to al call PRINT_NUM PRINT add si, 1 ; bump pointer to next element loop L1 call PRINT_NUM ret DEFINE_PRINT_NUM_UNS DEFINE_PRINT_NUM end
SAME PROGRAM WITH DIFFERENT ADDRESSING see emu tutor on memory access
include emu8086.inc org 100h jmp CODE ; ARRAY starting at address, x x db 10,9,8,7,6,5,4,3,2,1 count = $ - x CODE: ; init for later printing mov ax, 0 ; init INDEX mov bx, 0 ; ADD all the elements of the array mov cx, count ; loop counter mov al, 0 ; init accumulator sum ;lea si, x ; set up pointer mov si, OFFSET x ; set up pointer L1: add al, [si + bx] ; add what si [offset by BX] points to, to al call PRINT_NUM PRINT add bx, 1 ; bump pointer to next element loop L1 call PRINT_NUM ret DEFINE_PRINT_NUM_UNS DEFINE_PRINT_NUM end

.. PTR directive


PTR can be used to combine elements of a smaller data type and move them into a larger operand. myBytes DB 12h,34h,56h,78h -------------------------- mov ax, WORD PTR[myBytes] ;AX = 3412h mov ax, WORD PTR[myBytes+1] ;AX = 5634h mov ax, WORD PTR[myBytes+2] ;AX = 7856h
varB DB 65h, 31h, 02h, 05h varW DW 6543h, 1202h varD DD 12345678h ----------------------------------------- mov ax, WORD PTR [varB+2] ; a. 0502h mov bl, BYTE PTR varD ; b. 78h mov bl, BYTE PTR [varW+2] ; c. 02h mov ax, WORD PTR [varD+2] ; d. 1234h
add WORD PTR[varB+1], 7

.. Direct offset addressing

Can add a displacement to a memory operand to access 
a memory location that is unlabeled.

.data
   arrB byte   10h,   20h ; 10h has a label, but 20h does NOT
   arrW word 1234h, 5678h

arrB+0 refers to location at the beginning of arrB
arrB+1 refers to location one byte beyond the beginning of arrB
arrW+2 refers to location two bytes beyond the beginning of arrW
   mov al,arrB   ; AL =   10h
   mov al,arrB+1 ; AL =   20h (mem with displcmnt)
   mov ax,arrW+2 ; AX = 5678h
   mov ax,arrW+1 ; AX = 7812h (little endian!!)

   
.data arrayW WORD 1000h,2000h,3000h ; 6 bytes .code mov ax, [arrayW + 2] ; AX = 2000h mov ax, [arrayW + 4] ; AX = 3000h ----- What would the value be of AL? AH?