Microprocessor Programming I
Microprocessor Programming I
Programming I
Step Input Program output
Destination Source
Memory Accumulator
Accumulator Memory
Register Register
Register Memory
NO MOV
Memory Register
Mem Mem
Register Immediate
Imm Seg Reg
Memory Immediate Seg reg
Seg Reg
Seg reg Reg 16
Seg reg Mem 16
Reg 16 Seg reg
EX: MOV AL, BL
Memory Seg reg
Data Transfer Instructions - XCHG
Destination Source
Accumulator Reg 16
Example: XCHG [1234h], BX
Memory Register
NO XCHG
Register Register
MEMs
Register Memory
SEG REGs
Data Transfer Instructions – LEA, LDS, LES
Mnemonic Meaning Format Operation Flags
affected
LEA Load LEA Reg16,EA EA (Reg16) None
Effective
Address
LDS Load LDS Reg16,MEM32 (MEM32) None
Register (Reg16)
And DS
(Mem32+2) (DS)
7A 11000
BX 127A 12 11001
00 11002
DI 1000
30
11003
DS 3000
Packed BCD
Arithmetic Instructions
ADD, ADC, INC, AAA, DAA
Ex.2 INC BX
INC WORD PTR [BX]
CF=1
DAS
Borrow 1 from AH
Examples: DAS
(BX)=0000000000111010
2’s comp = 1111111111000110 = FFC6H
(CF)=1
Example
AH AL POS AH AL
15 02 → NEG → 2’s(02) = FEH→ 15 FE
NEG
R Q
AX 00 F 3H AH AL
4. DIV BL → = = 01→(F3-1*91=62) → 62 01
BL 91 H
R Q
Ex3: AX= F000H, BX= 9015H, DX= 0000H
DX AX
1. MUL BX = F000H * 9015H = 8713 B000
DX AX
2. IMUL BX = 2’S(F000H) * 2’S(9015H) = 1000 * 6FEB = 06FE B000
F 000 H
3. DIV BL = = B6DH → More than FFH → Divide Error.
15 H
AX 1250 H R Q
2. DIV BL → = = 20H→1250-20*90 =50H → 50H 20H
BL 90 H AH AL
Logical Instructions
Logical Instructions
Mnemonic Meaning Format Operation Flags Affected
AND Logical AND AND D,S (S) · (D) → (D) OF, SF, ZF, PF, CF
AF undefined
OR Logical Inclusive OR OR D,S (S)+(D) → (D) OF, SF, ZF, PF, CF
AF undefined
XOR Logical Exclusive OR XOR D,S (S) + (D)→(D) OF, SF, ZF, PF, CF
_ AF undefined
NOT LOGICAL NOT NOT D None
(D) → (D)
• OR
– Used in setting certain bits
xxxx xxxx OR 0000 1111 = xxxx 1111
(Set the upper four bits)
• XOR
– Used in inverting bits
xxxx xxxx XOR 0000 1111 = xxxxx’x’x’x’
-Example: Clear bits 0 and 1, set bits 6 and 7, invert bit 5 of
register CL:
AND CL, OFCH ; 1111 1100B
OR CL, 0C0H ; 1100 0000B
XOR CL, 020H ; 0010 0000B
Shift Instructions
Shift Instructions
Mnemonic Meaning Format Operation Flags Affected
SAL/SHL Shift arithmetic SAL/SHL D,Count Shift the (D) left by the CF,PF,SF,ZF
Left/shift number of bit positions AF undefined
Logical left equal to count and fill OF undefined if count ≠1
the vacated bits
positions on the right
with zeros
SHR Shift logical SHR D,Count Shift the (D) right by the CF,PF,SF,ZF
right number of bit positions AF undefined
equal to count and fill OF undefined if count ≠1
the vacated bits
positions on the left
with zeros
SAR Shift arithmetic SAR D,Count Shift the (D) right by the CF,PF,SF,ZF
right number of bit positions AF undefined
equal to count and fill OF undefined if count ≠1
the vacated bits
positions on the left
with the original most
significant bit
Shift Instructions
Destination Count
Register 1
Register CL
Memory 1
Memory CL
• Ex.
; Multiply AX by 10
SHL AX, 1
MOV BX, AX
MOV CL,2
SHL AX,CL
ADD AX, BX
• Ex. What are the results of SAR CL, 1
if CL initially contains B6H? DBH
• Ex. What are the results of SHL AL, CL
if AL contains 75H and CL contains 3? A8H
Rotate Instructions
Rotate Instructions
Mnemonic Meaning Format Operation Flags Affected
ROL Rotate left ROL D,Count Rotate the (D) left by the CF
number of bit positions OF undefined if count ≠ 1
equal to Count. Each bit
shifted out from the left
most bit goes back into the
rightmost bit position.
ROR Rotate right ROR D,Count Rotate the (D) right by the CF
number of bit positions OF undefined if count ≠ 1
equal to Count. Each bit
shifted out from the
rightmost bit goes back into
the leftmost bit position.
RCL Rotate left RCL D,Count Same as ROL except carry CF
through is attached to (D) for OF undefined if count ≠ 1
carry rotation.
RCR Rotate right RCR D,Count Same as ROR except carry CF
through is attached to (D) for OF undefined if count ≠ 1
carry rotation.
Rotate Instructions
Destination Count
Register 1
Register CL
Memory 1
Memory CL
Ex. What is the result of ROL BTRE PTR [SI], 1
if SI is pointing to a memory location that contains 41H? (82H)
Example
Write a program that counts the number of 1’s in a
byte and writes it into BL
DATA1 DB 97 ; 61h
SUB BL, BL ; clear BL to keep the number of 1s
MOV DL, 8 ; Counter
MOV AL, DATA1
AGAIN: ROL AL,1 ; rotate left once
JNC NEXT ; check for 1
INC BL ; if CF=1 then add one to count
NEXT: DEC DL ; go through this 8 times
JNZ AGAIN ; if not finished go back
NOP