About me

Course Work




Data Structures

Believe it or not, even a low level language such as assembly can support data structures. The basic declaration is as follows:

    structName  STRUC
       field declariations
    structName ENDS

For example,

    Student STRUC
       Name    DB      40 dup(?)
       SSN     DB       9 dup(?)
       GPA     DB      "4.000"
    Student ENDS

Notice that the GPA field has been initialized to "4.000". By default, this means that every instance of Student will have the GPA field set to that value.

To declare an instance of a structure you can do the following

    Student1 Student <"Jane Doe", "123456789",>

In the above case, the variable (Student1)) is created and the first and second fields are explicitly defined. The GPA field is automatically set to "4.000". We don't have to declare any of the fields. If we wanted to create an array of students and leave all of them blank we could do the following:

    Students Student 10 dup(<>)

which creates an array of ten students. Each of the GPA fields are initialized by default, and the rest of the structure is uninitialized.

To access a structure you can use shortcuts

             (is equal to)
    OFFSET Student1 + OFFSET name

Remember this is valid since the OFFSET command is a preprocessor directive. One additional preprocessor directive that you will find useful when working with structures is TYPE. TYPE Student produces the total length of a Student structure. To work with an array, you need to calculate the offset to an entity within that array. For example:

    mov     ax, 5                 ;index of record we want
    mov     cx, TYPE Student      ;length of a Student structure
    mul     cx
    mov     bx, ax                ;BX points to the start of Students[5]

    mov     dx, Students[bx].name ;point DX to the student[5].name string
    ...                           ;print name or do something important

Another creative use of structures is for retrieving parameters from the stack. I will leave this as an exercise for you now, and may cover this as an example in class if time permits. To give you a hint/warning, understand that you will need to different structures for near and far calls

Last Modified: January 26, 1999 - Barry E. Mapen