+ All Categories
Home > Documents > CS2422 Assembly Language and System Programming Structure and Macros Department of Computer Science...

CS2422 Assembly Language and System Programming Structure and Macros Department of Computer Science...

Date post: 19-Dec-2015
Category:
View: 222 times
Download: 1 times
Share this document with a friend
32
CS2422 Assembly Language and System Programming Structure and Macros Department of Computer Science National Tsing Hua University
Transcript

CS2422 Assembly Language and System Programming

Structure and Macros

Department of Computer ScienceNational Tsing Hua University

CS2422 Assembly Language and System ProgrammingAssembly Language for Intel-Based Computers, 5th Edition

Chapter 10: Structures and Macros

(c) Pearson Education, 2006-2007. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.

Slides prepared by the author

Revision date: May 5, 2006

Kip Irvine

3

Chapter Overview

Structures Macros Conditional-Assembly Directives Defining Repeat Blocks

Brief overview of Chapter 9, e.g., MOVSB…etc. LODSB, STOSB…etc. REP Prefix

4

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

5

Structure Definition

Field-declarations: same as variable declarations Ex.: COORD structure by MS-Windows

programming library identifies X and Y screen coordinates

name STRUCT

field-declarations

name ENDS

COORD STRUCT

X WORD ? ; offset 00

Y WORD ? ; offset 02

COORD ENDS

6

Employee Structure

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

IdNum BYTE "000000000"LastName BYTE 30 DUP(0)Years WORD 0SalaryHistory DWORD 0,0,0,0

Employee ENDS

7

Declaring Structure Variables

Structure name is a user-defined type Insert replacement initializers between brackets:

‒ < . . . > Empty brackets <> retain default field initializers

Use DUP operator to initialize one or more elements of an array field:

.datapoint1 COORD <5,10>point2 COORD <>worker Employee <>

.dataemp Employee <,,,2 DUP(20000)>

8

Array of Structures

An array of structure objects can be defined using the DUP operator.

Initializers can be used

NumPoints = 3AllPoints COORD NumPoints DUP(<0,0>)

RD_Dept Employee 20 DUP(<>)

accounting Employee 10 DUP(<,,,4 DUP(20000) >)

9

Referencing Structure Variables

.dataworker Employee <>mov eax,TYPE Employee ; 57mov eax,SIZEOF Employee ; 57mov eax,SIZEOF worker ; 57mov eax,TYPE Employee.SalaryHistory ; 4mov eax,LENGTHOF Employee.SalaryHistory ; 4mov eax,SIZEOF Employee.SalaryHistory ; 16

Employee STRUCT ; bytesIdNum BYTE "000000000" ; 9LastName BYTE 30 DUP(0) ; 30Years WORD 0 ; 2SalaryHistory DWORD 0,0,0,0 ; 16

Employee ENDS ; 57

10

. . . continued

mov dx,worker.Yearsmov worker.SalaryHistory,20000 ; first salarymov worker.SalaryHistory+4,30000 ; second salarymov edx,OFFSET worker.LastName

mov esi,OFFSET workermov ax,(Employee PTR [esi]).Years

mov ax,[esi].Years ; invalid operand (ambiguous)

11

Loop through an Array of Points

Sets X and Y coordinates of the AllPoints array to sequentially increasing values (1,1), (2,2), ...

.dataNumPoints = 3AllPoints COORD NumPoints DUP(<0,0>).code

mov edi,0 ; array indexmov ecx,NumPoints ; loop countermov ax,1 ; starting X, Y values

L1:mov (COORD PTR AllPoints[edi]).X,axmov (COORD PTR AllPoints[edi]).Y,axadd edi,TYPE COORDinc axLoop L1

12

Nested Structures (1/2)

Define a structure that contains other structures. Used nested braces (or brackets) to initialize

each COORD structure.

Rectangle STRUCTUpperLeft COORD <>LowerRight COORD <>

Rectangle ENDS

.datarect1 Rectangle { {10,10}, {50,20} }rect2 Rectangle < <10,10>, <50,20> >

COORD STRUCT

X WORD ?

Y WORD ?

COORD ENDS

13

Nested Structures (2/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, 10mov esi,OFFSET rect1mov (Rectangle PTR [esi]).UpperLeft.Y, 10// use the OFFSET operatormov edi,OFFSET rect2.LowerRightmov (COORD PTR [edi]).X, 50mov edi,OFFSET rect2.LowerRight.Xmov WORD PTR [edi], 50

14

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 same offset (differs from a structure)

Provides alternate ways to access the same data Syntax:

unionname UNION

union-fields

unionname ENDS

15

Integer Union Example

The Integer union takes 4 bytes

D, W, and B are often called variant fields Can be used to define data:

Integer UNIOND DWORD 0W WORD 0B BYTE 0

Integer ENDS

.dataval1 Integer <12345678h>val2 Integer <100h>val3 Integer <>

16

Integer Union Example

The variant field name is required when accessing the union:

mov val3.B, almov ax,val3.Wadd val3.D, eax

17

What's Next

Structures Macros Conditional-Assembly Directives Defining Repeat Blocks

Brief overview of Chapter 9, e.g., MOVSB…etc. LODSB, STOSB…etc. REP Prefix

18

Introducing Macros

A macro1 is a named block of assembly language statements.

Once defined, it can be invoked (called) one or more times.

During the assembler's preprocessing step, each macro call is expanded into a copy of the macro.

The expanded code is passed to the assembly step, where it is checked for correctness.

1Also called a macro procedure.

19

Defining Macros

A macro must be defined before it can be used. Parameters are optional. Each parameter follows the rules for identifiers

It is a string that is assigned a value when the macro is invoked.

Syntax:

macroName MACRO [param-1, param-2,...]

statement-list

ENDM

20

Example: mPutchar Macro

Writes a single character to standard output

mPutchar MACRO charpush eaxmov al,charcall WriteCharpop eax

ENDM

Definition:

.codemPutchar 'A'

Invocation:

1 push eax1 mov al,'A'1 call WriteChar1 pop eax

Expansion:viewed in the listing file

21

Invoking Macros

Each parameter is replaced by its corresponding argument when the macro is expanded.

When a macro expands, it generates assembly language source code.

Arguments are treated as simple text by the preprocessor.

22

Invalid Argument

If you pass an invalid argument, the error is caught when the expanded code is assembled.

Example:

.codemPutchar 1234h

1 push eax1 mov al,1234h ; error!1 call WriteChar1 pop eax

23

Blank Argument

If you pass a blank argument, the error is also caught when the expanded code is assembled.

Example:

.codemPutchar

1 push eax1 mov al,1 call WriteChar1 pop eax

24

mGotoXY

mGotoXY macro sets console cursor position by calling the Gotoxy library procedure.

mGotoxy MACRO X:REQ, Y:REQpush edxmov dh,Ymov dl,Xcall Gotoxypop edx

ENDM

REQ next to X and Y identifies them as required parameters

25

mWriteStr Macro (1/2)

Provides a convenient way to display a string, by passing the string name as an argument.

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

ENDM

.datastr1 BYTE "Welcome!",0.codemWriteStr str1

26

mWriteStr Macro (2/2)

The expanded code shows how the str1 argument replaced the parameter named buffer:

1 push edx1 mov edx,OFFSET str11 call WriteString1 pop edx

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

ENDM

27

mWrite

Writes a string literal to standard output. An example macro containing both code and data

mWrite MACRO textLOCAL string.data ;; data segmentstring BYTE text,0 ;; define local string.code ;; code segmentpush edxmov edx,OFFSET stringcall Writestringpop edx

ENDM

LOCAL directive prevents “string” from becoming a global label

28

Nested Macros

A macro invoked by another macro.

mWriteLn MACRO textmWrite textcall Crlf

ENDM

mWriteLn "My Sample Macro Program"

2 .data2 ??0002 BYTE "My Sample Macro Program",02 .code2 push edx2 mov edx,OFFSET ??00022 call Writestring2 pop edx1 call Crlf

nesting level

Local label as a result of LOAD

directive

29

What's Next

Structures Macros Conditional-Assembly Directives Defining Repeat Blocks

Brief overview of Chapter 9, e.g., MOVSB…etc. LODSB, STOSB…etc. REP Prefix

Just to let you get a feeling of what they are about.

30

MOVSB, MOVSW, and MOVSD

The MOVSB, MOVSW, and MOVSD instructions copy data from the memory location pointed to by ESI to the memory location pointed to by EDI.

.datasource DWORD 0FFFFFFFFhtarget DWORD ?.code

mov esi,OFFSET sourcemov edi,OFFSET targetMOVSD

31

LODSB, LODSW, and LODSD

Load data from memory at ESI into EAX Increment/decrement ESI based on Direction flag Use them if want to process data before moving it.

.dataarray 1,2,3,4,5,6,7,8,9dest 9 DUP(?).code

mov esi,OFFSET arraymov edi,OFFSET destmov ecx,LENGTHOF arraycld ; Direction = forward

L1: LODSB ; EAX [ESI] or al,30h ; process dataSTOSB ; [EDI] EAXloop L1

32

Using a Repeat Prefix

REP (a repeat prefix) can be inserted just before MOVSB, MOVSW, or MOVSD.

ECX controls the number of repetitions Ex: Copy 20 doublewords from source to target

.datasource DWORD 20 DUP(?)target DWORD 20 DUP(?).codecld ; direction = forwardmov ecx,LENGTHOF source ; set REP countermov esi,OFFSET sourcemov edi,OFFSET targetREP MOVSD


Recommended