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
In this example, we are using the bx
as an index
of an array
.
We use num1
as our reference, then we refer to bx
, increment by 2
and the loop repeats itself .
Segment Association
All the addressing mechanisms in iapx88
returns a number called effective address
.
The physical address which we get after addition of base
and offset
is an example of effective address
.
But this address alone is useless without a defined segment.
Without the segment
, the offset
is meaningless.
There are default associations between segments
and register. Such as:
instruction-pointer is associated with code segment
and cannot be used outside of it.
Similarly, the stack-pointer is associated with stack segment
.
However, when it comes to data, if we use DI
, SI
, BX
then the associated segment is data segment
by default.
If we use BP
then the associated segment will be stack segment
.
To override the associations of one of these register i.e. DI
, SI
, BX
, BP
, we use override prefix
such as mov ax, [cs:bx]
.
Here CS
is associated with BX
.
Address Wraparound
There are 2 types of wraparounds
.
1. Segment wraparound
2. Physical memory wraparound
Segment Wraparound
Example: BX = 9100
and DS = 1500
, when the effective address
is generated (offset = 7000
), we get 9100 + 7000 = 10100
.
Since 1
is a carry and is dropped, we are left with 0100
.
Physical Memory Wraparound
Same thing happens here but we drop the carry after 20-bits
since 20
is the limit of addressing any memory location.
Addressing Modes Summary
The memory access mechanisms can be written in general form as base + index + offset
.
Only addition is valid here, things like base - index
are not allowed.
Also, combination of base + base
or index + index
is also not allowed.
Direct
mov [1234], ax
The value of AX
is moved into address 1234
.
Based Register Indirect
mov [bx], ax
Moves AX
into the address stored by BX
in the data segment
.
Indexed Register Indirect
mov [si], ax
The contents AX
are moved into SI
in the current data segment.
Based Register Indirect + Offset
mov [bx + 300], ax
Here, the contents of AX
are moved into the address we get by adding bx + 300
in the data segment
.
Indexed Register Indirect + Offset
Same case here as well, just the register are index
type. i.e. DI
, SI
.
Base + Index
mov [bx+si], ax
Just like before, contents of AX
are moved into the resultant offset we get from bx + si
.
Base + Index + Offset
Same thing but with the combination of an offset
value as well.
mov [bx+si+300], ax
Exercises
- What is a label and how does the assembler differentiates between code labels and data labels?
- List the seven addressing modes available in the 8088 architecture.
- Differentiate between effective address and physical address.
- What is the effective address generated by the following instructions? Every instruction is independent of others. Initially
BX=0x0100
,num1=0x1001
,[num1]=0x0000
, andSI=0x0100
mov ax, [bx+12]
mov ax, [bx+num1]
mov ax, [num1+bx]
mov ax, [bx+si]
- 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
, andSP=0xFFFF
.bx-si
bx-bp
bx+10
bx-10
bx+sp
bx+di
- Identify the problems in the following instructions and correct them by replacing them with one or two instruction having the same effect.
mov [02], [22]
move [wordvar], 20
mov bx, al
mov ax, [si+di+100]
- What is the function of segment override prefix and what changes it brings to the opcode?
- What are the two types of address wraparound? What physical address is accessed with
[BX+SI]
ifFFFF
is loaded inBX
,SI
, andDS
. - Write instructions to do the following.
- Copy contents of memory location with offset 0025 in the current data segment into AX.
- Copy AX into memory location with offset 0FFF in the current data segment.
- Move contents of memory location with offset 0010 to memory location with offset 002F in the current data segment.
- Write a program to calculate the square of 20 by using a loop that adds 20 to the accumulator 20 times.