Experiment-5: Addressing Modes, CALL, RET, XLAT, Stack, Arrays
Experiment-5: Addressing Modes, CALL, RET, XLAT, Stack, Arrays
1600 1 W
1601 2 W+1
1602 3 W+2
1603 -3 W+3
1604 -2 W+4
1605 -1 W+5
1606 x W+6
Array
ORG 1600H
W DW 1,2,0ABCDH,?
DS:1600 1600 01 W
1601 00 W+1
1602 02 W+2
1603 00 W+3
1604 CD W+4
1605 AB W+5
1606 X W+6
Array
• Lower byte of the word is stored in the
lower address while the higher byte will
be stored in the next address
• For an array named MYARRAY, address
of the k-th element,
W + (k-1)*S
Where S is the size of each element (1 for
byte, 2 for word)
DUP
W DB 10 DUP (0)
0,0,0,0,0,0,0,0,0,0
W DB 1,2,3, 7 DUP(4)
1,2,3,4,4,4,4,4,4,4
3/25/2021
Based & Index Modes
• The operand’s offset is obtained by adding
a ‘displacement’ with the content of a
register.
• Format:
[Register + Displacement] MOV AX, [BX+2]
[Displacement + Register] MOV AX,[2+BX]
AX=34ABH
[Register] +Displacement MOV AX,[BX]+2
Displacement+ [Register] MOV AX, 2+[BX]
Displacement[Register] MOV AX, 2[BX]
Based & Index Modes
• The addressing mode is based for BX,BP
• The addressing mode is index for SI,DI
• Displacement can be negative:
MOV AX,-2[BX]
• Displacement can be a numerical value
or name of a variable/array
3/25/2021
Based & Index Modes
ORG 1000H
MYWARRAY DW 1,2,0ABCDH
3/25/2021
PTR
MOV BX,10H; bx=0010h
MOV [BX],1 ; ????
-creates ambiguity
-PTR assembler directive is used to solve it
3/25/2021
The PTR Operator
Example: Using PTR to override a type
N1 DB 01AH
N2 DB 52H
Solution
MOV AX, WORD PTR N1 ; AL = N1, AH = N2
3/25/2021
2D Array
Row Major Format
MY2DARRAY DB 1,2,3
DB 4,5,6
DB 7,8,9
MY2DARRAY DB 1,2,3,4,5,6,7,8,9
MY2DARRAY DB 1,4,7,2,5,8,3,6,9
3/25/2021
2D Array
For a M x N array A declared in Row Major Format,
3/25/2021
Based Indexed Addressing Mode
3/25/2021
3/25/2021
XLAT
• No operand instruction used to translate
/code/decode
• AL<= [BX+AL]
• BX will have the address of conversion
table
• The byte to be converted will be in AL
• XLAT adds AL with the address in BX,
goes to the address and copies that ‘byte’
in AL
3/25/2021
The XLAT Instruction
➢ Example:
TABLE DB 030H,031H,032H…….,046H
3/25/2021
Encoding-Decoding
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
X Q P O G H Z B C A D E I J U V F M N K L R S T W Y
J H I K L Q E F M N T U R S D C B V W X O P Y A Z G
3/25/2021
Encoding-Decoding
DECODING:
LEA BX,ENCRYPT MOV AL,[SI] ; Taking a letter from Encoded message
LEA SI,ORIGINAL CMP AL,'$' ; Checking for end of the message
LEA DI,ENCODED JE END
ENCODING: XLAT ; Collecting corresponding decoded letter from
;DECRYPT sequence
MOV AL,[SI] ; Taking a letter from original
MOV [DI],AL
;message
INC SI ; Putting the decoded letter in Decoded
CMP AL,'$' ; Checking for end of the ;message
;message INC DI
JE STEP1 JMP DECODING
XLAT ; Collecting corresponding coded END:
;letter from ENCRYPT sequence MOV [DI],AL
MOV [DI],AL ; Putting the coded letter in HLT
;Encoded message ENCRYPT DB 65 DUP(0),
‘XQPOGHZBCADEIJUVFMNKLRSTWY',37 DUP(0)
INC SI
DECRYPT DB 65 DUP(0),
INC DI ‘JHIKLQEFMNTURSDCBVWXOPYAZG',37 DUP(0)
JMP ENCODING ORIGINAL DB 'ATTACK$'
STEP1: MOV [DI],AL ENCODED DB 12 DUP(0)
LEA BX,DECRYPT DECODED DB 12 DUP(0)
LEA SI,ENCODED
LEA DI,DECODED
3/25/2021
Procedures
Code segment
offset
address offset
MAIN PROC Stack segment
address
3/25/2021