Chapter 9.2: The emu8086.inc Library
Multiple Choice · Library and INCLUDE
Question 1: The INCLUDE Directive
Which is correct for the line include 'emu8086.inc'?
B: INCLUDE is a directive: the assembler puts the contents of the file at that point as text, and the processor never sees this line (A is wrong). Macros must be defined before they are used; that is why the line is at the start of the source (C is wrong). Macros such as PUTC and PRINT are also defined in the library (D is wrong).
Multiple Choice · Library and INCLUDE
Question 2: About the Library
Which is correct for the emu8086.inc library?
D: PRINT_NUM calls PRINT_NUM_UNS while doing its job; if the second is not defined, the assembler cannot find the label. PRINT, PRINTN, PUTC and GOTOXY are macros and are written directly (A). GOTOXY uses the column, row order (B). The include line is at the start of the source (C).
Output Question · Macros
Question 3: Screen Output with Macros
What is the program's screen output? (b) What if the first two macros were PRINTN 'A' and PRINT 'B'?
include 'emu8086.inc' ORG 100h PRINT 'A' PRINTN 'B' PUTC 'C' RET
Show answer
Two lines on the screen:
AB C
PRINT 'A' writes only A; the cursor stays on the same line. PRINTN 'B' writes B, then moves the cursor to the start of the next line with 13 (CR) and 10 (LF). PUTC 'C' writes C there.
(b) The line break comes after A:
A BC
Multiple Choice · Macros
Question 4: What Does GOTOXY Expand To?
What values does the macro GOTOXY 10, 5 put into registers before calling INT 10h?
C: The macro is written in column, row order; the BIOS cursor-positioning service (AH = 02h) expects the row in DH and the column in DL. A reverses the order; 0Eh in B is the character-printing (teletype) service; D uses the wrong registers. The macro saves AX, BX and DX on the stack and restores them.
Step-by-Step Trace · Macros
Question 5: Does PUTC Destroy a Register?
PUTC 'A' is used with AX = 1234h. How do AX, SP and the stack change while the macro's expansion runs? What is AX after the macro?
Start: in a .COM program SP = FFFEh; the 0000h put by DOS is on top of the stack.
AX = 1234h. The macro must not destroy this value.
PUSH AX: SP = FFFCh, [FFFCh] = 1234h. The macro saves AX first.
MOV AL, 'A': AX = 1241h. The character goes into AL; AH is still the old one.
MOV AH, 0Eh: AX = 0E41h. 0Eh is the BIOS character-printing (teletype) service.
INT 10h: A is printed on the screen, the cursor moves one to the right.
POP AX: AX = 1234h, SP = FFFEh. The calling code does not notice that AX was changed temporarily.
ORG 100h MOV AX, 1234h ; PUTC 'A' expands to: PUSH AX MOV AL, 'A' MOV AH, 0Eh INT 10h POP AX RET
Classic Question · Macros
Question 6: Where Is the Cursor?
The screen is empty and the cursor is at (column 0, row 0). In which (column, row) cell is each character written? Where is the cursor when the program ends?
include 'emu8086.inc' ORG 100h PRINT 'Hello World!' GOTOXY 10, 5 PUTC 65 PUTC 'B' PRINTN '!' PUTC 'C' RET
Show answer
| Macro | Printed | Cell (column, row) |
|---|---|---|
| Hello World! | (0…11, 0) | |
| GOTOXY 10, 5 | — | cursor (10, 5) |
| PUTC 65 | A | (10, 5) |
| PUTC 'B' | B | (11, 5) |
| PRINTN '!' | ! + CR LF | (12, 5), cursor (0, 6) |
| PUTC 'C' | C | (0, 6) |
Final cursor: (1, 6). 65 = 41h = 'A'. GOTOXY only moves the cursor and writes nothing on the screen; CR takes the cursor to the start of the line, LF down one line.
Multiple Choice · Procedures
Question 7: PRINT or PRINT_STRING?
A program will print 10 different strings. If the PRINT macro is used 10 times, or the strings are printed with PRINT_STRING, how many times does the character-printing loop code appear?
A: A macro is expanded at every place it is used: 10 PRINTs mean 10 copies of the string and loop code. A procedure is defined once (DEFINE_PRINT_STRING); for each string only LEA SI and a 3-byte CALL are added. In return, the procedure spends CALL/RET time on every call.
Multiple Choice · Procedures
Question 8: The SCAN_NUM Result
While CALL scan_num runs, the user types -25 and presses Enter. When the procedure returns, where is the result?
D: SCAN_NUM returns the signed number it read in CX. In 16-bit two's complement −25 is 65536 − 25 = 65511 = FFE7h. 8019h would be sign-magnitude representation; the 8086 does not use it. To print the number with PRINT_NUM, MOV AX, CX is needed first.
Output Question · Procedures
Question 9: PRINT_NUM and PRINT_NUM_UNS
With the following values in AX, what do CALL print_num and CALL print_num_uns print?FFF6h · 8000h · 7FFFh · 0064h
Show answer
| AX | print_num | print_num_uns |
|---|---|---|
| FFF6h | -10 | 65526 |
| 8000h | -32768 | 32768 |
| 7FFFh | 32767 | 32767 |
| 0064h | 100 | 100 |
The bits are the same, the interpretation differs. If the most significant bit is 1, signed value = unsigned value − 65536 (FFF6h: 65526 − 65536 = −10). For numbers with MSB = 0 the two procedures print the same thing.
Multiple Choice · Procedures
Question 10: Printing the Buffer
The buffer buf filled by GET_STRING is to be printed on the screen. Which is correct?
B: GET_STRING ends the buffer with 0; PRINT_STRING prints the 0-terminated string at DS:SI. AH = 09h looks for '$' and dumps the bytes in memory to the screen until it finds one. PRINT only embeds the constant text written in the source into the code. PTHIS expects the string right after the CALL; it takes the address from the return address on the stack.
Classic Question · Procedures
Question 11: GET_STRING Buffer Size
The user types Mehmetcan and presses Enter. (a) How many characters are taken into the buffer, and what are the bytes? (b) What would happen with the same buffer if MOV DX, 10 were given?
LEA DI, buf MOV DX, 6 CALL get_string ... buf DB 6 DUP(0) count DW 0
Show answer
(a) At most DX − 1 = 5 characters: 'M', 'e', 'h', 'm', 'e', 0. When the buffer is full, new letters are ignored; the procedure waits for Enter or backspace.
(b) The procedure takes up to 9 characters: the 6 bytes of the buffer are not enough, and the bytes of the variable count after it and beyond are overwritten. DX must not be larger than the real size of the buffer.
Step-by-Step Trace · Example Program
Question 12: How Does PTHIS Return?
The equivalent of PTHIS with the register-saving parts removed. What address does CALL push, how does the procedure use it, and where does RET return to?
CALL pthis is at 0100h (3 bytes). Right after it, at 0103h, come the bytes of the string.
CALL pushes the address of the "next instruction": [FFFCh] = 0103h. This address is actually the first byte of the string.
POP SI: the return address was taken into SI (SI = 0103h); the stack is back at FFFEh.
The bytes are printed up to the 0: H, i. SI increases by one after each read.
AL = 0 was read: SI = 0106h, one past the 0 byte. JE jumps to done.
PUSH SI: 0106h is put on the stack as the new "return address".
RET took 0106h into IP: the string was skipped, MOV BX, 1 ran. The data was not taken for an instruction and executed.
ORG 100h CALL pthis DB 'Hi', 0 MOV BX, 1 RET pthis PROC POP SI next: MOV AL, [SI] INC SI CMP AL, 0 JE done MOV AH, 0Eh INT 10h JMP next done: PUSH SI RET pthis ENDP
Multiple Choice · Example Program
Question 13: DEFINE_ in the Wrong Place
What happens if the DEFINE_PTHIS line is written right below ORG 100h?
C: DEFINE_PTHIS expands the procedure's code where it stands, but every DEFINE_ macro in emu8086.inc starts with JMP skip_proc_…: the processor jumps over the procedure body and continues with the main program. Without this guard, the situation in B would occur. Still, the usual place is between RET and END; at the start of the program every definition means an extra JMP.
Spot the Bug · Example Program
Question 14: Three Bugs
The program that reads a number from the user and prints it back has three bugs. Find and fix them.
include 'emu8086.inc' ORG 100h DEFINE_PRINT_STRING LEA SI, msg CALL print_string CALL scan_num MOV AX, CX CALL print_num RET msg DB 'Number: ' DEFINE_SCAN_NUM DEFINE_PRINT_NUM END
Show answer
- No new line: SCAN_NUM echoes Enter only as CR, so the result overwrites the prompt (
42mber: 42). Fix:PUTC 13,PUTC 10. - msg has no 0 at the end: PRINT_STRING also prints the bytes after it.
- DEFINE_PRINT_NUM_UNS is missing: print_num calls it, so the label is undefined.
- DEFINE_PRINT_STRING at the start is not a bug: the macro jumps over its body.
CALL scan_num PUTC 13 PUTC 10 ... msg DB 'Number: ', 0 ... DEFINE_PRINT_NUM_UNS END
Coding Question · Example Program
Question 15: Printing Two Digits
AX holds a number between 0 and 99. Write code that prints the number as two decimal digits (7 → 07, 42 → 42) on the screen with the PUTC macro.
Show answer
MOV BL, 10 DIV BL ; AL = tens, AH = ones ADD AX, 3030h ; two ASCII digits MOV BX, AX PUTC BL ; tens digit PUTC BH ; ones digit
Since AX ≤ 99, AH = 0 and the quotient fits in AL. 42 ÷ 10 → AL = 4, AH = 2; adding 3030h gives '4', '2'. Moving the result to BX is for readability: the PUTC macro preserves AX with PUSH/POP, so PUTC AL and PUTC AH would give the same result.