Chapter 4.1: Memory Access

Chapter 4.1: Memory Access · Study Questions
Addressing modesEffective addressInvalid operandsDefault segmentAddress linesPhysical addressbyte ptrLittle-endianMOV rulesVideo memoryArray access
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Addressing Modes

Question 1: MOV AX, [BX]

MOV AX, [BX] uses which addressing mode?

A: BX holds not data but an offset (address); the processor loads the word at DS:BX into AX. For comparison: direct MOV AX, [1234h], register MOV AX, BX, based + displacement MOV AX, [BX+4], immediate MOV AX, 1234h.

Multiple Choice · Addressing Modes

Question 2: Default Segment

DS = 2000h, SS = 3000h, BX = 1000h, BP = 0100h, SI = 0020h. From which physical address does MOV AX, [BP+SI+4] read?

D: EA = BP + SI + 4 = 0124h. Because BP is in the address, the default segment is SS: 30000h + 0124h = 30124h. 20124h assumes the segment is DS; 21024h and 31024h take BX instead of BP. On the 8086 the base registers are BX and BP, the index registers are SI and DI; [SP] cannot be used in addressing.

Step-by-Step Trace · Addressing Modes

Question 3: Three Questions with Shared Data

1) What is the physical address of the next instruction? 2) After MOV AX, CX runs, what is AX? 3) After MOV AX, [BX] runs, what is AX? ([20151h] = 00h)

AX5000AHALBX0150BHBLCX5000CHCLDS2000memory (physical address)201500820151000015080

1) The next instruction is at CS:IP: 34000h + 4050h = 38050h. 2) Register-to-register copy: AX = 5000h; CX and the flags do not change.

AX0008AHALBX0150BHBLCX5000CHCLDS2000memory (physical address)2015008201510000150802000h × 10h + 0150h = 20150h

3) EA = BX = 0150h, segment DS → 20150h. A word is read: AL ← 08h, AH ← [20151h] = 00h → AX = 0008h. [00150h] = 80h is a distractor (assuming DS = 0).

; CS=3400h IP=4050h DS=2000h
; BX=0150h CX=5000h
; [00150h]=80h [20150h]=08h
MOV AX, CX     ; 2nd question
MOV AX, [BX]   ; 3rd question

Multiple Choice · Addressing Modes

Question 4: MOV CL, [BX]

BX = 0050h, DS = 2000h, CX = 5000h, [00500h] = 80h, [20050h] = 08h. What is CX after MOV CL, [BX] runs?

C: The physical address is DS × 10h + BX = 20050h → CL ← 08h. Since the destination is 8 bits, a single byte is read; CH (50h) does not change → 5008h. 5080h comes from thinking the address is BX × 10h; 0008h comes from thinking CH is cleared; 0050h comes from taking the address for data.

Output Question · Addressing Modes

Question 5: Base + Displacement

What do AX and BX become when the code runs? (b) What if the data were 8, 10, 12? (c) What if the data were DW 5, 10, 15, 0 and, before RET, MOV [SI+6], AX were added?

MOV SI, OFFSET NUM1
MOV AX, [SI]
MOV BX, [SI+2]
ADD AX, BX
MOV BX, [SI+4]
ADD AX, BX
RET
NUM1 DW 7
      DW 9
      DW 11
Show answer
  • AX = 001Bh (7 + 9 + 11 = 27), BX = 000Bh
  • DW lines without a label are placed right after the previous one: 7 → NUM1, 9 → NUM1+2, 11 → NUM1+4. Since words are 2 bytes, the displacement grows by 2 each time.
  • (b) AX = 001Eh, BX = 000Ch
  • (c) AX = 001Eh, BX = 000Fh, the fourth word [SI+6] = 001Eh

Multiple Choice · Addressing Modes

Question 6: Name of the Addressing Mode

MOV AX, [BX+DI+8] — which addressing mode does the source operand of this instruction use?

D: The effective address is the sum of the base register (BX), the index register (DI) and the constant displacement (8). The based mode has only BX/BP + displacement ([BX+4]), the indexed mode only SI/DI + displacement ([DI+8]); register indirect has a single register ([BX]). When a base and an index register are together the mode is based indexed; when a constant is added too it becomes based indexed with displacement.

Classic Question · Addressing Modes

Question 7: Effective and Physical Address

DS = 2100h, SS = 3000h, ES = 5000h, BX = 0150h, BP = 0A00h, SI = 0034h, DI = 0010h. Find the effective address (EA) and the physical address of each memory operand.

  1. [BX+SI+12h]
  2. [BP+DI-4]
  3. [DI+0FFF8h]
  4. ES:[BX]
  5. DS:[BP+SI]
Show answer
EASegmentPA
10150 + 0034 + 12 = 0196hDS21196h
20A00 + 0010 − 4 = 0A0ChSS30A0Ch
30010 + FFF8 = 1 0008 → 0008hDS21008h
40150hES50150h
50A00 + 0034 = 0A34hDS21A34h

An address containing BP uses SS by default (2); a prefix changes this (5). EA wraps around in 16 bits (3): FFF8h behaves like −8.

Spot the Bug · Addressing Modes

Question 8: Invalid Operands

Explain the error in each of the following lines and write valid code that does the same job.

MOV [SI], [DI]
MOV AX, [SI+DI]
MOV [BX], 5
MOV ES, DS
MOV AL, BX
Show answer
FaultyReasonFix
MOV [SI], [DI]no memory-to-memory MOVMOV AL, [DI]
MOV [SI], AL
MOV AX, [SI+DI]two index registers cannot be combinedMOV BX, SI
MOV AX, [BX+DI]
MOV [BX], 5size is ambiguousMOV BYTE PTR [BX], 5
MOV ES, DSno segment-to-segment MOVPUSH DS
POP ES
MOV AL, BX8 and 16 bit do not matchMOV AL, BL

For a byte string, MOVSB can also be used instead of the first line (ES:DI ← DS:SI; the direction is reversed).

Step-by-Step Trace · Addressing Modes

Question 9: Modes Together

Starting at DS:0300h, memory holds the bytes 11 22 33 44 55 66 77. What are the addressing mode and effective address of each instruction? What are AX and memory at the end?

030011030122030233030344030455030566030677AX0000hBX0300hSI0000hDI0000h

Immediate mode: BX = 0300h. The constant is inside the instruction; no memory access.

030011030122030233030344030455030566030677AX0000hBX0300hSI0003hDI0000h

SI = 0003h.

030011030122030233030344030455030566030677AX0022hBX0300hSI0003hDI0000hEA=0301

Direct mode: EA = 0301h → AL = 22h.

030011030122030233030344030455030566030677AX4422hBX0300hSI0003hDI0000hEA=0303

Based indexed: EA = BX + SI = 0303h → AH = 44h; AX = 4422h.

030011030122030233030344030455030566030677AX4477hBX0300hSI0003hDI0000hEA=0304

Based indexed with displacement: EA = 0300h + 3 + 1 = 0304h → AL = 22h + 55h = 77h.

030011030122030233030344030455030566030677AX4477hBX0300hSI0003hDI0004h

DI = 0004h.

030011030122030233030344030455030577030644AX4477hBX0300hSI0003hDI0004hEA=0305

EA = 0300h + 4 + 1 = 0305h ← AX: [0305h] = 77h (AL), [0306h] = 44h (AH). Little-endian: the low byte goes to the low address.

mov bx, 0300h
mov si, 3
mov al, [0301h]
mov ah, [bx+si]
add al, [bx+si+1]
mov di, 4
mov [bx+di+1], ax

Classic Question · Segment and Offset

Question 10: Address Lines

a) How many different memory locations does a 20-bit address bus address?
b) How many address lines are needed for 4 KB of RAM?
c) How many lines are needed at least for 50 GB and 90 GB of memory?
d) Why is a segment at most 64 KB on the 8086?

Show answer
  • a) 2²⁰ = 1,048,576 locations; on the 8086 each location is one byte → 1 MB (00000h–FFFFFh)
  • b) 4 × 1024 = 4096 = 2¹² → 12 lines (A0–A11)
  • c) 2³⁵ = 32 GB < 50 GB ≤ 64 GB = 2³⁶ → 36 lines; 2³⁶ = 64 GB < 90 GB ≤ 128 GB → 37 lines. Rule: the smallest n with 2ⁿ ≥ capacity.
  • d) The offset is 16 bits: 2¹⁶ = 64 KB. Segments start at 16-byte paragraph boundaries and may overlap.

Multiple Choice · Segment and Offset

Question 11: Memory Capacity

A memory chip has 20 address lines and 16 data lines. What is its capacity?

D: The number of locations is 2²⁰ = 1 M; each location is 16 bits = 2 bytes → 1 M × 2 B = 2 MB (= 16 Mbit). 16 MB comes from counting bits as bytes. The 8086 memory is byte-addressed; there 20 lines mean 1 MB.

Multiple Choice · Segment and Offset

Question 12: Physical Address

If CS = 348Ah and IP = 4214h, what is the 20-bit physical address of the next instruction?

A: CS × 10h + IP = 348A0h + 4214h = 38AB4h. 0769Eh adds without shifting; 455CAh shifts IP instead of the segment; 390B4h is an addition error (the correct sum produces no carry at all).

Multiple Choice · Segment and Offset

Question 13: Which Offset?

CS = 4042h; BX = 2025h, IP = 0580h, DI = 4247h. What is the physical address of the memory location CS:IP points to?

B: CS always pairs with IP: 40420h + 0580h = 409A0h. 42445h uses BX and 44667h uses DI; BX and DI pair with DS by default (DI with ES in a string destination). 40A20h is an addition error (420h + 580h = 9A0h).

Classic Question · Segment and Offset

Question 14: Calculating Both Ways

a) If CS = 5500h and IP = 1200h, what is the physical address?
b) If CS = 4321h and IP = 1000h?
c) What can DS and SI be to access physical address 12345h? What are the smallest and largest possible DS, and how many different DS:SI pairs are there?

Show answer
  • a) 55000h + 1200h = 56200h (17500h shifts IP, 67000h means 55000h + 12000h)
  • b) 43210h + 1000h = 44210h
  • c) For example DS = 1230h, SI = 0045h (12300h + 45h); or 1234:0005, 1000:2345. Since SI is 16 bits, the smallest DS is 0235h (SI = FFF5h), the largest 1234h (SI = 0005h) → 1234h − 0235h + 1 = 4096 pairs.

Multiple Choice · Data Type

Question 15: Size Ambiguity

Which of the following cannot be assembled without adding BYTE PTR or WORD PTR?

B: Between a memory operand and a constant the size is not clear: should 5 be written as a byte (05) or a word (05 00)? MOV BYTE PTR [BX], 5 or MOV WORD PTR [BX], 5 is needed. In the others a register operand determines the size (AL, CL: 8 bit; AX: 16 bit).

Step-by-Step Trace · Data Type

Question 16: Little-Endian and XCHG

Initially CX = DX = 0, DS = 0700h. After the code runs, what are [0200h], [0201h], CX and DX?

AX1234AHALBX0000BHBLCX0000CHCLDX0000DHDLSI0000DS:0200 … DS:0201020000020100

AX = 1234h: AH = 12h, AL = 34h.

AX1234AHALBX0200BHBLCX0000CHCLDX0000DHDLSI0000DS:0200 … DS:0201020000020100

BX = 0200h: a relative location in the data segment.

AX1234AHALBX0200BHBLCX0000CHCLDX0000DHDLSI0000DS:0200 … DS:0201020034020112

Word write, little-endian: [0200h] ← AL = 34h, [0201h] ← AH = 12h.

AX1234AHALBX0200BHBLCX0000CHCLDX0000DHDLSI0001DS:0200 … DS:0201020034020112

SI = 1.

AX1234AHALBX0200BHBLCX0012CHCLDX0000DHDLSI0001DS:0200 … DS:0201020034020112

EA = BX + SI = 0201h → CL ← 12h; CH = 00h → CX = 0012h.

AX1234AHALBX0200BHBLCX0012CHCLDX1234DHDLSI0001DS:0200 … DS:0201020034020112

Word read: DL ← [0200h] = 34h, DH ← [0201h] = 12h → DX = 1234h (the bytes are reassembled).

AX1234AHALBX0200BHBLCX0012CHCLDX3412DHDLSI0001DS:0200 … DS:0201020034020112

XCHG swaps the halves → DX = 3412h. MOV and XCHG do not affect the flags.

MOV AX, 1234h
MOV BX, 0200h
MOV [BX], AX
MOV SI, 1
MOV CL, [BX+SI]
MOV DX, [BX]
XCHG DL, DH

Multiple Choice · MOV Instruction

Question 17: MOV: Valid or Not?

Which of the following instructions is valid?

C: CS can be read as a source: MOV AX, CS is valid. CS cannot be a destination (A); there is no direct MOV between two segment registers, the value goes through a general register (B); BX and BP cannot be used together in the same address (D).

Output Question · Program Example

Question 18: Video Memory

On an 80×25 text screen each cell is 2 bytes (character + color). a) Which character does the code write at which row and column? b) Which physical addresses get what? c) Why was MOV DS, 0B800h not used to load DS directly?

ORG 100h
MOV AX, 0B800h
MOV DS, AX
MOV BX, 338h
MOV CX, 1E41h
MOV [BX], CX
RET
Show answer
  • a) Relative address = (row × 80 + column) × 2 → 338h = 824 = 412 × 2 → 412 = 5 × 80 + 12: row 5, column 12 (counting from 0). CL = 41h = 'A'.
  • b) B8000h + 338h = B8338h ← 41h (character), B8339h ← 1Eh (color: blue background, yellow text). The word is written little-endian.
  • c) A constant cannot be loaded into a segment register; the value is first put in AX.

Classic Question · MOV Examples

Question 19: Direct Addressing

In a .COM program DS = 0700h. Which physical addresses do MOV AX, [0x1000] and MOV [0x2000], BX access? (b) What about MOV BX, [0x1500] and MOV [0x2200], AX?

Show answer
  • The number in square brackets is a relative location (direct addressing); the default segment for data is DS.
  • MOV AX, [0x1000]: 07000h + 1000h = 08000h; AL ← [08000h], AH ← [08001h]
  • MOV [0x2000], BX: 07000h + 2000h = 09000h; BL → 09000h, BH → 09001h
  • (b) BX ← 08500h–08501h word; AX → 09200h (AL) and 09201h (AH)

In a .COM program CS = DS, so the same result also comes out if computed with CS; but the segment of a data access is DS (SS for BP-based addresses).

Multiple Choice · MOV Examples

Question 20: Address or Value?

In MASM/emu8086 syntax, what does mov si, num1 load into SI?

mov si, num1
mov ax, [si]
ret
num1 dw 5, 10, 15, 0

B: In MASM syntax a name with defined data is a variable: mov si, num1 loads its contents (0005h). [si] now reads the unrelated bytes at DS:0005. For the address, write mov si, offset num1 or lea si, num1 (in NASM, however, the name itself means the address).

Step-by-Step Trace · MOV Examples

Question 21: Accumulating the Sum in Memory

In a .COM program that starts with ORG 100h, what are the four words of num1 and AX after the code runs? Why is each number first loaded into AX?

AX0000AHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+600+700

BX ← address of num1 (relative location): since the code is 21 bytes, 0100h + 15h = 0115h. Four words: 5, 10, 15, 0.

AX0005AHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+600+700

AX ← [BX] = 0005h.

AX0005AHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+605+700

[BX+6] ← AX: the fourth word is now 0005h.

AX000AAHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+605+700

AX ← [BX+2] = 000Ah.

AX000AAHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+60F+700

The destination of ADD can be memory, but both operands cannot be memory; that is why the number is first loaded into AX: [BX+6] = 0005h + 000Ah = 000Fh.

AX000FAHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+60F+700

AX ← [BX+4] = 000Fh.

AX000FAHALBX0115BHBLnum1 (byte by byte)+005+100+20A+300+40F+500+61E+700

[BX+6] = 000Fh + 000Fh = 001Eh (30). The sum is in memory; AX = 000Fh.

mov bx, offset num1
mov ax, [bx]      ; 1st number
mov [bx+6], ax    ; write to result
mov ax, [bx+2]    ; 2nd number
add [bx+6], ax    ; add to result
mov ax, [bx+4]    ; 3rd number
add [bx+6], ax
ret
num1 dw 5, 10, 15, 0

Output Question · MOV Examples

Question 22: Byte Array

In a .COM program that starts with ORG 100h, what are AL, DL and num1 after the code runs? Why do the offsets grow by 1? What would happen if the 2nd number were read into BL instead of DL?

mov bx, offset num1
mov al, [bx]      ; 1st number
mov dl, [bx+1]    ; 2nd number
add al, dl
mov dl, [bx+2]    ; 3rd number
add al, dl
mov [bx+3], al
ret
num1 db 5, 10, 15, 0
Show answer
  • AL = 1Eh, DL = 0Fh, num1 = 05 0A 0F 1E
  • DB elements are 1 byte: [BX], [BX+1], [BX+2], [BX+3]. The registers are also chosen 8-bit (AL, DL).
  • BL is the low byte of BX: the 0Ah that is read overwrites the low byte of the address (BX becomes 010Ah); the following [bx+2] and [bx+3] access the wrong place, and the sum is not written to num1+3.

The End

Chapter 4.1: Memory Access: Study Questions
Lecture slides: Chapter 4.1: Memory Access · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.