Structs Professor John Carelli Kutztown University Computer Science

  • Slides: 12
Download presentation
Structs Professor John Carelli Kutztown University Computer Science Department

Structs Professor John Carelli Kutztown University Computer Science Department

Structure • A template or pattern given to a logically related group of variables.

Structure • A template or pattern given to a logically related group of variables. • field - structure member containing data • Program access to a structure: • entire structure as a complete unit • individual fields • Useful way to pass multiple related arguments to a procedure • example: file directory information Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 2

Using a Structure Using a structure involves three sequential steps: 1. Define the structure.

Using a Structure Using a structure involves three sequential steps: 1. Define the structure. 2. Declare one or more variables of the structure type, called structure variables. 3. Write runtime instructions that access the structure. Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 3

Structure Definition Syntax name STRUCT field-declarations name ENDS • Field-declarations are identical to variable

Structure Definition Syntax name STRUCT field-declarations name ENDS • Field-declarations are identical to variable declarations Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 4

COORD Structure • The COORD structure used by the MS-Windows programming library identifies X

COORD Structure • The COORD structure used by the MS-Windows programming library identifies X and Y screen coordinates COORD STRUCT X WORD ? Y WORD ? COORD ENDS Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. ; offset 00 ; offset 02 5

Employee Structure A structure is ideal for combining fields of different types: Employee STRUCT

Employee Structure A structure is ideal for combining fields of different types: Employee STRUCT Id. Num BYTE "00000" Last. Name BYTE 30 DUP(0) Years WORD 0 Salary. History DWORD 0, 0, 0, 0 Employee ENDS Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 6

Declaring Structure Variables • Structure name is a user-defined type • Insert replacement initializers

Declaring Structure Variables • Structure name is a user-defined type • Insert replacement initializers between brackets: <. . . > • Empty brackets <> retain the structure's default field initializers • Examples: . data point 1 COORD <5, 10> point 2 COORD <> worker Employee <> Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 7

Initializing Array Fields • Use the DUP operator to initialize one or more elements

Initializing Array Fields • Use the DUP operator to initialize one or more elements of an array field: . data emp Employee <, , , 2 DUP(20000)> Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 8

Array of Structures • An array of structure objects can be defined using the

Array of Structures • An array of structure objects can be defined using the DUP operator. • Initializers can be used Num. Points = 3 All. Points COORD Num. Points DUP(<0, 0>) RD_Dept Employee 20 DUP(<>) accounting Employee 10 DUP(<, , , 4 DUP(20000) >) Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 9

Referencing Structure Variables Employee STRUCT ; bytes Id. Num BYTE "00000" ; 9 Last.

Referencing Structure Variables Employee STRUCT ; bytes Id. Num BYTE "00000" ; 9 Last. Name BYTE 30 DUP(0) ; 30 Years WORD 0 ; 2 Salary. History DWORD 0, 0, 0, 0 Employee ENDS ; 57 ; 16 . data worker Employee <> mov mov 4 mov eax, TYPE Employee ; 57 eax, SIZEOF worker ; 57 eax, TYPE Employee. Salary. History ; eax, LENGTHOF Employee. Salary. History ; eax, SIZEOF Employee. Salary. History Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. ; 16 10

. . . continued mov mov dx, worker. Years worker. Salary. History, 20000 ;

. . . continued mov mov dx, worker. Years worker. Salary. History, 20000 ; first salary worker. Salary. History+4, 30000 ; second salary edx, OFFSET worker. Last. Name mov esi, OFFSET worker mov ax, (Employee PTR [esi]). Years mov ax, [esi]. Years ; invalid operand (ambiguous) ; (not clear what type of struct ; “Years” belongs to) Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 11

Looping Through an Array of Points Sets the X and Y coordinates of the

Looping Through an Array of Points Sets the X and Y coordinates of the All. Points array to sequentially increasing values (1, 1), (2, 2), . . data Num. Points = 3 All. Points COORD Num. Points DUP(<0, 0>). code mov edi, 0 ; array index mov ecx, Num. Points ; loop counter mov ax, 1 ; starting X, Y values L 1: mov (COORD PTR All. Points[edi]). X, ax mov (COORD PTR All. Points[edi]). Y, ax add edi, TYPE COORD inc ax Loop L 1 Example: student. Struct Irvine, Kip R. Assembly Language for x 86 Processors 7/e, 2015. 12