Register + Offset Addressing

 Direct addressing and indirect addressing using a single register are two basic forms of memory access. Another possibility is to use different combinations of direct and indirect references. In the above example we used BX to access different array elements which were placed consecutively in memory like an array. We can also place in BX only the array index and not the exact address and form the exact address when we are going to access the actual memory. This way the same register can be used for accessing different arrays and also the register can be used for index comparison like the following example does. 

 

Example 2.8

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

; a program to add ten numbers using register + offset addressing

[org 0x0100]

              mov  bx, 0              ; initialize array index to zero

              mov  cx, 10             ; load count of numbers in cx

              mov  ax, 0              ; initialize sum to zero

 

l1:           add  ax, [num1+bx]      ; add number to ax

              add  bx, 2              ; advance bx to next index

              sub  cx, 1              ; numbers to be added reduced

              jnz  l1                 ; if numbers remain add next

 

              mov  [total], ax        ; write back sum in memory

 

              mov  ax, 0x4c00         ; terminate program

              int  0x21

 

num1:         dw   10, 20, 30, 40, 50, 10, 20, 30, 40, 50

total:        dw   0

003

This time BX is initialized to zero instead of array base

007

The format of memory access has changed. The array base is added to BX containing array index at the time of memory access.

008

As the array is of words, BX jumps in steps of two, i.e. 0, 2, 4. Higher level languages do appropriate incrementing themselves and we always use sequential array indexes. However in assembly language we always calculate in bytes and therefore we need to take care of the size of one array element which in this case is two.

Inside the debugger we observe that the memory access instruction is shown as “mov ax, [011F+bx]” and the actual memory accessed is the one whose address is the sum of 011F and the value contained in the BX register. This form of access is of the register indirect family and is called base + offset or index + offset depending on whether BX or BP is used or SI or DI is used.

 

 

 

Previous

 

 

TOC

 

 

Next