Chapter 10 Structures and Macros - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Chapter 10 Structures and Macros

Description:

The literal-text operator ( ) groups one or more characters and symbols into a ... literals that incorporate either assembler-style or C-style radix notation ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 57
Provided by: kip77
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10 Structures and Macros


1
Chapter 10 Structures and Macros
  • Assembly Language for
  • Intel-Based Computers,
  • 4th edition
  • Kip R. Irvine

2
Special Operators
  • The substitution () operator resolves ambiguous
    references to parameter names within a macro.
  • The expansion operator () expands text macros or
    converts constant expressions into their text
    representations.
  • The literal-text operator (ltgt) groups one or more
    characters and symbols into a single text
    literal. It prevents the preprocessor from
    interpreting members of the list as separate
    arguments.
  • The literal-character operator (!) forces the
    preprocessor to treat a predefined operator as an
    ordinary character.

3
Substitution ()
Text passed as regName is substituted into the
literal string definition
ShowRegister MACRO regName .data tempStr BYTE "
regName",0 . . .code ShowRegister EDX invoke
the macro
4
Expansion ()
Forces the evaluation of an integer expression.
After the expression has been evaluated, its
value is passed as a macro argument
mGotoXY (5 10),(3 4) The preprocessor
generates the following code 1 push edx 1 mov
dl,50 1 mov dh,7 1 call Gotoxy 1 pop edx
5
Expansion ()
Instructs the preprocessor to expand all text
macros and macro functions on the same line
.data Array DWORD 1, 2, 3, 4, 5, 6, 7,
8 .code TempStr TEXTEQU (SIZEOF array) ECHO
The array contains TempStr bytes
6
Literal-Text (ltgt)
The first macro call passes three arguments. The
second call passes a single argument
mWrite "Line three", 0dh, 0ah mWrite lt"Line
three", 0dh, 0ahgt
7
Literal-Character (!)
The following declaration prematurely ends the
text definition when the first gt character is
reached.
BadYValue TEXTEQU Warning ltY-coordinate is gt 24gt
8
Macro Functions (1 of 2)
  • A macro function returns an integer or string
    constant
  • The value is returned by the EXITM directive
  • Example The IsDefined macro acts as a wrapper
    for the IFDEF directive.

IsDefined MACRO symbol IFDEF symbol EXITM lt-1gt
True ELSE EXITM lt0gt False ENDIF ENDM
Notice how the assembler defines True and False.
9
Macro Functions (2 of 2)
  • When calling a macro function, the argument(s)
    must be enclosed in parentheses
  • The following code permits the two MOV statements
    to be assembled only if the RealMode symbol has
    been defined

IF IsDefined( RealMode ) mov ax,_at_data mov
ds,ax ENDIF
10
Defining Repeat Blocks
  • WHILE Directive
  • REPEAT Directive
  • FOR Directive
  • FORC Directive
  • Example Linked List

11
WHILE Directive
  • The WHILE directive repeats a statement block as
    long as a particular constant expression is true.
  • Syntax

WHILE constExpression statements ENDM
12
WHILE Example
Generates Fibonacci integers between 1 and
F0000000h at assembly time
.data val1 1 val2 1 DWORD val1 first two
values DWORD val2 val3 val1 val2 WHILE val3
LT 0F0000000h DWORD val3 val1 val2 val2
val3 val3 val1 val2 ENDM
13
REPEAT Directive
  • The REPEAT directive repeats a statement block a
    fixed number of times.
  • Syntax

REPEAT constExpression statements ENDM
ConstExpression, an unsigned constant integer
expression, determines the number of repetitions.
14
REPEAT Example
The following code generates 100 integer data
definitions in the sequence 10, 20, 30, . . .
iVal 10 REPEAT 100 DWORD iVal iVal iVal
10 ENDM
How might we assign a data name to this list of
integers?
15
Your turn . . .
What will be the last integer to be generated by
the following loop?
500
rows 10 columns 5 .data iVal 10 REPEAT rows
columns DWORD iVal iVal iVal 10 ENDM
16
FOR Directive
  • The FOR directive repeats a statement block by
    iterating over a comma-delimited list of symbols.
  • Each symbol in the list causes one iteration of
    the loop.
  • Syntax

FOR parameter,ltarg1,arg2,arg3,...gt statements END
M
17
FOR Example
The following Window structure contains frame,
title bar, background, and foreground colors. The
field definitions are created using a FOR
directive
Window STRUCT FOR color,ltframe,titlebar,backgroun
d,foregroundgt color DWORD ? ENDM Window ENDS
18
FORC Directive
  • The FORC directive repeats a statement block by
    iterating over a string of characters. Each
    character in the string causes one iteration of
    the loop.
  • Syntax

FORC parameter, ltstringgt statements ENDM
19
FORC Example
Suppose we need to accumulate seven sets of
integer data for an experiment. Their label
names are to be Group_A, Group_B, Group_C, and so
on. The FORC directive creates the variables
FORC code,ltABCDEFGgt Group_code WORD ? ENDM
20
Example Linked List (1 of 5)
  • We can use the REPEAT directive to create a
    singly linked list at assembly time.
  • Each node contains a pointer to the next node.
  • A null pointer in the last node marks the end of
    the list

21
Linked List (2 of 5)
  • Each node in the list is defined by a ListNode
    structure

ListNode STRUCT NodeData DWORD ? the node's
data NextPtr DWORD ? pointer to next
node ListNode ENDS TotalNodeCount 15 NULL
0 Counter 0
22
Linked List (3 of 5)
  • The REPEAT directive generates the nodes.
  • Each ListNode is initialized with a counter and
    an address that points 8 bytes beyond the current
    node's location

.data LinkedList LABEL DWORD REPEAT
TotalNodeCount Counter Counter 1 ListNode
ltCounter, ( Counter SIZEOF ListNode)gt ENDM
The value of does not changeit remains fixed
at the location of the LinkedList label.
23
Linked List (4 of 5)
The following hexadecimal values in each node
show how each NextPtr field contains the address
of its following node.
offset contents
00000000 00000001 00000008 00000008 00000002 000
00010 00000010 00000003 00000018 00000018 0000000
4 00000020 00000020 (etc.)
NextPtr
24
Linked List (5 of 4)
View the program's source code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sample output
25
Chapter 12 High-Level Language Interface
  • Assembly Language for
  • Intel-Based Computers,
  • 4th edition
  • Kip R. Irvine

26
Chapter Overview
  • Why Link ASM and HLL Programs?
  • General and Calling Conventions
  • External Identifiers
  • Inline Assembly Code
  • __asm Directive
  • File Encryption Example
  • Linking to C Programs
  • Linking to Borland C
  • ReadSector Example
  • Special Section Optimizing Your Code
  • Loop Optimization Example
  • FindArray Example
  • Creating the FindArray Project

27
Why Link ASM and HLL Programs?
  • Use high-level language for overall project
    development
  • Relieves programmer from low-level details
  • Use assembly language code
  • Speed up critical sections of code
  • Access nonstandard hardware devices
  • Write platform-specific code
  • Extend the HLL's capabilities

28
General Conventions
  • Considerations when calling assembly language
    procedures from high-level languages
  • Both must use the same naming convention (rules
    regarding the naming of variables and procedures)
  • Both must use the same memory model, with
    compatible segment names
  • Both must use the same calling convention

29
Calling Convention
  • Identifies specific registers that must be
    preserved by procedures
  • Determines how arguments are passed to
    procedures in registers, on the stack, in shared
    memory, etc.
  • Determines the order in which arguments are
    passed by calling programs to procedures
  • Determines whether arguments are passed by value
    or by reference
  • Determines how the stack pointer is restored
    after a procedure call
  • Determines how functions return values

30
External Identifiers
  • An external identifier is a name that has been
    placed in a modules object file in such a way
    that the linker can make the name available to
    other program modules.
  • The linker resolves references to external
    identifiers, but can only do so if the same
    naming convention is used in all program modules.

31
Inline Assembly Code
  • Assembly language source code that is inserted
    directly into a HLL program.
  • Compilers such as Microsoft Visual C and
    Borland C have compiler-specific directives
    that identify inline ASM code.
  • Efficient inline code executes quickly because
    CALL and RET instructions are not required.
  • Simple to code because there are no external
    names, memory models, or naming conventions
    involved.
  • Decidedly not portable because it is written for
    a single platform.

32
_asm Directive in Microsoft Visual C
  • Can be placed at the beginning of a single
    statement
  • Or, It can mark the beginning of a block of
    assembly language statements
  • Syntax

__asm statement __asm statement-1
statement-2 ... statement-n
33
Commenting Styles
All of the following comment styles are
acceptable, but the latter two are preferred
mov esi,buf initialize index register mov
esi,buf // initialize index register mov
esi,buf / initialize index register /
34
You Can Do the Following . . .
  • Use any instruction from the Intel instruction
    set
  • Use register names as operands
  • Reference function parameters by name
  • Reference code labels and variables that were
    declared outside the asm block
  • Use numeric literals that incorporate either
    assembler-style or C-style radix notation
  • Use the PTR operator in statements such as inc
    BYTE PTR esi
  • Use LENGTH, TYPE, and SIZE directives

35
You Cannot Do the Following . . .
  • Use data definition directives such as DB, DW, or
    BYTE
  • Use assembler operators other than PTR
  • Use STRUCT
  • Use macro directives such as MACRO, REPT, ENDM
  • Reference segments by name.
  • (You can, however, use segment register names as
    operands.)

36
Register Usage
  • In general, you can modify EAX, EBX, ECX, and EDX
    in your inline code because the compiler does not
    expect these values to be preserved between
    statements
  • Conversely, always save and restore ESI, EDI, and
    EBP.

37
File Encryption Example
  • Reads a file, encrypts it, and writes the output
    to another file.
  • The TranslateBuffer function uses an __asm block
    to define statements that loop through a
    character array and XOR each character with a
    predefined value.

Void TranslateBuffer (char buf, unsigned count,
unsigned char encryptChar) _asm
mov esi, buf mov ecx, count mov al,
encryptChar L1 xor esi, al inc esi loop
L1 // asm
38
Linking Assembly Language to C
  • Basic Structure - Two Modules
  • The first module, written in assembly language,
    contains the external procedure
  • The second module contains the C/C code that
    starts and ends the program
  • The C module adds the extern qualifier to the
    external assembly language function prototype.
  • The "C" specifier must be included to prevent
    name decoration by the C compiler

extern "C" functionName( parameterList )
39
Name Decoration
Also known as name mangling. HLL compilers do
this to uniquely identify overloaded functions. A
function such as int ArraySum( int p, int
count ) would be exported as a decorated name
that encodes the return type, function name, and
parameter types. For example int_ArraySum_pInt_in
t The problem with name decoration is that the
C compiler assumes that your assembly language
function's name is decorated. The C compiler
tells the linker to look for a decorated name.
C compilers vary in the way they decorate
function names.
40
Linking to Borland C
  • We will look at a C program that calls an
    external assembly language procedure named
    ReadSector
  • Reads a range of sectors from a disk drive
  • Not possible with pure C code
  • ASM code uses 16-bit MS-DOS functions
  • Tools
  • 16-bit version of Borland C 5.01
  • Borland TASM 4.0 assembler (included with Borland
    C)

41
ReadSector Sample Output
Sector display program. Enter drive number 1A,
2B, 3C, 4D, 5E,... 1 Starting sector number
to read 0 Number of sectors to read 20
Reading sectors 0 - 20 from Drive 1 Sector 0
--------------------------------------------------
------ .lt.(P3j2IHC........_at_..................)Y...
MYDISK FAT12 .3. .......x..v..V.U."....N..
.........E...F..E.8N"....w.r...f.. f..W.u...
..V....s.3..F...f..F..V..F....v..F..V..
.......H...F ..N.a.....r98-t.......at9Nt...
.r............t.lt.t.......... ............f..
.......E..N....F..V......r....p..B.-fj.RP.Sj .j
...t...3..v...v.B...v..............V...d.ar._at_u.B.
.Iuw....'..I nvalid system disk...Disk I/O
error...Replace the disk, and then press any
key....IOSYSMSDOS SYS...A......._at_...U.
42
Special Section Optimizing Your Code
  • The 90/10 rule 90 of a program's CPU time is
    spent executing 10 of the program's code
  • We will concentrate on optimizing ASM code for
    speed of execution
  • Loops are the most effective place to optimize
    code
  • Two simple ways to optimize a loop
  • Move invariant code out of the loop
  • Substitute registers for variables to reduce the
    number of memory accesses
  • Take advantage of high-level instructions such as
    XLAT, SCASB, and MOVSD.

43
Loop Optimization Example
  • We will write a short program that calculates and
    displays the number of elapsed minutes, over a
    period of n days.
  • The following variables are used

.data days DWORD ? minutesInDay DWORD
? totalMinutes DWORD ? str1 BYTE "Daily total
minutes ",0
44
Sample Program Output
Daily total minutes 1440 Daily total minutes
2880 Daily total minutes 4320 Daily total
minutes 5760 Daily total minutes 7200 Daily
total minutes 8640 Daily total minutes
10080 Daily total minutes 11520 . . Daily
total minutes 67680 Daily total minutes
69120 Daily total minutes 70560 Daily total
minutes 72000
45
Version 1
No optimization. mov days,0 mov
totalMinutes,0 L1 loop contains 15
instructions mov eax,24 minutesInDay 24
60 mov ebx,60 mul ebx mov minutesInDay,eax mov
edx,totalMinutes totalMinutes
minutesInDay add edx,minutesInDay mov
totalMinutes,edx mov edx,OFFSET str1 "Daily
total minutes " call WriteString mov
eax,totalMinutes display totalMinutes call
WriteInt call Crlf inc days days cmp
days,50 if days lt 50, jb L1 repeat the
loop
46
Version 2
Move calculation of minutesInDay outside the
loop, and assign EDX before the loop. The loop
now contains 10 instructions. mov days,0 mov
totalMinutes,0 mov eax,24 minutesInDay 24
60 mov ebx,60 mul ebx mov minutesInDay,eax m
ov edx,OFFSET str1 "Daily total minutes
" L1 mov ebx,totalMinutes totalMinutes
minutesInDay add ebx,minutesInDay mov
totalMinutes,ebx call WriteString display
str1 (offset in EDX) mov eax,totalMinutes
display totalMinutes call WriteInt call
Crlf inc days days cmp days,50 if days
lt 50, jb L1 repeat the loop
47
Version 3
Move totalMinutes to EAX, use EAX throughout
loop. Use constant expresion for minutesInDay
calculation. The loop now contains 7
instructions. C_minutesInDay 24 60
constant expression mov days,0 mov
totalMinutes,0 mov eax,totalMinutes mov
edx,OFFSET str1 "Daily total minutes
" L1 add eax,C_minutesInDay totalMinutes
minutesInDay call WriteString display str1
(offset in EDX) call WriteInt display
totalMinutes (EAX) call Crlf inc days
days cmp days,50 if days lt 50, jb L1
repeat the loop mov totalMinutes,eax update
variable
48
Version 4
Substitute ECX for the days variable. Remove
initial assignments to days and
totalMinutes. C_minutesInDay 24 60
constant expression mov eax,0 EAX
totalMinutes mov ecx,0 ECX days mov
edx,OFFSET str1 "Daily total minutes
" L1 loop contains 7 instructions add
eax,C_minutesInDay totalMinutes
minutesInDay call WriteString display str1
(offset in EDX) call WriteInt display
totalMinutes (EAX) call Crlf inc ecx days
(ECX) cmp ecx,50 if days lt 50, jb L1
repeat the loop mov totalMinutes,eax update
variable mov days,ecx update variable
49
Using Assembly Language to Optimize C
  • Find out how to make your C compiler produce an
    assembly language source listing
  • /FAs command-line option in Visual C, for
    example
  • Optimize loops for speed
  • Use hardware-level I/O for optimum speed
  • Use BIOS-level I/O for medium speed

50
FindArray Example
Let's write a C function that searches for the
first matching integer in an array. The function
returns true if the integer is found, and false
if it is not
include "findarr.h" bool FindArray( long
searchVal, long array, long
count ) for(int i 0 i lt count i)
if( searchVal arrayi ) return true
return false
51
Code Produced by C Compiler
optimization switch turned off (1 of 3)
_searchVal 8 _array 12 _count 16 _i
-4 _FindArray PROC NEAR 29 push
ebp mov ebp, esp push ecx 30 for(int i
0 i lt count i) mov DWORD PTR _iebp,
0 jmp SHORT L174 L175 mov eax, DWORD PTR
_iebp add eax, 1 mov DWORD PTR _iebp,
eax
52
Code Produced by C Compiler
(2 of 3)
L174 mov ecx, DWORD PTR _iebp cmp ecx,
DWORD PTR _countebp jge SHORT L176 31
if( searchVal arrayi ) mov edx, DWORD PTR
_iebp mov eax, DWORD PTR _arrayebp mov
ecx, DWORD PTR _searchValebp cmp ecx, DWORD
PTR eaxedx4 jne SHORT L177 32 return
true mov al, 1 jmp SHORT L172 L177 33
34 return false jmp SHORT L175
53
Code Produced by C Compiler
(3 of 3)
L176 xor al, al AL 0 L172 35
mov esp, ebp restore stack pointer pop
ebp ret 0 _FindArray ENDP
54
Hand-Coded Assembly Language (1 of 2)
true 1 false 0 Stack parameters srchVal
equ ebp08 arrayPtr equ ebp12 count
equ ebp16 .code _FindArray PROC near
push ebp mov ebp,esp push edi
mov eax, srchVal search value mov
ecx, count number of items mov edi,
arrayPtr pointer to array
55
Hand-Coded Assembly Language (2 of 2)
repne scasd do the search
jz returnTrue ZF 1 if
found returnFalse mov al,
false jmp short exit returnTrue
mov al, true exit pop edi pop
ebp ret _FindArray ENDP
56
  • CSCE 380
  • Department of Computer Science and Computer
    Engineering
  • Pacific Lutheran University
  • 11/19/2002
Write a Comment
User Comments (0)
About PowerShow.com