Chap 10

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

Array in assembly language

• One‐Dimensional Arrays

• Addressing Modes

• Based Indexed Addressing Mode

1
One‐Dimensional Array A

• One‐Dimensional Array is an ordered list of elements


• All elements are same type
• DB is used to declare byte sized arrays
• DW is used to declare word sized arrays

2
One‐Dimensional Array
W DB 10, 20, 30, 40, 50, 60
*Address of the array variable is called base address of array

 If offset address assigned to W is 0200h,

Offset address Symbolic address Contents


0200h W 10
0201h W + 1h 20
0202h W + 2h 30
0203h W + 3h 40
0204h W + 4h 50
0205h W + 5h 60
One‐Dimensional Array
W DW 1000, 40, 29887, 329

*Address of the array variable is called base address of array

 If offset address assigned to W is 0300h,

Offset Address Symbolic Address Contents


0300h W 1000d
0302h W +2h 40d
0304h W +4h 29887d
0306h W +6h 329d

4
The DUP Operator
• The DUP (duplicate) is used to define arrays whose elements
share a common initial value.
• Format:
DUP (value)

• GAMMA DW 100 DUP (0)


• GAMMA DW 100 DUP (?)
; AX = 00C0h, OF=1

• DUP may be nested


• LINE DB 5, 4, 3 DUP (2, 3 DUP (0), 1)
 LINE DB 5,4,2,0,0,0,1,2,0,0,0,1,2,0,0,0,1

5
Location of Array Elements
•The address of an array element can be specified by
adding a constant to the base address
– If A is an array & S is the size of every element in bytes

Position Location S=1 for byte array,


1 A S=2 for word array
2 A+1xS
3 A+2xS
. .
. .
N A + (N‐1) x S

6
Example
Exchange 4th and 10th element of an [word]array:

• W[4] is located at W + 3x2 = W + 6


• W[10] is located at W + 9x2 = W + 18

MOV AX, W + 6
XCHG W+18, AX
MOV W+6, AX

7
Addressing Modes
• The way an operand is specified

• register mode: an operand is a register.


• immediate mode: an operand is a constant.
• direct mode: an operand is a variable.

– MOV AX, 0 (Destination AX is register mode,


Source 0 is immediate mode)

– ADD ALPHA, AX (Destination ALPHA is direct mode,


Source AX is register mode)

8
ADDITIONAL ADDRESSING MODES
Four additional addressing modes to address memory
operands indirectly:

1. Register Indirect Mode


2. Based
3. Indexed
4. Based Indexed

9
Register Indirect Mode
 Offset address of the operand is contained in a register.
 Register acts like a pointer to the memory location
 [register]
 The register is BX, SI, DI, or BP

the operand’s segment num the operand’s segment num


ber is contained in DS ber is contained in SS

1
0
Examples

 Suppose that SI contains 0100h, and the word at 0100h


contains 1234h.

MOV AX, [SI]


; AX = 1234h
The CPU,
1. examines SI and obtains the offset address 100h,
2. uses the address DS:0100h to obtain the value 1234h,
3. moves 1234h to AX.

MOV AX, SI
; AX = 0100h
1
1
Your turn . . .
 Suppose that,
BX contains 1000h Offset 1000h contains 1BACh
SI contains 2000h Offset 2000h contains 20FEh
DI contains 3000h Offset 3000h contains 031Dh
where the above offsets are in the data segment addressed by DS.

 Tell which of the following instructions are legal. If legal, give


the source offset address and the result or number moved.
Source offset Result
 a. MOV BX, [BX] 1000h 1BACh
 b. MOV CX, [SI] 2000h 20FEh
 c. MOV BX, [AX] illegal source register(must be BX/SI/DI)
 d. ADD [SI], [DI] illegal memory‐memory addition
 e. INC [DI] 3000h 031Eh
1
2
Example
 Write some code to sum the elements of the 10‐element array W
in AX.
W DW 10,20,30,40,50,60,70,80,90,100
*The idea is to set a pointer to the base of the array, and let it move
up the array, summing elements as it goes.

XOR AX, AX; AX holds sum


LEA SI, W DIV r/m16 to array W
; SI points
DIV r/m32
MOV CX, 10 ; CX has number of elements
ADDNOS:
ADD AX, [SI] ; sum = sum + element
ADD SI, 2 ; move pointer to ; the next element
LOOP ADDNOS ; loop until done
1
3
Home Assignment

• Write a procedure to reverse an array of N words

Hints: exchange the 1st and Nth words, the 2nd and (N-1)th word
and so on.

1
4
Based and Indexed Addressing Mode
•Operand’s offset address is obtained by adding a number
called displacement to the contents of a register

• Displacement can be of following forms;


– Offset address of a variable A
– A constant (+ve or ‐ve) -2
– Offset address of a variable
plus or minus a constant A+4
*A is a variable

1
5
Based and Indexed Addressing Mode
• [register + displacement]
• [displacement + register]
• [register] + displacement
• displacement + [register]
• displacement[register]
• [register]displacement

*The register is SI, DI, BX, or BP.

the operand’s segment num the operand’s segment num 1


ber is contained in DS ber is contained in SS 6
Based and Indexed Addressing Mode
Suppose W is a word array;
BX contains 4

MOV AX, W[BX]


Will move the element at address W + 4 to AX (the third
element of array)

MOV AX, [W + BX]


MOV AX, [BX + W]
MOV AX, W + [BX]
MOV AX, [BX] + W

1
7
Example
Rework the last example by using based mode.

XOR AX, AX ; AX holds sum


XOR BX, BX ; clear base register
MOV CX, 10 ; CX has number of elements
ADDNOS:
ADD AX, W[BX] ; sum = sum + element
ADD BX, 2 ; index next element
LOOP ADDNOS ; loop until done

1
8
Your turn . . .
 Suppose that ALPHA is declared as
ALPHA DW 0123H, 0456h, 0789h, 0ABCDh
in the segment addressed by DS.
Suppose also that,
BX contains 2, Offset 0002 contains 1084h
SI contains 4, Offset 0004 contains 2BACh
DI contains 1
 Tell which of the following instructions are legal. If legal, give the source
offset address and the result or number moved.
Source offset Number moved
a. MOV AX, [ALPHA+BX] ALPHA+2 0456h
b. MOV BX, [BX+2] 2+2 = 4 2BACh
c. MOV CX, ALPHA[SI] ALPHA+4 0789h
d. MOV AX, –2[SI] –2+4 = 2 1084h
e. MOV BX, [ALPHA+3+DI] ALPHA+4 0789h
f. MOV AX, [BX] 2 Illegal form of source operand
g. ADD BX, [ALPHA+AX] Illegal source register 1
9
PTR OPERATOR
• Operands of instruction must be of the same type

MOV AX, 1 ; legal word instruction


MOV BH, 5 ; legal byte instruction
MOV [BX], 1 ; illegal as cant interpret whether destination
is a byte pointed or a word pointed by BX

• For destination to be byte


MOV BYTE PTR [BX], 1

• For destination to be word


MOV WORD PTR [BX], 1

2
0
Accessing the STACK
• When BP specifies the offset address, SS supplies the segment
number
• BP may be used to access elements of the stack

 Move top three elements of stack to AX, BX, CX without


changing the stack

MOV BP, SP
MOV AX, [BP]
MOV BX, [BP + 2]
MOV CX, [BP + 4]
2
1
Home Assignment

• Write a procedure to sort an array of N words

2
2
Based Indexed Addressing Mode

• Offset address of the operand is the sum of;


– Contents of a base register (BX or BP)
– Contents of an index register (DI or SI)
– Optionally a variable's offset address
– Optionally a constant (+ve or ‐ve)

2
3
Based Indexed Addressing Mode

• The operand may be written in several ways,


-variable [base_register][index_register]
-[base_register + index_register + variable + constant]
-variable [base_register + index_register + constant]
-constant [base_register + index_register + variable]

2
4
Based Indexed Addressing Mode
• W is a word variable
• BX contains 2
• SI contains 4

MOV AX, W[BX][SI] Will move contents of W + 6 to AX


OR
MOV AX, [W + BX + SI]
OR
MOV AX, W[BX + SI]
2
5
Any Query?

You might also like