Microprocessor and Assembly Language CSC-321: Sheeza Zaheer
Microprocessor and Assembly Language CSC-321: Sheeza Zaheer
● References
■ Chapter 10, Ytha Yu and Charles Marut,
“Assembly Language Programming and
Organization of IBM PC
Arrays
4
● An ordered list of elements
● Syntax
Array_name type value1, value2, value3
● Data Definition Directives
■ DB, DW, DD, DQ, DT
myArray dw 1000h,2000h
Data dw 3000h,4000h
name
4 Values
Contd..
5
5
Arrays
6
● Sequence of memory bytes or words
● Example 1:
B_ARRAY DB 10h, 20h, 30h
Symbol Address Contents
6
Example 2
7
● W_ARRAY DW 1000, 40, 29887, 329
*If W_ARRAY is assigned offset address 0300h by assembler
● Nested DUP:
8
Location of Array Elements
9
● Address of an array element = base address + constant
● Suppose A is an array, S denotes no. of bytes in each element (S =
1 for a byte array, S = 2 for a word array).
● Position of elements in array A:
Position Location
1 A
2 A= 1 *S
. .
. .
N A = (N-1) * S
9
Example 10.1
10
● Solution:
MOV AX, W + 18 ; 10th element = 9 * 2
XCHG W + 48, AX ; 25th element = 24 * 2
MOV W + 18, AX
10
11
ADDRESSING MODES
11
Default Segment and Offset Registers
12
● CS: IP
● SS: SP, BP
● DS: BX, DI, SI
12
Addressing Mode
13
● The way an operand is specified is known as its addressing
mode.
● Modes
● Operand = Register
● Can be 8 or 16 bit register
● Efficient as no memory access required.
● Example
mov ax, bx
mov cl, al
14
Immediate Mode
15
15
Direct Mode
16
● Example
.data
count db 20
wordList dw 1000h,2000h
.code
mov al, count
mov bx, wordList
16
Contd..
17
.data
array db 10,20,30,40
.code
mov al, array
mov bl, array+1
mov cl, array+2
mov dl, array+3
17
Offset Operator
18
.data
aWord dw 1234
.code
mov bx, offset aWord
18
Register Indirect Mode
19
● Register contains the offset of data in memory.
● So, the register becomes a pointer to the memory location.
● Syntax:
[register]
● BX, SI, DI with default segment DS
● BP with default segment SS
0200 0205
A B C D E F G ...........
aString [bx]
22
Example
23
23
Adding 8-bit Integers
24
.data
aList db 10h,20h,30h
sum db 0
.code
mov bx,offset aList
mov al,[bx] ; AL = 10h
inc bx
add al,[bx] ; AL = 30h
inc bx
add al,[bx] ; AL = 60h
mov si,offset sum ; get offset of sum
mov [si],al ; store the sum
25
Displaying a String
26
.data
string db "This is a string."
COUNT = ($–string) ; calculate string length
.code
mov cx,COUNT ; loop counter
mov si,offset string
L1:
mov ah,2 ; DOS function: display char
mov dl,[si] ; get character from array
int 21h ; display it now
inc si ; point to next character
Loop L1 ; decrement CX, repeat until 0
26
Two-Dimensional Array Example
27
27
Based-Index Operands
28
Add the value of a base register to an index
register, producing an effective address of
0157:
BX = 0155, SI = 0002
0150 0155 0157
10 20 30 40 50 60 70 80 90 A0 B0 C0 D0 E0 F0
Example...
28
Base-Index Example
29
.data
ROWSIZE = 5
array db 10h, 20h, 30h, 40h, 50h
db 60h, 70h, 80h, 90h,0A0h
db 0B0h,0C0h,0D0h,0E0h,0F0h
.code
mov bx,offset array ; point to the array at 0150
add bx,ROWSIZE ; choose second row
mov si,2 ; choose third column
mov al,[bx + si] ; get the value at 0157
29
Base-Index with Displacement
30
.data
ROWSIZE = 5
array db 10h, 20h, 30h, 40h, 50h
db 60h, 70h, 80h, 90h,0A0h
db 0B0h,0C0h,0D0h,0E0h,0F0h
.code
mov bx,ROWSIZE ; row 1
mov si,2 ; column 2
mov dl,array[bx + si] ; DL = 80h
30