Complex Data Structures

Dr. Tim McGuire
CS 272
Sam Houston State University

Structures

• A structure is a named entity that contains variables, called fields.
• For example:
STRUCT Date day db 1 ;Day field, default value = 1
month db ? ;Month field, no default value
year dw 1999 ;Year field, default = 1999
ENDS Date
• A structure definition is not a variable -- it is a schematic for a variable
• The structure definition does not reserve space in the data segment
Declaring Structure Variables
• A structure variable has the design of the structure
• A structure variable declaration starts with a label, followed by the structure name, and ending with a list of default values in angle brackets
DATASEG birthday  Date <>          ; 1-0-1999
today     Date <16,11>     ; 16-11-1999
ship      Date <15,4,1912> ; 15-4-1912
payday    Date <,11,>      ; 1-11-1999
• Uninitialized default field values are set to zero, unless all fields in the structure are uninitialized
Using Structured Variables
• To use the fields in a structured variable, use the "dot operator"
• For example
mov     [today.day],17     ; change day to 17
mov     ax,[today.year]    ; put year into ax
• All of the normal addressing modes are still available
• In MASM mode, the field names are global, but in TASM mode they are local to the structure
Arrays
• A one-dimensional array is an ordered list of elements, all of the same type.
• To define an array in assembly language
• W     dw     10,20,30,40,50,60
• The address of the array variable is called the base address of the array
• If the offset address of the array is 0200h, the array looks like this in memory:
W[0]     0200h         W            10
W[1]     0202h         W+2h         20
W[2]     0204h         W+4h         30
W[3]     0206h         W+6h         40
Location of Array Elements
• The address of an array element may be computed by adding a constant to the base address
• If A is an array and S denotes the number of bytes in an element, then the address of element A[i] is A + i*S (assuming zero-based arrays; for one-based arrays it would be A + (i-1)*S)
• To exchange W[9] and W[24] in an word array W:
mov   ax,W+18     ; ax has W[9]
xchg  W+48,ax     ; ax has W[24]
mov   W+18,ax     ; complete exchange
Processing Arrays using Register-Indirect Mode
• Sum in ax the 10-element word array W
W     dw     10,20,30,40,50,60,70,80,90,10

xor     ax,ax     ; ax holds sum
lea     si,[W]    ; si points to array W
mov     cx,10     ; cx has number of elements
add     ax,[si]   ; sum = sum + element
add     si,2      ; move pointer to the next element
loop    addnos    ; loop until done

Processing Arrays using Based Addressing Mode
• Sum in ax the 10-element word array W using based mode
W     dw     10,20,30,40,50,60,70,80,90,10

xor     ax,ax     ; ax holds sum
xor     bx,bx     ; clear base register
mov     cx,10     ; cx has number of elements
add     ax,[bx+W] ; sum = sum + element
add     bx,2      ; index next element
loop    addnos    ; loop until done

The LABEL Pseudo-op
• We have seen the use of the WORD and BYTE pseudo-ops to remove type ambiguity
• The LABEL pseudo-op is another way of working with data of various types
• Example:
LABEL     money   WORD
dollars   db      1Ah
cents     db      52h
mov     ax,money     ;al=dollars, ah=cents

has same effect as

mov     al,dollars
mov     ah,cents

Two-Dimensional Arrays
• A 2D array is an array of arrays
• Usually view them as consisting of rows and columns
A[0,0] A[0,1] A[0,2] A[0,3]

A[1,0] A[1,1] A[1,2] A[1,3]

A[2,0] A[2,1] A[2,2] A[2,3]

• Elements may be stored in row-major order or column-major order
Locating an Element in a 2D Array
• A is an M x N array in row-major order, where the size of the elements is S
• To find the location of A[i,j]
• find where row i begins
• find the location of the jth element in that row
• Row 0 begins at location A -- row i begins at location A + i*N*S
• The jth element is stored j*S bytes from the beginning of the row
• So, A[i,j] is in location A+(i*N + j)*S
2D Arrays and Based-Indexed Addressing Mode
• Suppose A is a 5x7 array stored in row-major order. Write code to clear row 2.
mov     bx,28             ; bx indexes row 2
xor     si,si             ; si will index columns
mov     cx,7              ; # elements in a row
clear: mov     [bx + si + A],0   ; clear A[2,j]
add     si,2              ; go to next column
loop    clear             ; loop until done
Another Example:
• Write code to clear column 3 -- Since A is a 7 column word array we need to add  2*7 = 14 to get to the next row
mov     si,6             ; si indexes column 3
xor     bx,bx            ; bx will index rows
mov     cx,5             ; #elements in a column
clear: mov     [bx + si + A],0  ; clear A[i,3]
add     bx,14            ; go to next row
loop    clear            ; loop until done