10/6/2024
Lecture 6: 80x86 Assembly
Programming II
Seyed-Hosein Attarzadeh-Niaki
Based on the slides by Hongzi Zhu
Microprocessors and Interfacing 1
Review
• Assembly statement
• Model definition
• Segments definition
• Building programs
• Control transfer instructions
– Short, near and far
• Data types and definition
Microprocessors and Interfacing 2
1
10/6/2024
Outline
• Addition and subtraction
• Multiplication and division (unsigned)
• BCD arithmetic
• Rotate instructions
Microprocessors and Interfacing 3
Arithmetic Instructions
• Addition
• Subtraction
• Multiplication
• Division
Microprocessors and Interfacing 4
2
10/6/2024
Unsigned Addition
• ADD dest, src ;dest = dest + src
– dest can be a register or in memory
– src can be a register, in memory or an immediate
– No mem-to-mem operations in 80X86
– Change ZF, SF, AF, CF, OF, PF
• ADC dest, src ;dest = dest + src + CF
– For multi-byte numbers
– If there is a carry from last addition, adds 1 to the
result
• INC
– adds 1 to any register or memory location
Microprocessors and Interfacing 5
Addition Example of Individual Bytes
Exercise: Draw the layout of the data
segment in memory
What’s this for?
Microprocessors and Interfacing 6
3
10/6/2024
Addition Example of Multi-byte Nums
Microprocessors and Interfacing 7
Unsigned Subtraction
• SUB dest, src ;dest = dest - src
– Dest can be a register or in memory
– Src can be a register, in memory or an immediate
– No mem-to-mem operations in 80X86
– Change ZF, SF, AF, CF, OF, PF
• SBB dest, src ;dest = dest - src – CF
– For multi-byte numbers
– If there is a borrow from last subtraction,
subtracts 1 from the result
Microprocessors and Interfacing 8
4
10/6/2024
Subtraction Example of Individual Bytes
• CPU carries out
1. take the 2’s complement of the src
2. add it to the dest
3. invert the carry
After these three steps, if
• CF = 0: positive result;
• CF = 1: negative result, left in 2’s complement
– Magnitude: NOT + INC (if a programmer wants the magnitude)
Microprocessors and Interfacing 9
Subtraction Example of Multi-byte
Nums
Microprocessors and Interfacing 10
5
10/6/2024
Unsigned Multiplication
• MUL operand
• byte X byte:
– One implicit operand is AL, the other is the operand,
result is stored in AX
• word X word:
– One implicit operand is AX, the other is the operand,
result is stored in DX & AX
• word X byte:
– AL hold the byte, AH = 0, the word is the operand,
result is stored in DX & AX;
Microprocessors and Interfacing 11
Unsigned Multiplication Example
• Byte x byte (reg addr) • Byte x byte (reg ind)
addr)
• Word x word
• Word x byte
Microprocessors and Interfacing 12
6
10/6/2024
Unsigned Division
• DIV denominator
– Denominator cannot be zero
– Quotient cannot be too large for the assigned register
• byte / byte:
– Numerator in AL, clear AH; quotient is in AL, remainder in AH
• word / word:
– Numerator in AX, clear DX; ; quotient is in AX, remainder in DX
• word / byte:
– Numerator in AX; quotient is in AL (max 0FFH), remainder in AH
• double-word / word:
– Numerator in DX, AX; quotient is in AX (max 0FFFFH), remainder in DX
• Denominator can be in a register or in memory
Microprocessors and Interfacing 13
Unsigned Division Example
• Byte/byte • Word/word
• Word/byte • Dword/word
Microprocessors and Interfacing 14
7
10/6/2024
Signed Multiplication and Division
(Read the Book)
• IMUL
CBW will copy
D7 (the sign
flag) to all
bits of AH.
• IDIV
CWD sign
extends AX. It
copies Dl5 of AX
to all bits of the
DX register.
Microprocessors and Interfacing 15
Logic Instructions
• AND
• OR
• XOR
• NOT
• Logical SHIFT
• ROTATE
• COMPARE
Microprocessors and Interfacing 16
8
10/6/2024
Basic Logic Operations
• AND dest, src • NOT operand
• OR dest, src – Bit-wise logic
• XOR dest, src – Operand can be a
register or in memory
• Bit-wise logic
• dest can be a register or
in memory; src can be a
register, in memory, or
immediate
Microprocessors and Interfacing 17
Logical SHIFT
• SHR dest, times
– dest can be a register or in memory
– 0->MSB->…->LSB->CF
– Fixed:
SHR xx, 1
– Variable:
MOV CL, times
SHR xx, CL
• SHL dest, times
– All the same except in reverse direction
Microprocessors and Interfacing 18
9
10/6/2024
Example: BCD & ASCII Numbers
Conversion
• BCD: Binary Coded Decimal
– Digits 0~9 in binary representation
– Unpacked, packed
• ASCII
– Code for all characters that you can read or write
– Digit characters ‘0’~’9’
Microprocessors and Interfacing 19
ASCII -> Unpacked BCD Conversion
• Simply remove the higher 4 bits “0011”
• E.g.,
asc DB ‘3’
unpack DB ?
-------------------------
MOV AH, asc
AND AH, 0Fh
MOV unpack, AH
Microprocessors and Interfacing 20
10
10/6/2024
ASCII -> Packed BCD Conversion
• First convert ASCII to unpacked BCD
• Then, combine two unpacked into one packed
• E.g.,
asc DB ‘23’
unpack DB ?
-------------------------
MOV AH, asc
MOV AL, asc+1
AND AX, 0F0Fh
MOV CL, 4
SHL AH, CL
OR AH, AL
MOV unpack, AH
Microprocessors and Interfacing 21
ROTATE
• ROR dest, times
– dest can be a register, in memory
– Fixed:
ROR xx, 1
– Variable:
MOV CL, times
ROR xx, CL
• ROL dest, times
– All the same except in reverse direction
Microprocessors and Interfacing 22
11
10/6/2024
ROTATE Cont.
• RCR dest, times
– dest can be a register, in memory
– Fixed:
RCR xx, 1
– Variable:
MCV CL, times
RCR xx, CL
• RCL dest, times
– All the same except in reverse direction
Microprocessors and Interfacing 23
Arithmetic Shift
• Used for signed numbers
– the sign bit is copied to the shifted bits
• SAR (shift arithmetic right)
– SAR destination, count
• SAL (shift arithmetic left)
– Same as SHL (shift left)
Microprocessors and Interfacing 24
12
10/6/2024
COMPARE of Unsigned Numbers
• CMP dest, src
– Flags affected as (dest – src) but operands remain
unchanged
– E.g., CMP AL, 23
JA lable1 ; jump if above, CF = ZF = 0
Microprocessors and Interfacing 25
Jump Based on Unsigned Comparison
Microprocessors and Interfacing 26
13
10/6/2024
COMPARE of Signed Numbers
• CMP dest, src
– Same instruction as the unsigned case
– but different understanding about the numbers
and therefore different flags checked
Microprocessors and Interfacing 27
Jump Based on Signed Comparison
Microprocessors and Interfacing 28
14
10/6/2024
Given the ASCII table, write an algorithm to
convert lowercase letters in a string into uppercase
Example: letters and implement your algorithm using 8086
assembly language.
Microprocessors and Interfacing 29
Answer
Microprocessors and Interfacing 30
15
10/6/2024
BCD Arithmetic
Addition Subtraction
• Addition of two BCD • Similar (but different)
numbers is not a BCD! problem
• DAA (decimal adjust for • DAS
addition) fixes this!
Read the book for
BCD multiplication
and division.
Microprocessors and Interfacing 31
Self-Learning
• Signed arithmetic
• XLAT Instruction & Look-up Tables
• Chapter 6
Microprocessors and Interfacing 32
16
10/6/2024
Next Lecture
• Memory and IO interfacing
Microprocessors and Interfacing 33
17