[go: up one dir, main page]

0% found this document useful (0 votes)
9 views29 pages

Lec6 - Arrays and Addressing Modes

assembly

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

Lec6 - Arrays and Addressing Modes

assembly

Uploaded by

iamnah97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

ARRAYS AND ADDRESSING

MODES
Arrays
Arrays
▶ An ordered list of elements

▶ Examples:

MSG DB ‘abcde’

W DW 10, 20, 30, 40, 50, 60

GAMMA DW 100 DUP (0) ;sets an array of 100 elements initialized to 0

DELTA DB 20 DUP (?) ; creates an array with 20 uninitialized bytes


▶ Nested DUP:

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

Which is equivalent to:

LINE DB 5, 4, 2, 0, 0, 0, 1 , 2, 0, 0, 0, 1 , 2, 0, 0, 0, 1
Location of Array Elements
▶ 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 Location
▶ Position of 1elements Ain array A:
2 A2 = 1 * S
. .
. .
N An = (N-1) * S
Example of Addressing
▶ Exchange the 10th and 25th elements in a word
array W

▶ Solution:
MOV AX, [W + 18] ; 10 th element = 9 * 2
XCHG [W + 48], AX ; 25 th element = 24 * 2
MOV [W + 18], AX
Addressing Modes
Default Segment and Offset Registers
▶ CS: IP
▶ SS: SP or BP
▶ DS: BX, DI, SI
Addressing Modes
▶ The way an operand is specified is known as its
addressing mode.
▶ Modes
1. Register Mode (Operand is register)
2. Immediate (Operand is constant)
3. Direct (Operand is variable)
4. Register Indirect
5. Based
6. Indexed
7. Based Indexed (used with 2D array)
Register Indirect Mode
▶ 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
▶ Remember the difference:
MOV AX, SI
MOV AX, [SI]
Example
- Write some code to sum in AX the elements of the 10-element array W
defined by W DW 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

XOR AX,AX ;AX holds sum


LEA SI,W ;SI points to array w
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
Based and Indexed Modes
▶ A register is added to a displacement to generate an effective
address.
▶ Register may be: SI, DI, BX, or BP.
▶ Displacement can be a number or a label
▶ Based: If BX or BP used
▶ Indexed: IF SI or DI used
▶ Can be written as:
displacement [register]
displacement + [register]
[register] + displacement
[displacement + register]
[register + displacement ]
Example
- Write some code to sum in AX the elements of the 10-element array W
defined by W DW 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

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
Based Index (for 2D arrays)
- A two-dimensional array is an array of arrays ; that is, a one-dimensional
array whose elements are one-dimensional Arrays
- Because memory is one-dimensional, the elements of a two-dimensional array must be stored
sequentially

- Declaration:
Locating an Element
Q. Suppose we an A = M x N array, where each element is of size
S(=1for Byte or 2 for Word), how much is element A(i,j) from the start

ANS:

1. Locate Memory location of ith row first


2. Then add the offset to get to jth element in that (ith) row.

- i th row will be located at A + (i -1) x N x S from start of array


- jth coloumn is located at (j -l) x S from the start of that row

=> Combining we get A(i,j) is located at A+((i - 1) x N + (j -I)) x S from the start of the array.
Example
Addressing
Here the offset address of an element is composed of
1. the contents of a base register (BX or BP)
2. the contents of an index register (SI or DI)
3. optionally, a variable's offset address
4. optionally, a constant (positive or negative)

● Various ways of writing

1. variable[base_register] [index_register]
2. [baste_register + index_register + variable + constant]
3. variable[base _ register + index_register + constant]
4. constant[base_register + index_register + variable]
How to use
● This method is exceptionally good for use with 2D arrays
● Show by example on the next slide
BX

14

You might also like