0% found this document useful (0 votes)
287 views

Assembly Language For Intel - Based Computers, 4 Edition

This document discusses the use of structures and unions in assembly language. It defines several structures, including COORD (for storing X,Y coordinates), SYSTEMTIME (for time values), and nested structures. It shows how to declare structure variables, access structure fields, pass structures to procedures, and initialize array fields. Unions provide an alternative way to access the same data using different field types. The document provides examples of using structures to store and display the system time, simulate a random walk, and store file information.

Uploaded by

Saa-dia Batool
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
287 views

Assembly Language For Intel - Based Computers, 4 Edition

This document discusses the use of structures and unions in assembly language. It defines several structures, including COORD (for storing X,Y coordinates), SYSTEMTIME (for time values), and nested structures. It shows how to declare structure variables, access structure fields, pass structures to procedures, and initialize array fields. Unions provide an alternative way to access the same data using different field types. The document provides examples of using structures to store and display the system time, simulate a random walk, and store file information.

Uploaded by

Saa-dia Batool
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Assembly Language for Intel-Based Computers, 4th Edition

Kip R. Irvine

Chapter 10: Structures and Macros

(c) Pearson Education, 2002. All rights reserved.

Chapter Overview
Structures Macros Conditional-Assembly Directives Defining Repeat Blocks

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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 Intel-Based Computers, 2003.

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 Intel-Based Computers, 2003.

Structure Definition Syntax

name STRUCT field-declarations name ENDS

Field-declarations are identical to variable declarations

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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 point1 COORD <5,10> point2 COORD <> worker Employee <>

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

Initializing Array Fields


Use the DUP operator to initialize one or more elements of an array field:
.data emp Employee <,,,4 DUP(20000)>

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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) >)

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

10

Referencing Structure Variables


Employee STRUCT IdNum BYTE "000000000" LastName BYTE 30 DUP(0) Years WORD 0 SalaryHistory DWORD 0,0,0,0 Employee ENDS .data worker Employee <> mov mov mov mov mov mov eax,TYPE Employee eax,SIZEOF Employee eax,SIZEOF worker eax,TYPE Employee.SalaryHistory eax,LENGTHOF Employee.SalaryHistory eax,SIZEOF Employee.SalaryHistory ; ; ; ; ; ; 57 57 57 4 4 16 ; ; ; ; ; ; bytes 9 30 2 16 57

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

11

. . . continued
mov mov mov mov dx,worker.Years worker.SalaryHistory,20000 worker.SalaryHistory+4,30000 edx,OFFSET worker.LastName

; first salary ; second salary

mov esi,OFFSET worker mov ax,(Employee PTR [esi]).Years mov ax,[esi].Years ; invalid operand (ambiguous)

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

12

Looping Through an Array of Points


Sets the X and Y coordinates of the AllPoints array to sequentially increasing values (1,1), (2,2), ...
.data NumPoints = 3 AllPoints COORD NumPoints DUP(<0,0>) .code mov edi,0 ; array index mov ecx,NumPoints ; loop counter mov ax,1 ; starting X, Y values L1: mov (COORD PTR AllPoints[edi]).X,ax mov (COORD PTR AllPoints[edi]).Y,ax add edi,TYPE COORD inc ax Loop L1
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

13

Example: Displaying the System Time

(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

Example: Displaying the System Time (2 of 3)


Uses a Windows API call to get the standard console output handle. SetConsoleCursorPosition positions the cursor. GetLocalTime gets the current time of day:
.data sysTime SYSTEMTIME <> XYPos COORD <10,5> consoleHandle DWORD ? colonStr BYTE :,0 .code INVOKE GetStdHandle, STD_OUTPUT_HANDLE mov consoleHandle,eax INVOKE SetConsoleCursorPosition, consoleHandle, XYPos INVOKE GetLocalTime, ADDR sysTime
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

15

Example: Displaying the System Time (3 of 3)


Display the time using library calls:
movzx call mov call movzx call mov call movzx call eax,sysTime.wHour WriteDec edx,offset colonStr WriteString eax,sysTime.wMinute WriteDec edx,offset colonStr WriteString eax,sysTime.wSecond WriteDec ; hours ; ":" ; minutes ; ":" ; seconds

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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> >

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

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

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

18

Example: Drunkard's Walk


Random-path simulation Uses a nested structure to accumulate path data as the simulation is running Uses a multiple branch structure to choose the direction
WalkMax = 50 DrunkardWalk STRUCT path COORD WalkMax DUP(<0,0>) pathsUsed WORD 0 DrunkardWalk ENDS

View the source code


Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

19

Declaring and Using Unions


A union is similar to a structure in that it contains multiple fields All of the fields in a union begin at the same offset
(differs from a structure)

Provides alternate ways to access the same data Syntax:


unionname UNION union-fields unionname ENDS

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

20

Integer Union Example


The Integer union consumes 4 bytes (equal to the largest field)
Integer UNION D DWORD 0 W WORD 0 B BYTE 0 Integer ENDS

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 <>

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

21

Union Inside a Structure


An Integer union can be enclosed inside a FileInfo structure:
Integer UNION D DWORD 0 W WORD 0 B BYTE 0 Integer ENDS FileInfo STRUCT FileID Integer <> FileName BYTE 64 DUP(?) FileInfo ENDS .data myFile FileInfo <> .code mov myFile.FileID.W, ax
22

Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003.

You might also like