Chapter 3.2: Assembly Language

Chapter 3.2: Assembly Language · Study Questions
Assembly languageLine structureDirectivesNamesMOV and XCHGImplicit operandsInstruction timingIP and flagsFirst programErrors
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Assembly Language

Question 1: MOV AX, BX

Which of the following is true for the 8086 instruction MOV AX, BX?

C: MOV changes no flags. In Intel syntax the destination is on the left: BX → AX (A is wrong). Both operands are registers, so register addressing is used (B is wrong). The instruction is 2 bytes (8B C3; 89 D8 is also a valid encoding): IP advances from 0100h to 0102h (D is wrong).

Multiple Choice · Assembly Language

Question 2: Parts of a Line

diff: SUB CX, AX ; CX − AX — which operand is the destination in this line?

B: The line has the layout [label:] instruction dest, source [; comment]. diff is a label and names the address of this line (A); AX is the source (C); ; and everything after it is a comment, which is not translated into machine code (D). When SUB runs, CX ← CX − AX; AX does not change.

Multiple Choice · Assembly Language

Question 3: The Line That Produces No Machine Code

Which of the following assembly lines does not produce?

C: ORG is a directive: it tells the assembler "place the following bytes starting at relative location 100h" and writes no bytes to memory. In .COM programs the first 256 bytes are reserved for the PSP, so the code starts at 100h. The other three are instructions: B4 09, CD 21 and C3.

Multiple Choice · Assembly Language

Question 4: Valid Name

In emu8086 source code, which of the following variable or label names can be used?

C: A name starts with a letter or an underscore; letters, digits and underscores may follow. 2total starts with a digit (A). last-value contains a − sign, which is an operator; the assembler reads this as last minus value (B). cx is a register name and, since case is not distinguished, is the same as CX (D).

Multiple Choice · Working with Registers

Question 5: MOV Copies

With AL = 50h and BL = 40h, MOV AL, BL is executed. What do AL and BL become?

A: MOV copies the source (BL) into the destination (AL); the source does not change. It is not a swap (D is the result of XCHG); the destination is on the left (C reads the direction backwards).

Output Question · Working with Registers

Question 6: Swapping with XCHG

What do CX and DX become when the code runs? (b) What if the first two lines were MOV CX, 07h and MOV DX, 13h? How can the same swap be done without XCHG?

MOV CX, 5h
MOV DX, 15h
XCHG CX, DX
Show answer
  • CX = 0015h, DX = 0005h. 15h is hexadecimal (21 in decimal); the result is also written in hexadecimal.
  • (b) CX = 0013h, DX = 0007h.
  • XCHG swaps its two operands in a single instruction and does not affect the flags. Without it, a temporary register would be needed:
MOV AX, CX   ; backup
MOV CX, DX
MOV DX, AX

Multiple Choice · Working with Registers

Question 7: What Does MUL BL Change?

With AX = 1234h and BL = 10h, MUL BL is executed. What is AX afterwards?

B: In 8-bit MUL the multiplicand is implicitly AL and the result is implicitly written to all of AX: 34h × 10h = 52 × 16 = 832 = 0340h. The old 12h in AH is erased. 2340h is multiplying all of AX by 10h and truncating to 16 bits (A); 1240h would change only AL (C); the BL written in the instruction is only the multiplier (D).

Classic Question · Working with Registers

Question 8: Instruction Timing

On a 5 MHz 8086 a loop runs 500 times; the loop counter is incremented once per pass. If the counter is a word in memory, INC counter takes 21 clock periods; if it is in CX, INC CX takes 2.

  1. How many clock periods does incrementing the counter take in the two cases?
  2. What is the difference in microseconds?
  3. INC counter takes longer. Why?
Show answer
  1. In memory: 500 × 21 = 10,500 periods; in CX: 500 × 2 = 1000 periods.
  2. The difference is 9500 periods. At 5 MHz one period is 1 / 5 MHz = 0.2 µs → 9500 × 0.2 = 1900 µs (1.9 ms).
  3. A memory operand is first read and then written: two bus cycles and an effective-address calculation are needed. A register operand is inside the processor; no bus cycle is needed.

Step-by-Step Trace · IP and Flags

Question 9: IP and Flags

The program starts at 0100h. After each instruction, what are IP, AL, BX and CF, ZF, SF, OF?

AX007FAHALBX0000BHBLIP0102CF0ZF0SF0OF0

IP starts at 0100h; after the 2-byte instruction IP = 0102h, AL = 7Fh (+127). MOV does not change the flags.

AX0080AHALBX0000BHBLIP0104CF0ZF0SF1OF1

7Fh + 1 = 80h: the sum of two positive numbers came out negative → OF = 1, SF = 1. 128 fits unsigned → CF = 0. IP = 0104h.

AX0080AHALBX1234BHBLIP0107CF0ZF0SF1OF1

BX ← 1234h; 3 bytes → IP = 0107h. The flags are left over from the previous operation.

AX0000AHALBX1234BHBLIP0109CF0ZF1SF0OF0

80h − 80h = 00h → ZF = 1, SF = 0, OF = 0; no borrow → CF = 0. IP = 0109h.

AX0000AHALBX1235BHBLIP010ACF0ZF0SF0OF0

INC BX → 1235h; the result is not zero → ZF = 0. INC is a single byte: IP = 010Ah.

ORG 100h
MOV AL, 7Fh     ; 2 bytes
ADD AL, 1       ; 2 bytes
MOV BX, 1234h   ; 3 bytes
SUB AL, 80h     ; 2 bytes
INC BX          ; 1 byte

Multiple Choice · First Program

Question 10: The ORG Directive

The ______ directive tells the assembler to place the following code and data starting at the given relative location. What fills the blank?

B: ORG 100h: in .COM programs the code starts at 0100h (the first 256 bytes are the PSP) and variable addresses are computed accordingly. GROUP gathers several segments under one name; OFFSET gives the relative location of a name; LABEL gives a name a type (BYTE, WORD, NEAR).

Multiple Choice · First Program

Question 11: Address of a Variable

ORG 100h starts a .COM program, followed in order by MOV AX, 5 (3 bytes), MOV BX, 3 (3 bytes), ADD AX, BX (2 bytes), RET (1 byte), and num DB 0 follows. What is the relative location of the variable num?

A: ORG 100h places the first byte at 0100h and generates no bytes itself. The instructions are 3 + 3 + 2 + 1 = 9 bytes; num sits right behind them, at 0100h + 9 = 0109h. 0009h is forgetting the ORG (B); 010Ah counts one byte too many (C); 0100h is the address of the first instruction (D).

Multiple Choice · First Program

Question 12: Valid MOV

Which of the following instructions is valid?

D: MOV from a general register to a segment register is valid. A: a constant cannot be loaded into a segment register (first MOV AX, 1234h, then MOV DS, AX). B: two memory operands are not allowed. C: a 16-bit BX cannot be written into the 8-bit AL.

Coding Question · First Program

Question 13: Memory to Memory

How do you copy a word in memory to another memory location with MOV? Write a .COM program that copies the variable source to dest.

Show answer

It cannot be done with a single MOV: on the 8086 an instruction cannot have two memory operands at once (MOV [dest], [source] is invalid). The data is moved through a register in two steps:

ORG 100h
MOV AX, source
MOV dest, AX
RET
source DW 1234h
dest  DW 0

In the end dest = 1234h. The variables sit after RET so that they are not executed as instructions. Exception: the string instructions (MOVSB/MOVSW) and PUSH/POP can transfer from memory to memory.

Spot the Bug · First Program

Question 14: Common Mistakes

Each of lines 2–6 of the program gives an assembler error. What is the cause of each error, and how is it fixed?

ORG 100h
MOV AL, BX
MOV DS, 0B800h
MOV AL, [AX]
MOV CL, FFh
MOV AH, 300
RET
Show answer
  • MOV AL, BX: sizes differ (8 ↔ 16 bit) → MOV AL, BL
  • MOV DS, 0B800h: a constant cannot be assigned to a segment register → MOV AX, 0B800h · MOV DS, AX
  • MOV AL, [AX]: only BX, SI, DI, BP are allowed in addressing → MOV BX, AX · MOV AL, [BX]
  • MOV CL, FFh: a hexadecimal number starting with a letter is mistaken for a name → MOV CL, 0FFh
  • MOV AH, 300: 300 > 255, does not fit in 8 bits → 16-bit register: MOV AX, 300

Multiple Choice · First Program

Question 15: Logic Error

Which of the following is a logic error that the assembler cannot find and that appears only when the program runs?

C: ADD is also a valid instruction; the program assembles and runs, but the carry coming from the first byte is lost and the result is wrong. Such an error is found only by single-stepping and comparing with the expected register values. The other three are syntax errors that the assembler reports with a line number: size mismatch (A), undefined label (B), a constant that does not fit in 8 bits (D).

The End

Chapter 3.2: Assembly Language: Study Questions
Lecture slides: Chapter 3.2: Assembly Language · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.