Chapter 9.1: Macros

Chapter 9.1: Macros · Study Questions
Definition and expansionParameter pitfallsMacro and procedureSize calculationLOCALPUTCPOWERPRINT expansionemu8086.inc
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · The Macro Concept

Question 1: Defining a Macro

Which of the following is correct for macros?

B: To expand a macro call, the assembler must have seen the definition beforehand. A macro is expanded as text at assembly time, no CALL is used; a macro that is never used adds not a single byte to the code; every use produces a separate copy. A single copy is a property of the procedure.

Output Question · The Macro Concept

Question 2: Macro Expansion

What is AX when the program ends? How many machine instructions does the assembler generate from this source?

ADD2 MACRO a, b
    MOV AX, a
    ADD AX, b
ENDM

ORG 100h
ADD2 5, 7
ADD2 AX, 3
RET
Show answer

AX = 000Fh (000Ch before); 5 instructions.

MOV AX, 5    ; ADD2 5, 7
ADD AX, 7    ; AX = 000Ch
MOV AX, AX   ; ADD2 AX, 3
ADD AX, 3    ; AX = 000Fh
RET

Each call is replaced by a copy of the body, and the parameters are substituted as text. In the second call a = AX, so MOV AX, AX is generated: harmless but unnecessary. A macro contains no CALL/RET, so the stack is not used.

Output Question · The Macro Concept

Question 3: Is the Expanded Code Valid?

What are AX and BX after the first call? Does the second call assemble?

SWAP2 MACRO a, b
    PUSH a
    PUSH b
    POP  a
    POP  b
ENDM

ORG 100h
MOV AX, 1111h
MOV BX, 2222h
SWAP2 AX, BX
SWAP2 AX, 5
RET
Show answer

The first call becomes PUSH AX / PUSH BX / POP AX / POP BX: since the last in is the first out, AX = 2222h, BX = 1111h (a swap).

The second call becomes PUSH AX / PUSH 5 / POP AX / POP 5 and does not assemble: pushing an immediate value (PUSH 5) arrived with the 80186 and does not exist on the 8086 (even though emu8086 accepts it); POP 5 makes sense on no processor (a constant cannot be written to).

The macro definition is error-free; the assembler only substitutes text, and the error shows up in the expanded lines.

Step-by-Step Trace · The Macro Concept

Question 4: Parameter Pitfall

The macro SUM2 MACRO a, b · MOV AX, a · ADD AX, b · MOV BX, AX · ENDM is called as SUM2 CX, AX with AX = 3, CX = 4. What is BX after the expanded code runs?

AX0003hBX0000hCX0000h

AX = 3.

AX0003hBX0000hCX0004h

CX = 4. The programmer expects BX = 4 + 3 = 7 from SUM2 CX, AX.

AX0004hBX0000hCX0004h

Line 1 of the macro MOV AX, a → MOV AX, CX: AX = 0004h. The old AX (3) is gone.

AX0008hBX0000hCX0004h

Line 2 ADD AX, b → ADD AX, AX: parameter b is the text "AX", and AX is now 4 → AX = 0008h.

AX0008hBX0008hCX0004h

BX = 0008h, not the expected 7. A parameter is text, not a value; its value is read on the line where it is used. A register that the macro changes (AX) must not be passed as a parameter.

ORG 100h
MOV AX, 3
MOV CX, 4
MOV AX, CX     ; SUM2 CX, AX
ADD AX, AX
MOV BX, AX
RET

Multiple Choice · Macro and Procedure

Question 5: Macro or Procedure?

Which of the following is correct about macros and procedures?

C: A macro is expanded as text at assembly time: the code grows, but with no CALL/RET and no stack access it is faster. A procedure is a single copy in memory; every call goes to it with CALL and returns with RET. If a macro with a label is used twice, the label is defined twice; the LOCAL directive prevents this.

Multiple Choice · Macro and Procedure

Question 6: Size at 25 Uses

Every copy of the PUTC macro is 8 bytes. The same job could be done by a procedure whose body is 8 bytes + RET (1 byte), with a 3-byte near CALL at every use. What are the code sizes at 25 uses?

A: Macro: 25 × 8 = 200 bytes. Procedure: the body once (8 + 1 = 9 bytes) plus 25 × 3 = 75 bytes of CALL → 84 bytes. 75 bytes counts only the CALLs and forgets the body; 225 bytes counts the macro copy as 9 bytes (a macro has no RET); 209 bytes counts the body again at every call.

Classic Question · Macro and Procedure

Question 7: Break-Even Point

A job whose body is n bytes is used in k places. The macro takes k · n bytes, the procedure (n + 1) + 3k bytes.

  1. From how many uses on is the procedure smaller for n = 8?
  2. What is the situation for n = 3?
  3. If each CALL + RET pair takes about 27 clock cycles, how many ms of extra time do 10,000 calls add on a 4.77 MHz 8086?
Show answer
  1. 8k > 9 + 3k → 5k > 9 → k ≥ 2. k = 1: 8 and 12 bytes (macro smaller); k = 2: 16 and 15 bytes (procedure smaller).
  2. 3k versus 4 + 3k: the macro is always smaller. If the body is as short as a CALL, the procedure only adds the RET and the call cost.
  3. 10,000 × 27 = 270,000 cycles; 270,000 ÷ 4,770,000 Hz ≈ 56.6 ms. A macro has no such cost.

Size grows with the number of uses, the speed difference with the number of calls: short, frequently executed code suits a macro, long code used in many places suits a procedure.

Multiple Choice · Macro and Procedure

Question 8: Which One Should Be a Macro?

In which of the following situations is choosing a macro most appropriate?

D: For a 2-instruction job the CALL + RET cost is bigger than the job itself; a macro stays both fast and short. A 300-byte sort takes 900 bytes as a macro, 301 + 3 × 3 = 310 bytes as a procedure; if size is critical, a procedure (609 bytes) is chosen over 200 copies (1600 bytes). The expansion of a macro that calls itself never ends without conditional assembly.

Multiple Choice · Labels and LOCAL

Question 9: The Same Label Twice

What happens when the following program is assembled?

WAIT_LOOP MACRO
first: DEC CX
     JNZ first
ENDM

MOV CX, 5
WAIT_LOOP
MOV CX, 9
WAIT_LOOP

B: After expansion the code has two first: lines; the assembler cannot tell which one to jump to and reports an error. The fix is to write LOCAL first on the first line of the macro: at every expansion the label is replaced by a unique name (??0000, ??0001 … in MASM).

Step-by-Step Trace · Labels and LOCAL

Question 10: Two Copies with LOCAL

The macro MAX2 MACRO a, b · LOCAL done · MOV AX, a · CMP AX, b · JGE done · MOV AX, b · done: · ENDM is used twice. What are AX and DX? What would the first call give if JAE were written instead of JGE?

AX0000hBXFFFEhDX0000hSF0OF0CF0

BX = −2 = FFFEh (two's complement).

AXFFFEhBXFFFEhDX0000hSF0OF0CF0

First expansion: AX ← BX = FFFEh.

AXFFFEhBXFFFEhDX0000hSF1OF0CF0

CMP AX, 1: −2 − 1 = −3 → SF = 1, OF = 0. In the unsigned view FFFEh > 1, so there is no borrow: CF = 0.

AXFFFEhBXFFFEhDX0000hSF1OF0CF0

JGE (wants SF = OF): SF ≠ OF → −2 ≥ 1 is false, no jump.

AX0001hBXFFFEhDX0000hSF1OF0CF0

AX ← 1: the larger one is b. Thanks to LOCAL the label became ??0000 in this copy.

AX0001hBXFFFEhDX0001hSF1OF0CF0

DX = 0001h: a copy of the first result.

AX0001hBXFFFEhDX0001hSF1OF0CF0

Second expansion MAX2 AX, -5: MOV AX, AX has no effect.

AX0001hBXFFFEhDX0001hSF0OF0CF1

CMP AX, −5: 1 − (−5) = 6 → SF = 0, OF = 0 (in the unsigned view 1 < FFFBh, so CF = 1).

AX0001hBXFFFEhDX0001hSF0OF0CF1

JGE: SF = OF → 1 ≥ −5, jumps to ??0001. Since this copy's label is different, there is no clash. Result: AX = DX = 0001h.

AXFFFEhBXFFFEhDX0000hSF1OF0CF0

With JAE: JAE compares unsigned, and since CF = 0 the first call would jump and AX = FFFEh (65,534) would remain. For signed magnitude JGE/JG is used.

ORG 100h
MOV BX, -2
MOV AX, BX     ; MAX2 BX, 1
CMP AX, 1
JGE ??0000
MOV AX, 1
??0000:
MOV DX, AX
MOV AX, AX     ; MAX2 AX, -5
CMP AX, -5
JGE ??0001
MOV AX, -5
??0001:
RET

Output Question · The PUTC Macro

Question 11: An Invisible Side Effect

What appears on the screen? Which two lines added to the macro make -5 appear on the screen?

PUTC2 MACRO c        ; no PUSH/POP
    MOV AL, c
    MOV AH, 0Eh
    INT 10h
ENDM

ORG 100h
MOV AL, '5'
PUTC2 '-'
MOV AH, 0Eh
INT 10h          ; print '5'
RET
Show answer

Screen: --

Since the macro writes '-' to AL, the caller's '5' in AL is lost; the next INT 10h prints '-' again.

PUSH AX is added at the start of the body and POP AX at the end (as in the PUTC macro). AX is preserved and -5 appears on the screen.

Multiple Choice · The PUTC Macro

Question 12: If RET Is Forgotten

What happens if the RET on the last line is deleted from the program ORG 100h · PUTC 'A' · PUTC 'B' · RET?

C: The processor does not tell code from data: after the last instruction it decodes and executes whatever is in memory, and the system may hang. Macro expansion only copies the body; POP AX only restores AX; INT 10h returns to the program with IRET.

Step-by-Step Trace · The POWER Macro

Question 13: POWER 3, 4

The expansion of the POWER macro runs with base = 3, exponent = 4. What are AX, CX, DX at each step? What is result?

AX0001hBX0000hCX0000hDX0000h

Temporary result AX = 1.

AX0001hBX0000hCX0004hDX0000h

CX ← exponent = 4 (loop count).

AX0001hBX0003hCX0004hDX0000h

BX ← base = 3.

AX0003hBX0003hCX0004hDX0000h

Round 1: DX:AX = 1 × 3 = 0003h. Upper half DX = 0 → CF = OF = 0.

AX0003hBX0003hCX0003hDX0000h

LOOP: CX = 3 ≠ 0 → powerloop.

AX0009hBX0003hCX0003hDX0000h

Round 2: AX = 3 × 3 = 0009h.

AX0009hBX0003hCX0002hDX0000h

CX = 2.

AX001BhBX0003hCX0002hDX0000h

Round 3: AX = 9 × 3 = 001Bh (27).

AX001BhBX0003hCX0001hDX0000h

CX = 1.

AX0051hBX0003hCX0001hDX0000h

Round 4: AX = 27 × 3 = 0051h (81).

AX0051hBX0003hCX0000hDX0000h

CX = 0 → the loop is exited.

AX0051hBX0003hCX0000hDX0000h

result (0117h) ← 0051h = 34 = 81. MUL wrote DX in every round as well; the macro changes AX, BX, CX and DX.

ORG 100h
mov ax, 1        ; POWER base, exponent, result
mov cx, exponent
mov bx, base
powerloop:
mul bx           ; DX:AX = AX * BX
loop powerloop
mov result, ax
ret
base     dw 3
exponent dw 4
result   dw ?

Output Question · The POWER Macro

Question 14: When the Exponent Is Zero

The first version of the POWER macro (without a JCXZ check) is called with exponent = 0.

  1. How many times does MUL run?
  2. What is result if base = 2?
  3. What is result if base = 1? Does this result show the macro is correct?
Show answer
  1. LOOP decrements CX first: 0 → FFFFh ≠ 0. The loop runs 65,536 times.
  2. After 216 the low 16 bits of AX are zero and stay so: result = 0000h (20 = 1 was expected).
  3. result = 0001h: however many times 1 is multiplied, it is 1. The result is correct by coincidence; the bug is still there (65,536 needless MULs).

Fix: MOV CX, e followed by JCXZ p_end; if e = 0, AX = 1 is written directly.

Multiple Choice · The POWER Macro

Question 15: Nested Expansion

How many machine instructions and how many bytes does the call PRINT2 'O', 'K' of the macro PRINT2 MACRO c1, c2, which calls the PUTC macro twice, produce?

A: PRINT2 is expanded first: two PUTC lines. Each PUTC is in turn expanded to 5 instructions (PUSH AX, MOV AL, MOV AH, INT 10h, POP AX) and 1 + 2 + 2 + 2 + 1 = 8 bytes. 2 instructions/6 bytes would correspond to two CALLs, 5 instructions/8 bytes to a single PUTC.

Step-by-Step Trace · Macro Libraries

Question 16: Expansion of PRINT 'Hi'

The expansion of the macro PRINT 'Hi' (starting with AX = 1234h). Where does the string sit, how does SI advance, how does the loop end?

AX1234hSI0000hSPFFFEhZF0String inside the code (CS:0104h)'H'0104'i'010500h0106

The string was embedded in the code, at the place where the macro was expanded; a 0 was added at the end.

AX1234hSI0000hSPFFFAhZF0String inside the code (CS:0104h)'H'0104'i'010500h0106

AX and SI were saved on the stack. JMP jumps over the bytes of the string; otherwise 48h, 69h would be taken for instructions.

AX1234hSI0104hSPFFFAhZF0String inside the code (CS:0104h)'H'0104'i'010500h0106↑ SI

SI = 0104h: the first byte of the string.

AX0E48hSI0105hSPFFFAhZF0String inside the code (CS:0104h)'H'0104'i'010500h0106↑ SI

AL = 'H' ≠ 0 (ZF = 0): SI advances, H is printed.

AX0E69hSI0106hSPFFFAhZF0String inside the code (CS:0104h)'H'0104'i'010500h0106↑ SI

AL = 'i' ≠ 0: SI = 0106h, i is printed.

AX0E00hSI0106hSPFFFAhZF1String inside the code (CS:0104h)'H'0104'i'010500h0106↑ SI

AL = 0 → ZF = 1, JZ jumps to done. The 0 at the end marks the end of the string.

AX1234hSI0000hSPFFFEhZF1String inside the code (CS:0104h)'H'0104'i'010500h0106

POP SI and POP AX: both registers returned to their old values (AX = 1234h, SI = 0000h). Because the data is in the code segment, CS:[SI] was used.

ORG 100h
; PRINT 'Hi' expands to:
PUSH AX
PUSH SI
JMP skip
s DB 'Hi', 0
skip:
LEA SI, s
next:
MOV AL, CS:[SI]
CMP AL, 0
JZ done
INC SI
MOV AH, 0Eh
INT 10h
JMP next
done:
POP SI
POP AX
RET

Multiple Choice · Macro Libraries

Question 17: Library and Code Size

A library with dozens of macros was added with include 'emu8086.inc'; the program uses only GOTOXY and PUTC. Which is correct?

D: A macro that is not used produces no code; adding a large library does not enlarge the executable. INCLUDE inserts the text of the file into the source at assembly time; it is used in .COM programs as well. The procedures in the library are defined separately with the DEFINE_… macros.

Coding Question · The POWER Macro

Question 18: A Shift-Based Multiply Macro

Write a macro MUL2N dest, n that multiplies a register or word variable by 2n without MUL. It must use no register other than CX, and the value of CX must be preserved. What does MUL2N num, 3 give for num dw 5? What happens in the call MUL2N CX, 2?

Show answer
MUL2N MACRO dest, n
    PUSH CX
    MOV  CL, n     ; 8086: the count goes in CL
    SHL  dest, CL
    POP  CX
ENDM

There is no label, so LOCAL is not needed. On the 8086 a shift count greater than 1 can be given only in CL; SHL dest, n is an 80186 instruction.

MUL2N num, 3: 5 × 8 = 40 (0028h).

MUL2N CX, 2: if CX = 7, MOV CL, 2 makes CX 0002h, SHL makes the result 0008h, and POP CX restores the old 7: CX does not change. A register that the macro preserves must not be passed as a parameter.

The End

Chapter 9.1: Macros: Study Questions
Lecture slides: Chapter 9.1: Macros · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.