Experiment-5: Addressing Modes, CALL, RET, XLAT, Stack, Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Experiment-5

Addressing modes, CALL , RET , XLAT , Stack , Arrays


Array
ORG 1600H
MYARRAY DB 1,2,3,-3,-2,-1,?

1600 1 MYARRAY
1601 2 MYARRAY+1
1602 3 MYARRAY+2
1603 -3 MYARRAY+3
1604 -2 MYARRAY+4
1605 -1 MYARRAY+5
1606 x MYARRAY+6
Array

ORG 1600H
MYWARRAY DW 1,2,0ABCDH,?

DS:1600 1600 01 MYWARRAY


1601 00 MYARRAY+1
1602 02 MYARRAY+2
1603 00 MYARRAY+3
1604 CD MYARRAY+4
1605 AB MYARRAY+5
1606 X MYARRAY+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,
MYARRAY + (k-1)*S
Where S is the size of each element (1 for
byte, 2 for word)
DUP
MYARRAY DB 10 DUP (0)
0,0,0,0,0,0,0,0,0,0

MYARRAY DB 1,2,3, 7 DUP(4)


1,2,3,4,4,4,4,4,4,4

MYARRAY DB 3, 2 DUP (5,6, 2 DUP(4),5),8


3, 5,6,4,4,5,5,6,4,4,5,8
Addressing Modes
• The way an operand is specified is known as
its addressing mode.
• Register Mode- when operand is a register
• Immediate Mode- when operand is a constant
• Direct Mode-when operand is a variable
• MOV AX, BX
• SUB AX, 12
• MOV DATA1,AX
Register Indirect Mode
• The offset address of a memory content is
contained in a register. The register acts as
a pointer to that memory.
• Format : [register]
• Allowed Registers: BX,SI,DI,BP
• For BX,SI & DI, DS contains the segment
address and for BP, SS is the segment.
Register Indirect Mode
MOV BX, 1000H
MOV DX,BX
MOV AX, [BX] DS: 1000H 1000H 5E H
HLT 1001H 12 H
1002H AB
1003H 34
;DX=1000H
1004H 22
;AX=125E H
LEA
• LEA=Load Effective Address
• Loads the offset of a variable in a register

LEA SI, MYWARRAY


ORG 1000H
MYWARRAY DW 1,2,0ABCDH

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

[Register + Displacement] MOV AX, [BX+MYWARRAY]


[Displacement + Register] MOV AX,[MYWARRAY+BX]
[Register] +Displacement MOV AX,[BX]+MYWARRAY
Displacement+ [Register] MOV AX, MYWARRAY+[BX]
Displacement[Register] MOV AX, MYWARRAY[BX]

3/25/2021
PTR
MOV BX,10H; bx=0010h
MOV [BX],1 ; ????

-creates ambiguity
-PTR assembler directive is used to solve it

MOV BYTE PTR [BX],1; [1000]=1


MOV WORD PTR [BX], 1; [1000]=1;[1001]=0

3/25/2021
The PTR Operator
Example: Using PTR to override a type
N1 DB 01AH
N2 DB 52H

MOV AX, N1 ;Illegal

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

Column Major Format


MY2DARRAY DB 1,4,7
DB 2,5,8
DB 3,6,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,

Location of A(i , j)=A+{(i-1)N + (j-1)} S

When declared in Column Major Format,

Location of A(i , j)=A+{(j-1) + (i-1)M} S

3/25/2021
Based Indexed Addressing Mode

• Array_Name [Base Register] [Index Register]


MOV AX, W [BX][SI]
• [Array_Name +Base Register + Index Register+ Constant]
MOV AX, [W+BS+SI+3]
MOV AX,[W+BX+SI]
• Array_Name [Base Register+Index Register+Constant]
MOV AX, W[BX+SI+3]
• Constant [Array_Name +Base Register+Index Register]
MOV AX, 3[W+BX+SI]
❑ The order of terms within bracket is arbitrary

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

MOV AL, 0CH; number to convert

LEA BX, Table; BX has table offset

XLAT; AL has ‘C’

➢ Replaces AL by [Table + 12]

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

0010H CALL MY_PROC


IP 0012H next instruction
00FAH

MY_PROC PROC 00FCH


0200H first instruction
00FEH

last instruction 0100H SP


0302H RET

Before CALL STACK

EEE 416 - Experiment 4 23


Procedures
Code segment
offset
address offset
MAIN PROC Stack segment
address

0010H CALL MY_PROC


0012H next instruction
00FAH

MY_PROC PROC 00FCH


IP 0200H first instruction
00FEH 0012 SP

last instruction 0100H


0302H RET

After CALL STACK

EEE 416 - Experiment 4 24


Procedures
Code segment
offset
address offset
MAIN PROC Stack segment
address

0010H CALL MY_PROC


0012H next instruction
00FAH

MY_PROC PROC 00FCH


0200H first instruction
00FEH 0012 SP

last instruction 0100H


IP 0302H RET

Before RET STACK

EEE 416 - Experiment 4 25


Procedures
Code segment
offset
address offset
MAIN PROC Stack segment
address

0010H CALL MY_PROC


IP 0012H next instruction
00FAH

MY_PROC PROC 00FCH


0200H first instruction
00FEH 0012

last instruction 0100H SP


0302H RET

After RET STACK

EEE 416 - Experiment 4 26


Thank You

3/25/2021

You might also like