Lecture6-80x86 Assembly Programming II
Lecture6-80x86 Assembly Programming II
Review
• Assembly statement
• Model definition
• Segments definition
• Building programs
• Control transfer instructions
– Short, near and far
• Data types and definition
1
10/6/2024
Outline
• Addition and subtraction
• Multiplication and division (unsigned)
• BCD arithmetic
• Rotate instructions
Arithmetic Instructions
• Addition
• Subtraction
• Multiplication
• Division
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
3
10/6/2024
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
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;
• Word x word
• Word x byte
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
• Word/byte • Dword/word
7
10/6/2024
• IDIV
CWD sign
extends AX. It
copies Dl5 of AX
to all bits of the
DX register.
Logic Instructions
• AND
• OR
• XOR
• NOT
• Logical SHIFT
• ROTATE
• COMPARE
8
10/6/2024
• Bit-wise logic
• dest can be a register or
in memory; src can be a
register, in memory, or
immediate
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
10
10/6/2024
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
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
Arithmetic Shift
• Used for signed numbers
– the sign bit is copied to the shifted bits
• SAR (shift arithmetic right)
– SAR destination, count
12
10/6/2024
13
10/6/2024
14
10/6/2024
Answer
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!
Self-Learning
• Signed arithmetic
• XLAT Instruction & Look-up Tables
• Chapter 6
16
10/6/2024
Next Lecture
• Memory and IO interfacing
17