Chapter 4.2: Variables
Multiple Choice · Variables
Question 1: Valid Definition
Which of the following data definitions assembles without error?
D: ? reserves an uninitialized word (emu8086 puts 00 00 in memory). A: a name must start with a letter. B: a register name cannot be a variable name. C: 300 does not fit in a byte (0 … 255 or −128 … 127); DW is needed.
Output Question · Variables
Question 2: DB, DW and DD
The definitions are laid out one after another. a) How many bytes is each, and what is its offset relative to A? b) Which bytes do X and Y occupy in memory? c) Why is MOV AL, X wrong?
A DB 41h S DB 'Hi$' X DW 1234h Y DD 12345678h Z DW ? T DB 3 DUP(0)
Show answer
- a) A: 1 byte (+0) · S: 3 bytes, 'H' 'i' '$' (+1) · X: 2 (+4) · Y: 4 (+6) · Z: 2 (+10) · T: 3 (+12) → total 15 bytes
- b) Little-endian: X → 34 12, Y → 78 56 34 12
- c) X is a word, AL is a byte: the sizes do not match.
MOV AL, BYTE PTR X→ AL = 34h (low byte);MOV AX, X→ AX = 1234h.
DB = 1, DW = 2, DD = 4 bytes; every character inside the quotes is one byte.
Multiple Choice · Memory Layout
Question 3: How Many Bytes?
How many bytes in total do the following definitions occupy in memory?
a DB 5 b DW 3 DUP(0) c DB 'ABC', 0 d DD 12345678h
C: a: 1 byte · b: 3 words = 6 bytes · c: 'A', 'B', 'C' and 0 = 4 bytes · d: double word = 4 bytes → 1 + 6 + 4 + 4 = 15. 11 counts DUP only once; 19 counts DD as 8 bytes.
Step-by-Step Trace · Memory Layout
Question 4: Variable Addresses
Initially the registers are 0. Just before RET, what are AX, DX and CL? At which offsets are the variables placed?
The code starts at 0100h; the instructions are 3+3+3+3+2+4+1 = 19 (13h) bytes (emu8086 encodes LEA SI, w as the 3-byte MOV SI, OFFSET w) → a = 0113h, arr = 0114h, w = 0117h. AL ← [0113h] = 07h (a is a byte variable).
OFFSET arr = 0114h: the address is a constant known at assembly time. BX now points to arr.
EA = 0114h + 2 = 0116h → arr[2] = 30h → AH = 30h, AX = 3007h.
LEA SI, w → SI = 0117h (the address, not the contents).
DX ← the word at [0117h]: low byte CDh, high byte ABh → DX = ABCDh.
BYTE PTR w reads only [0117h] → CL = CDh (low byte).
ORG 100h MOV AL, a MOV BX, OFFSET arr MOV AH, [BX+2] LEA SI, w MOV DX, [SI] MOV CL, BYTE PTR w RET a DB 7 arr DB 10h, 20h, 30h w DW 0ABCDh
Multiple Choice · Memory Layout
Question 5: ORG 100h
At the start of a .COM program, the line ORG 100h — which statement is correct?
A: ORG is a directive, not translated to machine code; variable addresses are computed accordingly (MOV AL, var1 → MOV AL, [0108h]). The first 256 bytes of the segment (the PSP) belong to DOS. In .COM, CS = DS = ES = SS and SP = FFFEh at the start; the physical address depends on the segment DOS chooses.
Output Question · Arrays
Question 6: Index in a String
After the code runs, what are AL, AH, BL and the string s? ('c' = 63h, 'e' = 65h)
ORG 100h MOV SI, 3 MOV AL, s[SI] MOV BL, s[1] INC SI MOV s[SI], '!' MOV AH, s+6 RET s DB 'Welcome', 0
Show answer
- The index counts bytes and starts at 0: s[0] = 'W', s[1] = 'e', s[2] = 'l', s[3] = 'c' …
- AL ← s[3] = 63h ('c'); BL ← s[1] = 65h ('e')
- SI = 4: s[4] ← '!' (the size is known because s is defined with DB) → s = "Welc!me", 0
- AH ← s+6 = 65h ('e') → AX = 6563h
s[SI] = [s + SI] = [SI + s]: the same address, different notation.
Classic Question · Arrays
Question 7: DUP and Word Array
a) c DB 3 DUP(1, 2), 0 — which bytes does it create in memory?
b) t DW 10 DUP(?) — how many bytes does it occupy? What is the offset of the 7th element (index 6) relative to t?
c) MOV AX, t[3] — which element does it read?
Show answer
- a) 01 02 01 02 01 02 00 (7 bytes): DUP repeats the list in parentheses 3 times.
- b) 10 words = 20 bytes; the offset of element i is 2 × i → 6 × 2 = 12 = 0Ch.
- c) It reads none of them exactly: t[3] combines the high byte of the 2nd element (t+2, t+3) with the low byte of the 3rd element (t+4). In a word array the index must always be even.
Multiple Choice · Addresses
Question 8: LEA and OFFSET
LEA BX, arr and MOV BX, OFFSET arr — which statement is correct?
B: For a simple variable the two give the same result. The difference: OFFSET is a constant computed at assembly time; LEA can compute the address at run time: LEA SI, [BX+DI+4], LEA BX, arr[SI]. Neither LEA nor MOV changes the flags.
Step-by-Step Trace · Addresses
Question 9: Getting an Address with LEA
In a .COM program that starts with ORG 100h, what are AX, BX and num3 after the code runs? What does LEA load?
SI ← address of num1 (0112h): the code is 18 bytes, because emu8086 encodes LEA, whose address is constant, as the 3-byte MOV SI, OFFSET num1. LEA does not access memory; it only computes the address.
AX ← [SI] = 0005h (register indirect addressing).
SI ← address of num2 (0114h): 2 bytes further on.
BX ← [SI] = 000Ah (10 is decimal).
AX = 0005h + 000Ah = 000Fh.
SI ← address of num3 (0116h).
[SI] ← AX: num3 = 000Fh (0F 00 in memory).
lea si, num1 mov ax, [si] ; 1st number lea si, num2 mov bx, [si] ; 2nd number add ax, bx lea si, num3 mov [si], ax ; write to num3 ret num1 dw 5 num2 dw 10 num3 dw 0
Multiple Choice · Addresses
Question 10: Value or Address?
VAR1 is defined at relative location 0108h as VAR1 DW 1234h. After MOV BX, VAR1 and MOV CX, OFFSET VAR1 run, what are BX and CX?
D: In MASM/emu8086 a variable name represents the value in memory: MOV BX, VAR1 = MOV BX, [0108h] → 1234h. OFFSET, on the other hand, gives the address itself (0108h) as a constant; LEA CX, VAR1 would give the same result too.
Multiple Choice · Constants
Question 11: EQU Constant
k EQU 5 and ROWLEN EQU 80 are defined. Which statement is correct?
A: EQU constants exist only at assembly time: the assembler replaces the name with its value at every use and evaluates the expression: 80 × 2 + 5 = 165 (an immediate value). They take no space in memory and cannot be changed (MOV k, 7 → the destination cannot be a constant); MOV AL, k loads the immediate 5.
Output Question · Sample Programs
Question 12: Adding with Variables
What values does AX take, in order? What does RESULT contain (byte by byte)? Why is JMP START needed? (b) What if VALUE1 = 1225H and VALUE2 = 2133H?
JMP START VALUE1 DW 125H VALUE2 DW 133H RESULT DW ? START: MOV AX, VALUE1 ADD AX, VALUE2 MOV RESULT, AX
Show answer
- AX: 0125h → 0258h (5 + 3 = 8, 2 + 3 = 5, 1 + 1 = 2; no carry)
- RESULT = 0258h → in memory 58h, 02h. MOV RESULT, AX does not change AX.
- The data sits between the code: without the JMP the CPU would take the bytes 25 01 33 01 … for instructions and execute them.
- (b) AX: 1225h → 3358h
Multiple Choice · Sample Programs
Question 13: Decimal or Hexadecimal?
What is AX after the code runs?
MOV AX, NUM1 MOV BX, NUM2 ADD AX, BX RET NUM1 DW 5H NUM2 DW 10H
C: 10H is hexadecimal (16): 5h + 10h = 15h. 000Fh counts 10 as decimal (5 + 10 = 15 = 0Fh); 0010h is the value of BX; 0105h writes the bytes side by side. Without a suffix a number is decimal: DW 10 → 000Ah.
Step-by-Step Trace · Sample Programs
Question 14: Data After RET
After the code runs, what are AX, BX and num4?
AX ← num1 = 0005h. A variable name represents its value.
BX ← num2 = 000Ah: 10 is decimal (no suffix).
AX = 5 + 10 = 000Fh.
BX ← num3 = 15 = 000Fh.
AX = 000Fh + 000Fh = 001Eh (30).
num4 ← AX → in memory 1E 00. Then RET: the data lines below are never executed, so no JMP is needed.
mov ax, num1 ; 1st number mov bx, num2 ; 2nd number add ax, bx mov bx, num3 ; 3rd number add ax, bx mov num4, ax ; store the sum ret num1 dw 5 num2 dw 10 num3 dw 15 num4 dw 0
Output Question · Sample Programs
Question 15: Signed or Unsigned?
The program writes the larger value into max_value. What is max_value? What if JGE were replaced by JAE?
ORG 100h MOV AX, value1 CMP AX, value2 JGE bigger MOV AX, value2 bigger: MOV max_value, AX RET value1 DW -3 value2 DW 2 max_value DW ?
Show answer
- value1 = −3 sits in memory as FFFDh.
- CMP AX, value2: FFFDh − 0002h is computed, the result is discarded, and the flags are set.
JGEcompares signed: −3 ≥ 2 is false → no jump, AX ← value2 → max_value = 0002h.JAEcompares unsigned: 65533 ≥ 2 is true → jump → max_value = FFFDh.
Memory holds only a bit pattern; the instruction that uses it decides whether it is signed or unsigned.
Multiple Choice · Sample Programs
Question 16: Double Word
What are DX:AX after the code runs?
MOV AX, WORD PTR data MOV DX, WORD PTR [data+2] ADD AX, 1 ADC DX, 0 RET data DD 0001FFFFh
B: The bytes of data are FF FF 01 00: AX = FFFFh (low word), DX = 0001h. ADD AX, 1 → AX = 0000h, CF = 1. ADC DX, 0 adds the carry to the high word: DX = 0002h. DX:AX = 00020000h = 0001FFFFh + 1. A forgets the carry (ADD DX, 0).
Spot the Bug · Sample Programs
Question 17: Faulty Definition and Access
There are four errors in this program. What are the causes and how are they fixed?
ORG 100h MOV AL, v2 MOV v1, v2 MOV k, 7 RET v1 DW 1 v2 DW 2 s DW 'Welcome' k EQU 5
Show answer
MOV AL, v2: v2 is a word, AL is a byte →MOV AX, v2(orMOV AL, BYTE PTR v2)MOV v1, v2: two memory operands →MOV AX, v2·MOV v1, AXMOV k, 7: k is an EQU constant; it has no place in memory and cannot be changed (the assembler seesMOV 5, 7)s DW 'Welcome': a text string cannot be defined with DW →s DB 'Welcome'