Lesson 77888
Lesson 77888
07
1.1. 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 ; a program to add ten numbers using register + offset addressing
002 [org 0x0100]
003 mov bx, 0 ; initialize array index to zero
004 mov cx, 10 ; load count of numbers in cx
005 mov ax, 0 ; initialize sum to zero
006
007 l1: add ax, [num1+bx] ; add number to ax
008 add bx, 2 ; advance bx to next index
009 sub cx, 1 ; numbers to be added reduced
010 jnz l1 ; if numbers remain add next
011
012 mov [total], ax ; write back sum in memory
013
014 mov ax, 0x4c00 ; terminate program
015 int 0x21
016
017 num1: dw 10, 20, 30, 40, 50, 10, 20, 30, 40, 50
018 total: dw 0
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.
Direct
A fixed offset is given in brackets and the memory at that offset is accessed. For
example “mov [1234], ax” stores the contents of the AX registers in two bytes
starting at address 1234 in the current data segment. The instruction “mov [1234],
al” stores the contents of the AL register in the byte at offset 1234.
Base + Index
One base and one index register is used in this addressing mode. The value of the
base register and the index register are added together to get the effective address.
For example “mov [bx+si], ax” moves the word contained in the AX register to offset
attained by adding BX and SI in the current data segment. The instruction “mov
[bp+di], al” moves the byte contained in AL to the offset attained by adding BP and
DI in the current stack segment. Observe that the default segment is based on the
base register and not on the index register. This is why base registers and index
registers are named separately. Other examples are “mov [bx+di], ax” and “mov
[bp+si], ax.” This method can be used to access a two dimensional array such that
one dimension is in a base register and the other is in an index register.
EXERCISES
1. What is a label and how does the assembler differentiates between code
labels and data labels?
2. List the seven addressing modes available in the 8088 architecture.
3. Differentiate between effective address and physical address.
4. What is the effective address generated by the following instructions? Every
instruction is independent of others. Initially BX=0x0100, num1=0x1001,
[num1]=0x0000, and SI=0x0100
a. mov ax, [bx+12]
b. mov ax, [bx+num1]
c. mov ax, [num1+bx]
d. mov ax, [bx+si]
5. What is the effective address generated by the following combinations if
they are valid. If not give reason. Initially BX=0x0100, SI=0x0010,
DI=0x0001, BP=0x0200, and SP=0xFFFF
a. bx-si
b. bx-bp
c. bx+10
d. bx-10
e. bx+sp
f. bx+di
6. Identify the problems in the following instructions and correct them by
replacing them with one or two instruction having the same effect.
a. mov [02], [ 22]
b. mov [wordvar], 20
c. mov bx, al
d. mov ax, [si+di+100]
7. What is the function of segment override prefix and what changes it
brings to the opcode?
8. What are the two types of address wraparound? What physical
address is accessed with [BX+SI] if FFFF is loaded in BX, SI, and DS.
9. Write instructions to do the following.
a. Copy contents of memory location with offset 0025 in the current
data segment into AX.
b. Copy AX into memory location with offset 0FFF in the current data
segment.
c. Move contents of memory location with offset 0010 to memory
location with offset 002F in the current data segment.
10. Write a program to calculate the square of 20 by using a loop that
adds 20 to the accumulator 20 times.