• 沒有找到結果。

Assembly Language for Intel

N/A
N/A
Protected

Academic year: 2022

Share "Assembly Language for Intel"

Copied!
22
0
0

全文

(1)

Assembly Language for Intel

Assembly Language for Intel - - Based Based Computers, 4

Computers, 4 th th Edition Edition

Chapter 10: Structures and Macros

Kip R. Irvine

(2)

Chapter Overview Chapter Overview

• Structures

• Macros

• Conditional-Assembly Directives

• Defining Repeat Blocks

(3)

Structure 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

(4)

Using a 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.

(5)

Structure Definition Syntax Structure Definition Syntax

name STRUCT

field-declarations name ENDS

• Field-declarations are identical to variable

declarations

(6)

COORD Structure COORD Structure

• The COORD structure used by the MS-Windows programming library identifies X and Y screen coordinates

COORD STRUCT

X WORD ? ; offset 00 Y WORD ? ; offset 02 COORD ENDS

(7)

Employee Structure Employee Structure

Employee STRUCT

IdNum BYTE "000000000"

LastName BYTE 30 DUP(0) Years WORD 0

SalaryHistory DWORD 0,0,0,0 Employee ENDS

A structure is ideal for combining fields of different types:

"000000000" (null) 0 0 0 0 0

SalaryHistory Lastname

Years Idnum

(8)

Declaring Structure Variables 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 <>

(9)

Initializing Array Fields Initializing Array Fields

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

.data

emp Employee <,,,4 DUP(20000)>

(10)

Array of Structures 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) >)

(11)

Referencing Structure Variables Referencing Structure Variables

.data

worker Employee <>

mov eax,TYPE Employee ; 57

mov eax,SIZEOF Employee ; 57

mov eax,SIZEOF worker ; 57

mov eax,TYPE Employee.SalaryHistory ; 4 mov eax,LENGTHOF Employee.SalaryHistory ; 4

Employee STRUCT ; bytes

IdNum BYTE "000000000" ; 9 LastName BYTE 30 DUP(0) ; 30

Years WORD 0 ; 2

SalaryHistory DWORD 0,0,0,0 ; 16

Employee ENDS ; 57

(12)

. . . continued . . . continued

mov dx,worker.Years

mov worker.SalaryHistory,20000 ; first salary mov worker.SalaryHistory+4,30000 ; second salary mov edx,OFFSET worker.LastName

mov esi,OFFSET worker

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

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

(13)

Looping Through an Array of Points Looping Through an Array of Points

.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

Sets the X and Y coordinates of the AllPoints array to

sequentially increasing values (1,1), (2,2), ...

(14)

Example: Displaying the System Time

Example: Displaying the System Time (1 of 3) (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 ?

(15)

Example: Displaying the System Time

Example: Displaying the System Time (2 of 3) (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,

(16)

Example: Displaying the System Time

Example: Displaying the System Time (3 of 3) (3 of 3)

• Display the time using library calls:

movzx eax,sysTime.wHour ; hours call WriteDec

mov edx,offset colonStr ; ":"

call WriteString

movzx eax,sysTime.wMinute ; minutes call WriteDec

mov edx,offset colonStr ; ":"

call WriteString

movzx eax,sysTime.wSecond ; seconds

(17)

Nested Structures

Nested Structures (1 of 2) (1 of 2)

Rectangle STRUCT

UpperLeft COORD <>

LowerRight COORD <>

Rectangle ENDS .code

rect1 Rectangle { {10,10}, {50,20} } rect2 Rectangle < <10,10>, <50,20> >

• Define a structure that contains other structures.

• Used nested braces (or brackets) to initialize each COORD structure.

COORD STRUCT X WORD ? Y WORD ? COORD ENDS

(18)

Nested Structures

Nested Structures (2 of 2) (2 of 2)

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

• Use the dot (.) qualifier to access nested fields.

• Use indirect addressing to access the overall

structure or one of its fields

(19)

Example: Drunkard's Walk 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

(20)

Declaring and Using Unions 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

(21)

Integer Union Example Integer Union Example

Integer UNION D DWORD 0 W WORD 0 B BYTE 0 Integer ENDS

The Integer union consumes 4 bytes (equal to the largest field)

.data

val1 Integer <12345678h>

val2 Integer <100h>

val3 Integer <>

D, W, and B are often called variant fields.

Integer can be used to define data:

(22)

Union Inside a Structure Union Inside a 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

An Integer union can be enclosed inside a FileInfo structure:

參考文獻

相關文件

Root the MRCT b T at its centroid r. There are at most two subtrees which contain more than n/3 nodes. Let a and b be the lowest vertices with at least n/3 descendants. For such

• The memory storage unit holds instructions and data for a running program.. • A bus is a group of wires that transfer data from one part to another (data,

• tiny (a single segment, used by .com programs), small (one code segment and one data segment), medium (multiple code segments and a single data segment), compact (one code

• The memory storage unit holds instructions and data for a running program.. • A bus is a group of wires that transfer data from one part to another (data,

• The first module, written in assembly language, contains the external procedure. • The second module contains the C/C++ code that starts and ends

• The Java programming language is based on the virtual machine concept. • A program written in the Java language is translated by a Java compiler into Java

The natural structure for two vari- ables is often a rectangular array with columns corresponding to the categories of one vari- able and rows to categories of the second

Reading Task 6: Genre Structure and Language Features. • Now let’s look at how language features (e.g. sentence patterns) are connected to the structure

Understanding and inferring information, ideas, feelings and opinions in a range of texts with some degree of complexity, using and integrating a small range of reading

Writing texts to convey information, ideas, personal experiences and opinions on familiar topics with elaboration. Writing texts to convey information, ideas, personal

Guiding students to analyse the language features and the rhetorical structure of the text in relation to its purpose along the genre egg model for content

Writing texts to convey simple information, ideas, personal experiences and opinions on familiar topics with some elaboration. Writing texts to convey information, ideas,

• The SHL (shift left) instruction performs a logical left shift on the destination operand, filling the lowest bit with

In the work of Qian and Sejnowski a window of 13 secondary structure predictions is used as input to a fully connected structure-structure network with 40 hidden units.. Thus,

The remaining positions contain //the rest of the original array elements //the rest of the original array elements.

3. Works better for some tasks to use grammatical tree structure Language recursion is still up to debate.. Recursive Neural Network Architecture. A network is to predict the

ReadInt - Reads a 32-bit signed decimal integer from standard input, terminated by the Enter key. ReadString - Reads a string from standard input, terminated by the

value2 BYTE 0 ; smallest unsigned byte value3 BYTE 255 ; largest unsigned byte value4 SBYTE -128 ; smallest signed byte value5 SBYTE +127 ; largest signed byte value6 BYTE.

xchg ax,bx ; exchange 16-bit regs xchg ah,al ; exchange 8-bit regs xchg var1,bx ; exchange mem, reg xchg eax,ebx ; exchange 32-bit regs.. xchg var1,var2 ; error: two

• If we want analysis with amortized costs to show that in the worst cast the average cost per operation is small, the total amortized cost of a sequence of operations must be

source:PTR BYTE, ; source string target:PTR BYTE ; target string. INVOKE Str_length,source ; EAX =

Its principle is to use Gyrator structure such that active MOSFET can equivalent to inductor ,along with parasitic capacitor to form a resonator tank for an oscillator.. The second

Inferring a chemical structure from a feature vector based on frequency of labeled-paths and small fragments, Branch-and-Bound Chemical compound Inference from