Complex Data Structures
 
Dr. Tim McGuire
CS 272
Sam Houston State University

 

Structures

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
Declaring Structure Variables 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
Using Structured Variables mov     [today.day],17     ; change day to 17
mov     ax,[today.year]    ; put year into ax
add     [today.year],cx    ; add cx to year field
Arrays element     offset address         symbolic address     contents
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 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 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
addnos:
    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 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
addnos:
    add     ax,[bx+W] ; sum = sum + element
    add     bx,2      ; index next element
    loop    addnos    ; loop until done

The LABEL Pseudo-op 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[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]

Locating an Element in a 2D Array 2D Arrays and Based-Indexed Addressing Mode 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: 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