LEC6
LEC6
Assembly language/Architecture
Addressing Modes
Immediate
Addressing
Instruction
OPERAND
[org 0x0100]
mov ax, 5
mov bx, 10
add ax, bx
mov bx, 15
add ax, bx
mov ax, 0x4c00
int 0x21
In instruction“mov ax, 5” , MOV was the
opcode; AX was the destination operand, while
5 was the source operand. The value of 5 in
this case was stored as part of the instruction
encoding. In the opcode B80500, B8 was the
opcode and 0500 was the operand stored
immediately afterwards. Such an operand is
called an immediate operand. It is one of the
many types of operands available.
DIRECT ADDRESSING
Now we will rewrite our first program such
that the numbers 5, 10, and 15 are stored as
memory variables instead of constants and
we access them from there.
A program to add three numbers using memory
variables
[org 0x0100]
mov ax, [num1] ; load first number in ax
mov bx, [num2] ; load second number in bx
add ax, bx ; accumulate sum in ax
mov bx, [num3] ; load third number in bx
add ax, bx ; accumulate sum in ax
mov [num4], ax ; store sum in num4
mov ax, 0x4c00 ; terminate program
int 0x21
num1: dw 5
num2: dw 10
num3: dw 15
num4: dw 0
Explanation
Originate our program at 0100. The first executable instruction should be
placed at this offset.
The source operand is changed from constant 5 to [num1].
The bracket is signaling that the operand is placed in memory at address
num1. The value 5 will be loaded in ax even though we did not
specified it in our program code, rather the value will be picked from
memory. The instruction should be read as “read the contents of
memory location num1 in the ax register.” The label num1 is a
symbol for us but an address for the processor while the conversion
is done by the assembler.
The label num1 is defined as a word and the assembler is requested
to place 5 in that memory location. The colon signals that num1 is a
label and not an instruction.
Another way of writing program(using a
single label)
[org 0x0100]
mov ax, [num1] ; load first number in ax
mov bx, [num1+2] ; load second number in bx
add ax, bx ; accumulate sum in ax
mov bx, [num1+4] ; load third number in bx
add ax, bx ; accumulate sum in ax
mov [num1+6], ax ; store sum at num1+6
mov ax, 0x4c00 ; terminate program
int 0x21
num1: dw 5
dw 10
dw 15
dw 0
Explanation
The second number is read from num1+2.
Similarly the third number is read from
num1+4 and the result is accessed at
num1+6.
The labels num2, num3, and num4 are
removed and the data there will be accessed
with reference to num1.
Accessed using a single label
Another way to declare the above data and
produce exactly same results is shown in the
following example.
A program to add three numbers accessed using a single
label(Direct Addressing)
[org 0x0100]
mov ax, [num1] ; load first number in ax
mov bx, [num1+2] ; load second number in bx
add ax, bx ; accumulate sum in ax
mov bx, [num1+4] ; load third number in bx
add ax, bx ; accumulate sum in ax
mov [num1+6], ax ; store sum at num1+6
mov ax, 0x4c00 ; terminate program
int 0x21
num1: dw 5, 10, 15, 0
Explanation
As we do not need to place labels on individual variables we can save
space and declare all data on a single line separated by commas.
This declaration will declare four words in consecutive memory
locations while the address of first one is num1.
The method used to access memory in the above examples is called direct
addressing. In direct addressing the memory address is fixed and is given in
the instruction. The actual data used is placed in memory and now that data
can be used as the destination operand as well. Also the source and
destination operands must have the same size. For example a word defined
memory is read in a word sized register. A last observation is that the data
0500 in memory was corrected to 0005 when read in a register. So registers
contain data in proper order as a word.
Another variation using direct addressing
Another variation using direct addressing shows that we can directly add a
memory variable and a register instead of adding a register into another that we
were doing till now.
; a program to add three numbers directly in memory
[org 0x0100]
mov ax, [num1] ; load first number in ax
mov [num1+6], ax ; store first number in result
mov ax, [num1+2] ; load second number in ax
add [num1+6], ax ; add second number to result
mov ax, [num1+4] ; load third number in ax
add [num1+6], ax ; add third number to result
mov ax, 0x4c00 ; terminate program
int 0x21
num1: dw 5, 10, 15, 0
Lets practice in lab