Assembly Language For Intel - Based Computers, 4 Edition
Assembly Language For Intel - Based Computers, 4 Edition
Kip R. Irvine
Chapter Overview
Structures Macros Conditional-Assembly Directives Defining Repeat Blocks
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
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.
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
; offset 00 ; offset 02
Employee Structure
A structure is ideal for combining fields of different types:
Employee STRUCT IdNum BYTE "000000000" LastName BYTE 30 DUP(0) Years WORD 0 SalaryHistory DWORD 0,0,0,0 Employee ENDS
"000000000" Idnum
(null) Lastname
SalaryHistory Years
Array of Structures
An array of structure objects can be defined using the DUP operator. Initializers can be used
NumPoints = 3 AllPoints COORD NumPoints DUP(<0,0>) RD_Dept Employee 20 DUP(<>) accounting Employee 10 DUP(<,,,4 DUP(20000) >)
10
11
. . . continued
mov mov mov mov dx,worker.Years worker.SalaryHistory,20000 worker.SalaryHistory+4,30000 edx,OFFSET worker.LastName
mov esi,OFFSET worker mov ax,(Employee PTR [esi]).Years mov ax,[esi].Years ; invalid operand (ambiguous)
12
13
(1 of 3)
Retrieves and displays the system time at a selected screen location. Uses COORD and SYSTEMTIME structures:
SYSTEMTIME STRUCT wYear WORD ? wMonth WORD ? wDayOfWeek WORD ? wDay WORD ? wHour WORD? wMinute WORD ? wSecond WORD ? wMilliseconds WORD ? SYSTEMTIME ENDS
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.
14
15
16
Nested Structures (1 of 2)
Define a structure that contains other structures. Used nested braces (or brackets) to initialize each COORD structure.
Rectangle STRUCT UpperLeft COORD <> LowerRight COORD <> Rectangle ENDS COORD STRUCT X WORD ? Y WORD ? COORD ENDS
.code rect1 Rectangle { {10,10}, {50,20} } rect2 Rectangle < <10,10>, <50,20> >
17
Nested Structures (2 of 2)
Use the dot (.) qualifier to access nested fields. Use indirect addressing to access the overall structure or one of its fields
mov rect1.UpperLeft.X, 10 mov esi,OFFSET rect1 mov (Rectangle PTR [esi]).UpperLeft.Y, 10 // use the OFFSET operator mov edi,OFFSET rect2.LowerRight mov (COORD PTR [edi]).X, 50 mov edi,OFFSET rect2.LowerRight.X mov WORD PTR [edi], 50
18
19
20
D, W, and B are often called variant fields. Integer can be used to define data:
.data val1 Integer <12345678h> val2 Integer <100h> val3 Integer <>
21