Top Banner
1 Lecture 6 Lecture 6 Conditional Processing Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine
63

1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

1

Lecture 6Lecture 6

Conditional ProcessingConditional Processing

Assembly Language for

Intel-Based Computers,

4th edition

Kip R. Irvine

Page 2: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

2

OutlineOutline

Boolean and Comparison Instructions

Conditional Jumps Conditional Loops High-Level Logic Structures

Page 3: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

3

Logic InstructionsLogic Instructions Syntax for AND, OR, XOR, and TEST:

op-code destination, source AND, OR and XOR perform the Boolean bitwise

operation and store the result into destination. TEST is just an AND but the result is not stored Both operands must be of the same type

either byte, word or doubleword Both operands cannot be memory at same time

again: memory to memory operations are forbidden They modify OF, CF, SF, ZF, PF according to the

result of the operation (as usual)

Page 4: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

4

Logic Instructions (cont.)Logic Instructions (cont.)

The source is often an immediate operand called a bit mask: used to fix certain bits to 0 or 1

To clear a bit we use an AND since: 0 AND b = 0 (b is cleared) 1 AND b = b (b is conserved)

Ex: to clear the sign bit of AL without affecting the others, we do: AND al,7Fh ;msb of AL is cleared

since 7Fh = 0111 1111b

Page 5: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

5

Logic Instructions (cont.)Logic Instructions (cont.)

To set (ie: set to 1) certain bits, we use OR: 1 OR b = 1 (b is set) 0 OR b = b (b is conserved)

To set the sign bit of AH, we do: OR ah,80h

To test if ECX=0 we can do: OR ecx,ecx since this does not change the number in ECX

and set ZF=1 if and only if ecx=0

Page 6: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

6

Logic Instructions (cont.)Logic Instructions (cont.)

XOR can be used to complement, conserve, or clear certain bits because: b XOR 1 = NOT(b) (b is complemented) b XOR 0 = b (b is conserved) b XOR b = 0 (b is cleared)

Ex: to initialize a register to 0 we can use a two-byte instruction: XOR ax,ax instead of the three-byte instruction: MOV ax,0

This was faster on 8088's

Page 7: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

7

Logic Instructions (cont.)Logic Instructions (cont.)

Using OR and XOR in strange ways: XOR ax, ax MOV ax, 0

OR ax, ax CMP ax, 0bytes 2 386/88 3 cycles 4 cycles286 2 cycles 2 cycles386 2 cycles 2 cycles486 1 cycle 1 cycle

Machine code 33 C0 B8 0000

0B C0 83 F8 00

Page 8: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

8

Logic Instructions (cont.)Logic Instructions (cont.)

To convert from upper case to lower case we can use the usual method: ADD dl,20h

But "A" = 0100 0001b "a" = 0110 0001b OR dl,20h ;converts from upper to lower case AND dl,0DFh ;converts from lower to upper case

since DFh = 1101 1111b OR dl,20h and AND dh, 20h

leaves lower case characters in dl unchanged and upper case characters in dh unchanged

Bit 5

Page 9: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

9

Logic Instructions (cont.)Logic Instructions (cont.)

To invert all the bits (ones complement): NOT destination does not affect any flag and destination cannot

be an immediate operand

Reflection What are the easiest ways to clearing or

setting individual CPU flags?

Page 10: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

10

The CMP instructionThe CMP instruction CMP destination,source

Performs: destination - source but does not store the result of this subtraction into destination

But the flags are affected like SUB Same restrictions on operands as SUB Very often used just before performing a

conditional jump CMP is similar to TEST except TEST does

AND operation

Page 11: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

11

Conditional JumpsConditional Jumps Jumps Based On . . .

Specific flags Equality Unsigned comparisons Signed Comparisons

Applications Encrypting a String Bit Test (BT) Instruction

Page 12: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

12

JJcondcond Instruction Instruction

A conditional jump instruction branches to a label when specific register or flag conditions are met

Examples: JB, JC jump to a label if the Carry flag is set JE, JZ jump to a label if the Zero flag is set JS jumps to a label if the Sign flag is set JNE, JNZ jump to a label if the Zero flag is clear JECXZ jumps to a label if ECX equals 0

Page 13: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

13

Jumps Based on Specific FlagsJumps Based on Specific Flags

Page 14: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

14

Jumps Based on EqualityJumps Based on Equality

Page 15: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

15

Jumps Based on Unsigned Jumps Based on Unsigned ComparisonsComparisons

Page 16: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

16

Jumps Based on Signed ComparisonsJumps Based on Signed Comparisons

Page 17: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

17

Applications Applications (1 of 5) (1 of 5)

cmp eax,ebxja Larger

• Task: Jump to a label if unsigned EAX is greater than EBX

• Solution: Use CMP, followed by JA

cmp eax,ebxjg Greater

• Task: Jump to a label if signed EAX is greater than EBX

• Solution: Use CMP, followed by JG

Page 18: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

18

Applications Applications (2 of 5) (2 of 5)

cmp eax,Val1jbe L1 ; below or equal

• Jump to label L1 if unsigned EAX is less than or equal to Val1

cmp eax,Val1jle L1

• Jump to label L1 if signed EAX is less than or equal to Val1

Page 19: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

19

Applications Applications (3 of 5) (3 of 5)

mov Large,bxcmp ax,bxjna Nextmov Large,ax

Next:

• Compare unsigned AX to BX, and copy the larger of the two into a variable named Large

mov Small,axcmp bx,axjnl Nextmov Small,bx

Next:

• Compare signed AX to BX, and copy the smaller of the two into a variable named Small

Page 20: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

20

Applications Applications (4 of 5) (4 of 5)

cmp WORD PTR [esi],0je L1

• Jump to label L1 if the memory word pointed to by ESI equals Zero

test DWORD PTR [edi],1jz L2

• Jump to label L2 if the doubleword in memory pointed to by EDI is even

Page 21: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

21

Applications Applications (5 of 5) (5 of 5)

and al,00001011b ; clear unwanted bitscmp al,00001011b ; check remaining bitsje L1 ; all set? jump to L1

• Task: Jump to label L1 if bits 0, 1, and 3 in AL are all set.

• Solution: Clear all bits except bits 0, 1,and 3. Then compare the result with 00001011 binary.

Page 22: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

22

Your turn . . .Your turn . . . Write code that jumps to label L1 if either

bit 4, 5, or 6 is set in the BL register. Write code that jumps to label L1 if bits 4,

5, and 6 are all set in the BL register. Write code that jumps to label L3 if EAX is

negative.

Page 23: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

23

Encrypting a StringEncrypting a String

KEY = 239 ; can be any byte valueBUFMAX = 128.databuffer BYTE BUFMAX+1 DUP(0)bufSize DWORD BUFMAX

.codemov ecx,bufSize ; loop countermov esi,0 ; index 0 in buffer

L1:xor buffer[esi],KEY ; translate a byteinc esi ; point to next byteloop L1

The following loop uses the XOR instruction to transform every character in a string into a new value.

Page 24: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

24

String Encryption ProgramString Encryption Program

Tasks: Input a message (string) from the user Encrypt the message Display the encrypted message Decrypt the message Display the decrypted message

View the Encrypt.asm program's source code. Sample output:

Enter the plain text: Attack at dawn.

Cipher text: «¢¢Äîä-Ä¢-ïÄÿü-Gs

Decrypted: Attack at dawn.

Page 25: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

25

Conditional Loop InstructionsConditional Loop Instructions

LOOPZ and LOOPE LOOPNZ and LOOPNE

Page 26: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

26

LOOPZ and LOOPELOOPZ and LOOPE Syntax:

LOOPE destinationLOOPZ destination

Logic: ECX ECX – 1 if ECX > 0 and ZF=1, jump to destination

Useful when scanning an array for the first element that does not match a given value.

Page 27: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

27

LOOPNZ and LOOPNELOOPNZ and LOOPNE

LOOPNZ (LOOPNE) is a conditional loop instruction

Syntax: LOOPNZ destinationLOOPNE destination

Logic: ECX ECX – 1; if ECX > 0 and ZF=0, jump to destination

Useful when scanning an array for the first element that matches a given value.

Page 28: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

28

LOOPNZ ExampleLOOPNZ Example

.dataarray SWORD -3,-6,-1,-10,10,30,40,4sentinel SWORD 0.code

mov esi,OFFSET arraymov ecx,LENGTHOF array

next:test WORD PTR [esi],8000h ; test sign bitpushfd ; push flags on stackadd esi,TYPE arraypopfd ; pop flags from stackloopnz next ; continue loopjnz quit ; none foundsub esi,TYPE array ; ESI points to value

quit:

The following code finds the first positive value in an array:

Page 29: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

29

Your turn . . .Your turn . . .

.dataarray SWORD 50 DUP(?)sentinel SWORD 0FFFFh.code

mov esi,OFFSET arraymov ecx,LENGTHOF array

L1: cmp WORD PTR [esi],0 ; check for zero

(fill in your code here)

quit:

Locate the first nonzero value in the array. If none is found, let ESI point to the sentinel value:

Page 30: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

30

. . . (solution). . . (solution).dataarray SWORD 50 DUP(?)sentinel SWORD 0FFFFh.code

mov esi,OFFSET arraymov ecx,LENGTHOF array

L1: cmp WORD PTR [esi],0 ; check for zeropushfd ; push flags on stackadd esi,TYPE arraypopfd ; pop flags from stackloope L1 ; continue loopjz quit ; none foundsub esi,TYPE array ; ESI points to value

quit:

Page 31: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

31

Conditional StructuresConditional Structures

• Block-Structured IF Statements

• Compound Expressions with AND

• Compound Expressions with OR

• WHILE Loops

• Table-Driven Selection

Page 32: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

32

Block-Structured IF StatementsBlock-Structured IF Statements

Assembly language programmers can easily translate

logical statements written in C++/Java into assembly

language. For example:

mov eax,op1cmp eax,op2jne L1mov X,1jmp L2

L1: mov X,2L2:

if( op1 == op2 ) X = 1;else X = 2;

Page 33: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

33

Your turn . . .Your turn . . .

Implement the following pseudocode in

assembly language. All values are unsigned:

cmp ebx,ecxja nextmov eax,5mov edx,6

next:

if( ebx <= ecx ){ eax = 5; edx = 6;}

(There are multiple correct solutions to this problem.)

Page 34: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

34

Your turn . . .Your turn . . .

Implement the following pseudocode in assembly

language. All values are 32-bit signed integers:mov eax,var1cmp eax,var2jle L1mov var3,6mov var4,7jmp L2

L1: mov var3,10L2:

if( var1 <= var2 ) var3 = 10;else{ var3 = 6; var4 = 7;}

(There are multiple correct solutions to this problem.)

Page 35: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

35

Compound Expression with ANDCompound Expression with AND (1 of 3) (1 of 3)

When implementing the logical AND operator, consider

that HLLs use short-circuit evaluation

In the following example, if the first expression is false, the

second expression is skipped:

if (al > bl) AND (bl > cl) X = 1;

Page 36: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

36

Compound Expression with ANDCompound Expression with AND (2 of 3) (2 of 3)

cmp al,bl ; first expression...ja L1jmp next

L1:cmp bl,cl ; second expression...ja L2jmp next

L2: ; both are truemov X,1 ; set X to 1

next:

if (al > bl) AND (bl > cl) X = 1;

This is one possible implementation . . .

Page 37: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

37

Compound Expression with ANDCompound Expression with AND (3 of 3) (3 of 3)

cmp al,bl ; first expression...jbe next ; quit if falsecmp bl,cl ; second expression...jbe next ; quit if falsemov X,1 ; both are true

next:

if (al > bl) AND (bl > cl) X = 1;

But the following implementation uses 29% less code by reversing the first relational operator. We allow the program to "fall through" to the second expression:

Page 38: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

38

Your turn . . .Your turn . . .

Implement the following pseudocode in

assembly language. All values are unsigned:

cmp ebx,ecxja nextcmp ecx,edxjbe nextmov eax,5mov edx,6

next:

if( ebx <= ecx && ecx > edx )

{ eax = 5; edx = 6;}

(There are multiple correct solutions to this problem.)

Page 39: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

39

Compound Expression with ORCompound Expression with OR (1 of 2) (1 of 2) When implementing the logical OR operator, consider that

HLLs use short-circuit evaluation

In the following example, if the first expression is true, the

second expression is skipped:

if (al > bl) OR (bl > cl) X = 1;

Page 40: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

40

Compound Expression with ORCompound Expression with OR (1 of 2) (1 of 2)

cmp al,bl ; is AL > BL?ja L1 ; yescmp bl,cl ; no: is BL > CL?jbe next ; no: skip next statement

L1: mov X,1 ; set X to 1next:

if (al > bl) OR (bl > cl) X = 1;

We can use "fall-through" logic to keep the code as short as possible:

Page 41: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

41

WHILE LoopsWHILE Loops

while( eax < ebx)eax = eax + 1;

A WHILE loop is really an IF statement followed by the body of the loop, followed by an unconditional jump to the top of the loop. Consider the following example:

top:cmp eax,ebx ; check loop conditionjae next ; false? exit loopinc eax ; body of loopjmp top ; repeat the loop

next:

This is a possible implementation:

Page 42: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

42

Your turn . . .Your turn . . .

top:cmp ebx,val1 ; check loop conditionja next ; false? exit loopadd ebx,5 ; body of loopdec val1jmp top ; repeat the loop

next:

while( ebx <= val1){

ebx = ebx + 5;val1 = val1 - 1

}

Implement the following loop, using unsigned 32-bit integers:

Page 43: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

43

Using the .IF DirectiveUsing the .IF Directive

Runtime Expressions Relational and Logical

Operators MASM-Generated Code .REPEAT Directive .WHILE Directive

Page 44: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

44

Runtime ExpressionsRuntime Expressions

.IF eax > ebxmov edx,1

.ELSEmov edx,2

.ENDIF

• .IF, .ELSE, .ELSEIF, and .ENDIF can be used to evaluate runtime expressions and create block-structured IF statements.

• Examples:

• MASM generates "hidden" code for you, consisting of code labels, CMP and conditional jump instructions.

.IF eax > ebx && eax > ecxmov edx,1

.ELSEmov edx,2

.ENDIF

Page 45: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

45

Relational and Logical OperatorsRelational and Logical Operators

Page 46: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

46

MASM-Generated CodeMASM-Generated Code

mov eax,6cmp eax,val1jbe @C0001 mov result,1

@C0001:

.data

val1 DWORD 5

result DWORD ?

.code

mov eax,6

.IF eax > val1

mov result,1

.ENDIF

Generated code:

MASM automatically generates an unsigned jump (JBE) because val1 is unsigned.

Page 47: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

47

MASM-Generated CodeMASM-Generated Code

mov eax,6cmp eax,val1jle @C0001 mov result,1

@C0001:

.data

val1 SDWORD 5

result SDWORD ?

.code

mov eax,6

.IF eax > val1

mov result,1

.ENDIF

Generated code:

MASM automatically generates a signed jump (JLE) because val1 is signed.

Page 48: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

48

MASM-Generated CodeMASM-Generated Code

mov ebx,5mov eax,6cmp eax,ebxjbe @C0001 mov result,1

@C0001:

.data

result DWORD ?

.code

mov ebx,5

mov eax,6

.IF eax > ebx

mov result,1

.ENDIF

Generated code:

MASM automatically generates an unsigned jump (JBE) when both operands are registers . . .

Page 49: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

49

MASM-Generated CodeMASM-Generated Code

mov ebx,5mov eax,6cmp eax,ebxjle @C0001 mov result,1

@C0001:

.data

result SDWORD ?

.code

mov ebx,5

mov eax,6

.IF SDWORD PTR eax > ebx

mov result,1

.ENDIF

Generated code:

. . . unless you prefix one of the register operands with the SDWORD PTR operator. Then a signed jump is generated.

Page 50: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

50

.REPEAT Directive.REPEAT Directive

; Display integers 1 – 10:

mov eax,0.REPEAT

inc eaxcall WriteDeccall Crlf

.UNTIL eax == 10

Executes the loop body before testing the loop condition associated with the .UNTIL directive.

Example:

Page 51: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

51

.WHILE Directive.WHILE Directive

; Display integers 1 – 10:

mov eax,0.WHILE eax < 10

inc eaxcall WriteDeccall Crlf

.ENDW

Tests the loop condition before executing the loop body The .ENDW directive marks the end of the loop.

Example:

Page 52: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

52

MacrosMacros

Introducing Macros Defining Macros Invoking Macros Macro Examples Nested Macros Example Program: Wrappers

Page 53: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

53

Introducing MacrosIntroducing 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.

Page 54: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

54

Defining MacrosDefining 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 [parameter-1, parameter-2,...]

statement-list

ENDM

Page 55: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

55

mNewLine Macro ExamplemNewLine Macro Example

mNewLine MACRO ; define the macrocall Crlf

ENDM.data

.codemNewLine ; invoke the macro

This is how you define and invoke a simple macro.

The assembler will substitute "call crlf" for "mNewLine".

Page 56: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

56

mPutChar MacromPutChar Macro

mPutchar MACRO charpush eaxmov al,charcall WriteCharpop eax

ENDM

Writes a single character to standard output.

Definition:

.codemPutchar 'A'Invocation:

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

Expansion:viewed in the listing file

Page 57: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

57

Invoking MacrosInvoking Macros (1 of 2) (1 of 2)

When you invoke a macro, each argument you pass matches a declared parameter.

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.

Page 58: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

58

Invoking MacrosInvoking Macros (2 of 2) (2 of 2)Relationships between macros, arguments, and parameters:

Page 59: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

59

mWriteStr MacromWriteStr Macro (1 of 2) (1 of 2)

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

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

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

Page 60: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

60

mWriteStr MacromWriteStr Macro (2 of 2) (2 of 2)

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

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

mWriteStr MACRO bufferpush edxmov edx,OFFSET buffercall WriteStringpop edx

ENDM

Page 61: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

61

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

mPutchar MACRO charpush eaxmov al,charcall WriteCharpop eax

ENDM

Page 62: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

62

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

mPutchar MACRO charpush eaxmov al,charcall WriteCharpop eax

ENDM

Page 63: 1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.

63

Your turn . . .Your turn . . .

Write a macro named mMove32 that receives two 32-bit memory operands. The macro should move the source operand to the destination operand.

. . . Solution. . . SolutionmMove32 MACRO destination,source

push eaxmov eax,sourcemov destination,eaxpop eax

ENDM