Lecture #11, Microprocessor-ALP-Data-Arithmetic
Lecture #11, Microprocessor-ALP-Data-Arithmetic
✓ MOV
✓ XCHG
✓ XLAT
✓ LEA
✓ LDS
✓ LES
MOV Instruction
❖ Move source operand to destination Rules
MOV destination, source • Both operands must be of same size
This reads “Move WORD1 to AX”. The contents of register AX are replaced by the contents
of memory location WORD1. The contents of WORD1 are unchanged.
Before After
0006 0008
AX AX
0008 0008
WORD1 WORD1
XCHG Instruction
❖ Exchanges the values of two operands
Rules
XCHG destination, source • Operands must be of the same size
Example: • At least one operand must be a register
XCHG AH, BL • No immediate operands are permitted
This instruction swaps the contents of AH and BL, so that AH contains what was previously
in BL and BL contains what was originally in AH.
Before After
DATA SEGMENT
HEX_TABLE DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH
DATA ENDS
DP g f e d c b a DATA INPUT
0 1 1 0 1 1 0 1 6DH 5
XLAT Instruction for Seven-Segment Display
CODE SEGMENT
ASSUME DS:DATA, CS:CODE DATA INPUT
START: 3FH 0 BX
06H 1
MOV AX, DATA ; Load data segment address into AX
5BH 2
MOV DS, AX ; Initialize DS register 4FH 3
MOV BX, OFFSET HEX_TABLE ; Load the offset of HEX_TABLE into BX 66H 4
6DH 5 BX + AL
MOV AL, DIGIT ; Load the digit (index) into AL
7DH 6
XLAT ; Translate digit to 7-segment code 07H 7
MOV SEGMENT_CODE, AL ; Store the result in SEGMENT_CODE 7FH 8
MOV AH, 4CH ; DOS interrupt to exit 6FH 9
INT 21H
CODE ENDS
END START
LEA Instruction
❖ LEA = Load Effective Address
LEA destination, source
❖ The LEA instruction can be used to get the offset address of a variable
Example:
LEA BX, MSG
This instruction puts the offset address of the variable MSG into BX.
LDS & LES Instruction
❖ LDS/LES instruction copies a word from two memory locations into a specified register.
❖ It then copies a word from the next two memory locations into DS/ES register.
LDS destination, source
Example:
LDS BX, [4326]
✓ BL ← [4326]
✓ BH ← [4327]
✓ DS ← [4328, 4329]
Before After
0000 2A 0D AE07
DS [4327] [4326] DS
1A 05 AE 07 2A 0D
BH BL [4329] [4328] BH BL
Arithmetic Instructions
✓ INC/DEC
✓ ADD/SUB
✓ MUL/IMUL
✓ DIV/IDIV
✓ ADC/SBB
INC/DEC Instruction
✓ destination = destination + 1 ✓destination = destination – 1
INC destination DEC destination
Example: Example:
INC WORD1 DEC BYTE1
0002 0003
WORD1 WORD1
Before After
FFFE FFFD
BYTE1 BYTE1
NEG Instruction
✓ destination = 2's complement of destination
NEG destination
Hexadecimal 0002
Example:
Binary 0000 0000 0000 0010
NEG BX 1111 1111 1111 1101
+1
Negates the contents of BX Binary 1111 1111 1111 1110
Hexadecimal F F F E
Before After
0002 FFFE
BX BX
ADD/SUB Instruction
✓destination = destination + source ✓destination = destination – source
ADD destination, source SUB destination, source
Rules
Example: Example:
ADD WORD1, AX SUB AX, DX
Before After
01BC 01BC
AX AX
0523 06DF
WORD1 WORD1
ADD/SUB Instruction
✓ destination = destination + source ✓destination = destination - source
ADD destination, source SUB destination, source
Example: Example:
ADD WORD1, AX SUB AX, DX
Before After
0000 FFFF
AX AX
0001 0001
DX DX
MUL/IMUL Instruction
❖ The MUL instruction is used for unsigned multiplication
❖ The IMUL instruction is used for signed multiplication r/m8
Multiplier
(Byte)
❖ Multiplies 8-, 16-, or 32-bit operand by AL, AX, or EAX
MUL multiplier r/m16 Multiplier
(Word)
❖ The instruction formats are:
MUL r/m8 ; AX = AL * r/m8 r/m32 Multiplier
(Double-
MUL r/m16 ; DX:AX = AX * r/m16 Word)
Before After
000A 00FA
AX AX
19 19
DL DL
MUL/IMUL Instruction
Example:
MOV AL, 0BEH ; AL=-66
MOV DL, 0FFH ; DL=-1
IMUL DL ; multiply 0BEH with 0FFH and result will be stored in AX
Before After
00 BE 00 42
AH AL AH AL
00 FF 00 FF
DH DL DH DL
DIV/IDIV Instruction
❖ The DIV instruction is used for unsigned division
❖ The IDIV instruction is used for signed multiplication r/m8
Divisor
(Byte)
❖ A single operand (divisor) is supplied
r/m16 Divisor
Divisor is an 8-bit, 16-bit, or 32-bit register or memory
(Word)
Dividend is implicit and is either AX, DX:AX, or EDX:EAX
Divisor
❖ The instruction formats are: r/m32
(Double-
DIV r/m8 Word)
DIV r/m16
DIV r/m32
DIV/IDIV Instruction
Example:
Divide AX = 8003h by CX = 100h
00 00 00 03
DH DL DH DL
01 00 01 00
CH CL CH CL
80 03 00 80
AH AL AH AL
DIV/IDIV Instruction
Question:
❖ What will be the hexadecimal values of DX and AX after the following instructions execute?
mov dx,0087h
mov ax,6023h
mov bx,100h
div bx
• Example:
MOV AX, 0FE9Bh ; AX = -357
cwd ; DX:AX = FFFFFF9Bh
IDIV Instruction
Example:
Divide DX:AX (-48) by BX (-5)
mov ax,-48
cwd ; sign-extend AX into DX
mov bx,-5
idiv bx ; AX = 9, DX = -3
FF FF FF FD
DH DL DH DL
FF D0 00 09
AH AL AH AL
FF FB FF FB
BH BL BH BL
ADC/SBB Instruction
✓ ADC Instruction: Addition with Carry
ADC destination, source
destination = destination + source + CF
✓ SBB Instruction: Subtract with Borrow
SBB destination, source
destination = destination - source – CF
✓ Destination can be a register or a memory location
✓ Source can be a register, memory location, or a constant
✓ Destination and source must be of the same size
✓ Memory-to-memory arithmetic is not allowed
Source: Integer Arithmetic
ADC/SBB Instruction
Example:
MOV AX, 0FFFFH ; Load the value 0xFFFF into AX (16-bit register)
MOV BX, AX ; Copy the value of AX into BX
ADD AX, 1 ; Add 1 to AX, resulting in an overflow
ADC BX, 1 ; Add 1 to BX, considering the carry flag from the previous addition
Final Result:
AX = 0000H
BX = 0001H