Professional Documents
Culture Documents
Lecture 1
Dr.Ayman AbuBaker
Dr.Ayman AbuBaker
ENIAC
http://www.seas.upenn.edu/~museum/index.html
Dr.Ayman AbuBaker
EDVAC
http://www.softlord.com/comp/
Types of Modern Computers
• Supercomputers
• Mainframes
• Minicomputers (workstations, servers)
• Microcomputers (PCs)
• Microcontrollers (PICs)
Dr.Ayman AbuBaker
Basic Parts of a Computer
• 3 Functional Units
– Central Processing Unit (CPU)
– Memory Unit
– I/O Devices
• 3 Buses
– Data Bus
– Address Bus
– Control Bus
Dr.Ayman AbuBaker
Basic Parts of a Computer
Address
Memory
Control
CPU Data
I/O
Dr.Ayman AbuBaker
Instruction Cycle
The basic action of microprocessor as it moves through the
instruction stream can be broken down into a series of four
simple steps:
1.FETCH
-Get instruction from memory and store in register.
2.DECODE
-Determine the instruction type and do the calculation.
3.EXECUTE
-Show the results.
Dr.Ayman AbuBaker
Fetch
Address
153 298
299
Instruction Pointer
Memory
Control
CPU Data
64
Instruction Register
7 I/O
Accumulator
Dr.Ayman AbuBaker
Decode & Execute
Address
153 299
Instruction Pointer
Memory
Control
CPU Data
64
Increment
Accumulator
Instruction Register
87 I/O
Accumulator
Dr.Ayman AbuBaker
Three-Bus System Architecture
• Bus
• A collection of electronic signal lines all dedicated
to a particular task
– The Data Bus
– The Address Bus
– The Control Bus
Dr.Ayman AbuBaker
Three-Bus Architecture
Address
Memory
Control
CPU Data
I/O
Dr.Ayman AbuBaker
Machine vs. Assembly Language
Comment
;Load 2000H into accumulator
Dr.Ayman AbuBaker
The 8086 Microprocessor
• Features
– 20-bit address bus, 16-bit data bus
– Separate units to fetch and execute instructions
– 16-bit registers (8-bit accessible)
– Hardware multiply and divide
– Support for external math coprocessor
– Instruction set 8080-compatible
Dr.Ayman AbuBaker
• It is a 16-bit μp.
Dr.Ayman AbuBaker
About Metrics
• The term metric means
– “a standard of measurement”
• A synonymous term is benchmark
– “a standard by which we can measure”
• We use metrics to measure the performance
of a microprocessor or computer.
• Other benchmarks:
– Whetstone, Dhrystone, CPUmark, Norton SI,
Winstone
Dr.Ayman AbuBaker
Performance Metrics
• MHz
• MIPS (Million Instructions Per Second)
• MFLOPS (Million Floating-point Operations
Per Second)
• SPEC (System Performance and Evaluation
Cooperative)
• iCOMP
– Productivity, Internet, 3D, multimedia
Dr.Ayman AbuBaker
8086 Package
40 lead 8086 Package
From:
Datasheet for Intel 80C86A
Dr.Ayman AbuBaker
Order number: 240029-002
Pipelining Inside 8086
• Two ways to make CPU become faster
– Increase frequency
• Technology dependent
• Only when the technology and cost allow, then we can make
the CPU work faster by increasing the frequency
– Improve the internal working of the CPU
• Pipeline scheme used in 8086
– Splitting the internal structure of the CPU into two parts
• Execution Unit (EU)
– Executes instructions previously fetched
• Bus Interface Unit (BIU)
– Access memory and peripherals
Dr.Ayman AbuBaker
Intel 8086 Internal Architecture
Dr.Ayman AbuBaker
Pipelining Inside 8086
• BIU has a buffer/queue to keep the BIU always
ahead of EU
– 4 bytes in 8088
– 6 bytes in 8086
– When queue is filled to the maximum capacity, the bus
will become idle
– BIU will fetch new instruction whenever the queue has
room for 2 bytes in the 8086 queue and for 1 byte in the
8088 queue
Dr.Ayman AbuBaker
Functional Units
Simple CPUs perform one action at a time.
Bus:
Busy Busy Busy Busy Busy
Dr.Ayman AbuBaker
Functional Units
The 8086/8088 has a pipelined architecture.
BIU – accesses memory and peripherals
EU – executes fetched instructions
BIU:
Fetch Fetch Write Fetch Fetch Read
EU:
6 cycles instead of 8
Idle Execute Execute Idle Execute Wait
Bus is more efficient
Bus:
Busy Busy Busy Busy Busy Busy
Dr.Ayman AbuBaker
Internal architecture of 8086
• 8086 has two blocks BIU and EU.
Dr.Ayman AbuBaker
• Both units operate asynchronously to give the 8086 an
overlapping instruction fetch and execution mechanism which
is called as Pipelining. This results in efficient use of the
system bus and system performance.
Dr.Ayman AbuBaker
EXECUTION UNIT
• Decodes instructions fetched by the BIU
• Generate control signals,
• Executes instructions.
• Control system
• Instruction decoder
• ALU
Dr.Ayman AbuBaker
EXECUTION UNIT – General Purpose Registers
16 bits
8 bits 8 bits
AX
AH AL Accumulator
BX BH BL Base
CX CH CL Count
DX DH DL Data
SP Stack Pointer
Pointer
BP Base Pointer
Index
SI Source Index
DI Destination Index
Dr.Ayman AbuBaker
EXECUTION UNIT – General Purpose Registers
Register Purpose
AX Word multiply, word divide, word I /O
AL Byte multiply, byte divide, byte I/O, decimal arithmetic
AH Byte multiply, byte divide
BX Store address information
CX String operation, loops
CL Variable shift and rotate
• The index registers (SI & DI) and the BX generally default to the
Data segment register (DS).
• The SI and the DI registers may also be used to access data stored in
arrays
Dr.Ayman AbuBaker
EXECUTION UNIT – Flag Register
• A flag is a flip flop which indicates some conditions produced by the
execution of an instruction or controls certain operations of the EU .
• In 8086 The EU contains
a 16 bit flag register
9 of the 16 are active flags and remaining 7 are undefined.
6 flags indicates some conditions- status flags
3 flags –control Flags
U U U U OF DF IF TF SF ZF U AF U PF U CF
Carry (CF) Holds the carry after addition or the borrow after subtraction.
Also indicates some error conditions, as dictated by some
programs and procedures .
Parity (PF) PF=0;odd parity, PF=1;even parity. Which count number of ones in the
number
Auxiliary (AF) Holds the carry (half – carry) after addition or borrow after
subtraction between bit positions 3 and 4 of the result
(for example, in BCD addition or subtraction.)
Zero (ZF) Shows the result of the arithmetic or logic operation.
Z=1; result is zero. Z=0; The result is 0
Sign (SF) Holds the sign of the result after an arithmetic/logic instruction
execution. Which isDr.Ayman
MSD inAbuBaker
the number , S=1; negative, S=0 for positive
Flag Purpose
A control flag.
Trap (TF) Enables the trapping through an on-chip debugging
feature. ( the program is debugging to find the error)
A control flag.
Interrupt (IF) Controls the operation of the INTR (interrupt request)
I=0; INTR pin disabled. I=1; INTR pin enabled.
A control flag.
Direction (DF) It selects either the increment or decrement mode for DI
and /or SI registers during the string instructions.
Overflow occurs when signed numbers are added or
Overflow (OF) subtracted. An overflow indicates the result has exceeded
the capacity of the Machine
Dr.Ayman AbuBaker
Execution unit – Flag Register
• Six of the flags are status indicators reflecting properties of the last
arithmetic or logical instruction.
• For example, if register AL = 7Fh and the instruction ADD AL,1 is
executed then the following happen
AL = 80h
CF = 0; there is no carry out of bit 7
PF = 0; 80h has an odd number of ones
AF = 1; there is a carry out of bit 3 into bit 4
ZF = 0; the result is not zero
SF = 1; bit seven is one
OF = 1; the sign bit has changed
Dr.Ayman AbuBaker
BUS INTERFACE UNIT (BIU)
Contains
• 6-byte Instruction Queue (Q)
• The Segment Registers (CS, DS, ES, SS).
• The Instruction Pointer (IP).
• The Address Summing block (Σ)
Dr.Ayman AbuBaker
THE QUEUE (Q)
• The BIU uses a mechanism known as an instruction stream
queue to implement a pipeline architecture.
Dr.Ayman AbuBaker
• These pre-fetching instructions are held in its FIFO queue. With
its 16 bit data bus, the BIU fetches two instruction bytes in a
single memory cycle.
• The EU accesses the queue from the output end. It reads one
instruction byte after the other from the output of the queue.
Dr.Ayman AbuBaker
Microprocessors
Lecture 2
Since 8086 is 16 bit data, the addresses are too big 00000
to fit in a 16 bit register or memory word. The 8086
gets around this problem by partitioning its
memory into segments
Code segment (64KB)
The memory in an 8086/88 based system
is organized as segmented memory. Data segment (64KB)
1 MB
The CPU 8086 is able to address
1Mbyte of memory. Extra segment (64KB)
FFFFF
Dr. Ayman AbuBaker
• The size of each segment is 64 KB
• A segment is an area that begins at any location which is
divisible by 16.
• A segment may be located any where in the memory
• Each of these segments can be used for a specific function.
– Code segment is used for storing the instructions.
– The stack segment is used as a stack and it is used to store the
return addresses.
– The data and extra segments are used for storing data byte.
• The 4 segments are Code, Data, Extra and Stack
segments.
• Each of the Segment registers store the upper 16 bit
address of the starting address of the corresponding
segments.
Dr. Ayman AbuBaker
Dr. Ayman AbuBaker
MEMORY
00000
BIU
Segment Registers 34BA0
CODE (64k)
44B9F
CSR 34BA 44EB0
DATA (64K)
1 MB
DSR 44EB 54EAF
54EB0
ESR 54EB EXTRA (64K)
64EAF
SSR 695E 695E0
STACK (64K)
795D
F
34BA0(CS)+
8AB4(IP)
3 D 6 5 4 (next address)
44B9F
Segment Address
0001 0000 0000 0000 0000
• SS:SP SS:BP
• DS:BX DS:SI
EU BIU
AX AH AL
IP
BX BH BL
Fetch & store
CX CH CL D code bytes in
CS DS ES SS
DH DL E PIPELINE
DX C
C C
O
O D O IP BX DI SP
PIPELINE D
SP D E (or) DI BP
O E
BP E U
QUEUE I SI
R T N
SI
DI
Timing Default
FLAGS ALU control Assignment
ADD AX, BX
1. Statements:
Programs consists of statements. Each statement is either an instruction which translates into
machine code or an assembler directive , which instructs the assembler to perform a
specific task. (memory allocation, procedure .. ).
There are Four fields in the statement (Instruction)
Name Operation Operand Comment
Ex: Start: Mov Cx,5 ; Initialized
(Assembler Directive )
Ex: MAIN PROC ; it creates a procedure called MAIN
Dr. Ayman AbuBaker
• Name Field : is used for instruction Labels, Procedure Names, and Variable names. (They
are memory address)
• Can be from 1 to 31 characters long
• May consist of letters, digits, and special characters (? . @ _ $ %)
• if a period is used (.), it must be the first char. (.Name)
• names my not begin with a digit
Legal Names:
Counter1 , @character , Sum_Of_Digits, Done?, .Test
Illegal Names:
Two Words, 2abc, A4.28, you&me
Operand Filed:
For an instruction, it is the data that are to be acted on by the operation. An instruction may have
Zero, One, or Two Operands
Ex: NOP Zero operand
Inc Ax One Operand
ADD Word,2 Two operand
Dr. Ayman AbuBaker
Comments Field:
A (;) Semicolon marks the beginning of this field, and an assembler ignores anything typed after
the (;).
2. Program Data
Data may express as binary, decimal, or Hex numbers and Characters.
Numbering Format:
1. Binary: Written as a bit string followed by ‘B’ or ‘b’. EX: 1010B
2. Decimal: Decimal digits ends with ‘D’ or ‘d’. Ex:1010D
3. Hex: Hex number ends with ‘H’ or ‘h’. EX: 1010H
Characters:
Characters or character strings must be enclosed in single or double quotes . Ex: “A” or ‘Hello’
Quiz: What is the value of lower and higher bits for WRD?
Arrays:
Is Sequence of memory bytes or words
EX: B_Array DB 10H, 20H, 30 H
W_Array DW 1000H, 40H, 329H
Character String:
Letters DB ‘ABC’
MSG DB ‘Hello’, 0AH Dr. Ayman AbuBaker
Named Constants
Symbolic name for a constant quality EQU ( Equalities):
To sign a name to a constant, we can use the EQU pseudo-op.
Syntax:
name EQU Constant
Ex: LF EQU 0AH
Str EQU ‘Type your name’
– Register Direction Bit (D bit) Tells the register operand in REG field in
byte 2 is source or destination operand
1: destination 0: source
- Data Size Bit (W bit) Specifies whether the operation will be performed
on 8-bit or 16-bit data
0: 8 bits 1: 16 bits
Destination Source
Memory Accumulator
Accumulator Memory NO MOV
Register Register
Memory Memory
Register Memory Immediate Segment Register
Memory Register Segment Register Segment Register
Register Immediate
Memory Immediate
Seg reg Reg 16
Seg reg Mem 16
Reg 16 Seg reg EX: MOV AL, BL
Dr. Ayman AbuBaker
Memory Seg reg
80x86 Addressing Mode
• The ways the CPU access operands.
• The number of addressing modes is determined
when the uP is designed and can’t be changed.
• Seven distinct addressing mode for 80x86
– 1. Register addressing mode
• Memory is not accessed.
• Very fast to execute.
• E.g., MOV BX, DX
• The source and destination must match in size
10003H 10003H
10002H 10002H
10001H 10001H
10000H 10000H
10003H 78H
10002H 56H
10001H 34H
10000H 12H
SS:135A 53H
SS:135B 9FH
SS:135C 6DH 6DH
SS:135D 4BH 4BH
SS:135E FCH FCH FCH
SS:135F 34H 34H 34H
SS:1360
SS:135A 53H
SS:135B 9FH
SS:135C 6DH 6DH
SS:135D 4BH 4BH
SS:135E FCH FCH FCH
SS:1360
Destination Source
Accumulator Reg 16
Memory Register
EX: ADD AX,BX
Register Register
Register Memory
Register immediate
STSEG_Label SEGMENT
DB 64 DUP (?)
STSEG_Label ENDS
;--------------------------------------------------
DTSEG_Label SEGMENT
; Data definition goes here
DTSEG_Label ENDS
;--------------------------------------------------
CDSEG SEGMENT
PROC_Label PROC FAR
; Instruction go here
PROC_Label ENDP
CDSEG ENDS
END MAIN
.Model small
STSEG_Label SEGMENT . STACK 100H
DB 64 DUP (?)
STSEG_Label ENDS
;--------------------------------------------------
DTSEG_Label SEGMENT .DATA
; Data definition goes here ; Data definition goes here
DTSEG_Label ENDS
;--------------------------------------------------
CDSEG SEGMENT .CODE
PROC_Label PROC FAR PROC_Label PROC FAR
; Instruction go here ; Instruction go here
PROC_Label ENDP PROC_Label ENDP
CDSEG ENDS
END MAIN END MAIN
1 .MODEL SMALL
2
.STACK 64 Find the content of AL and Flag register?
.DATA
3 COUNTER EQU 06
4 VALUES DB B4H, C4H, C8H, E6H AL= 7C
5
.CODE
6 MAIN: MOV AX,@DATA CF ZF SF OF PF AF
7 MOV DS, AX
MOV SI, OFFEST VALUES 1 0 0 1 0 0
8
MOV AL, [SI]
9 ADD AL, [SI+2]
10 END
1 .MODEL SMALL
2
.STACK 64 Find the content of AX and Flag
.DATA
3 COUNTER EQU 06
register?
4 VALUES DB B4H, C4H, C8H, E6H
5 AX= EE8C
.CODE
6 MAIN: MOV AX,@DATA
MOV DS, AX CF ZF SF OF PF AF
7
MOV SI, OFFEST VALUES
8
MOV AL, [SI+1] 0 0 1 0 0 0
9 ADD AX, [SI+2]
10 END
1 .MODEL SMALL
2
.STACK 64 Find the content of AX and Flag
.DATA
3 COUNTER EQU 06
register?
4 VALUES DB B4H, C4H, C8H, E6H
5 AX= AB90
.CODE
6 MAIN: MOV AX,@DATA
MOV DS, AX CF ZF SF OF PF AF
7
MOV SI, OFFEST VALUES
8
MOV AX, [SI] 1 0 1 0 0 1
9 MOV BX, [SI+2]
10 PUSH AX
PUSH BX
11
POP AL
12 ADD AX, [SI+2]
13 END
Ex:
MOV AH,01 ;option 01 inputs one character
INT 21H ;after the interrupt, AL = input character
(ASCII)
.CODE
Destination Source
Accumulator Reg 16
Example: XCHG [1234h], BX
Memory Register
Register Register
Register Memory NO XCHG
MEMs
SEG REGs
.CODE .CODE
MOV AL,STUDENT1 MOV AL,STUDENT1
ADD AL,STUDENT2 ADD AL,STUDENT2
ADC AL,STUDENT3 ADD AL,STUDENT3
57 56
Dr. Ayman AbuBaker
Arithmetic Instructions – SUB, SBB, DEC, NEG
Mnemonic Meaning Format Operation Flags
affected
SUB Subtract SUB D,S (D) - (S) (D) All
Borrow (CF)
SBB Subtract SBB D,S (D) - (S) - (CF) (D) All
with borrow
.CODE .CODE
MOV AL,STUDENT1 MOV AL,STUDENT1
SUB AL,STUDENT2 SUB AL,STUDENT2
SBB AL,STUDENT3 SUB AL,STUDENT3
2F 30
Dr. Ayman AbuBaker
CONTROL TRANSFER
INSTRUCTIONS
• In NEAR the control is transferred within
the current code segment (intrasegment).
ONLY the IP is changed.
• In FAR the control is transferred outside the
current code segment (intersegment). Both
CS and IP are changed.
Note: “above” and “below” refer to the relationship of two unsigned values; “greater” and “less” refer to the relationship of
two signed values. Dr. Ayman AbuBaker
Above/Below
vs.
Greater than/Less than
Unsigned Numbers
Signed Numbers
CMP Compare CMP D,S (D) – (S) is used in setting CF, AF, OF,
or resetting the flags PF, SF, ZF
Allowed Operands
Destination Source
(D) = (S) ; ZF=1, CF=0, SF=0
Register Register
(D) > (S) ; ZF=0, CF=0, SF=1 Register Memory
(D) < (S) ; ZF=0, CF=1, SF=0 Memory Register
Register Immediate
Memory Immediate
Dr.Ayman AbuBaker Accumulator Immediate
Above/Below
vs.
Greater than/Less than
MOV AL, 0FFH
MOV BL, 0
CMP AL, BL
___ bigger ; Is __ bigger than __?
…
The instruction “JNZ AGAIN” is assembled as “JNZ 000D” and the 000D is
the address of the instruction with label AGAIN.
(000D = 0013+FA = 000D) the carry is dropped. Note that FA is 2’s
complement of –6, meaning that the address of target is –6 bytes from the IP of
the next instruction.
Dr. Ayman AbuBaker
Example
.ASM File
cmp ax, [500h]
jle loc
jmp further_loc
loc: push cx
*si = al;
Dr. Ayman AbuBaker
Conditional Jump Example
0005 8A 47 02 AGAIN: MOV AL, [BX]+2
0008 3C 61 CMP AL, 61H
000A 72 06 JB NEXT
000C 3C 7A CMP AL, 7AH
000E 77 02 JA NEXT
0010 24 DF AND AL, 0DFH
0012 88 04 NEXT: MOV [SI], AL
Machine Code
SUBR1: blah1
RET
SUBR2: blah2
RET
IN AL, DX
Dr.Ayman AbuBaker
Unsigned Division
• Word / word
– The numerator is in AX and DX must be cleared
– The denominator can be in a register or memory
– After the division, AX will have the quotient and the
remainder will be in DX
• Word / byte
– The numerator is in AX and denominator can be a
register or memory
– After the division, AL will have the quotient and the
remainder will be in AH.
– The maximum quotient is FFH
Dr.Ayman AbuBaker
Unsigned Division
• Double Word / word
– The numerator is in AX and DX, with the most
significant word in DX the least significant word in AX
– The denominator can be in a register or memory
– After the division, the quotient will be in AX and the
remainder in DX
– The maximum quotient is FFFFH
Dr.Ayman AbuBaker
Multiplication and Division
Examples
Ex1: Assume that each instruction starts from these values:
AL = 85H, BL = 35H, AH = 0H
Dr.Ayman AbuBaker
Ex2: AL = F3H, BL = 91H, AH = 00H
1. MUL BL → AL * BL = F3H * 91H = 89A3H → AX = 89A3H
AH AL AH AL
POS
15 02 → NEG → 2’s(02) = FEH→ 15 FE
NEG
R Q
00 F 3H AH AL
AX
4. DIV BL → = = 01→(F3-1*91=62) → 62 01
BL 91H
R Q
Dr.Ayman AbuBaker
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
Dr.Ayman AbuBaker
Ex4: AX= 1250H, BL= 90H
1250H R Q
AX 50H 20H
2. DIV BL → = = 20H→1250-20*90 =50H →
BL 90 H AH AL
Dr.Ayman AbuBaker
Microprocessors
Lecture 8
XOR Logical Exclusive XOR D,S (S) + (D)→(D) OF, SF, ZF, PF, CF
OR AF undefined
_
NOT LOGICAL NOT NOT D (D) → (D) None
Destination Source
Destination
Register Register
Register Memory Register
Memory Register Memory
Register Immediate
Memory Immediate
Accumulator Immediate
Dr.Ayman AbuBaker
LOGICAL Instructions
• AND
– Uses any addressing mode except memory-to-memory and
segment registers
– Especially used in clearing certain bits (masking)
xxxx xxxx AND 0000 1111 = 0000 xxxx
(clear the first four bits)
– Examples: AND BL, 0FH
AND AL, [345H]
• OR
– Used in setting certain bits
xxxx xxxx OR 0000 1111 = xxxx 1111
(Set the upper four bits)
Dr.Ayman AbuBaker
• XOR
– Used in Inverting bits
Dr.Ayman AbuBaker
Dr.Ayman AbuBaker
ASCII to BCD Conversion
Dr.Ayman AbuBaker
Ex: ASC DB ‘9562481273’
ORG 0010H
UNPACK DB 10 DUP(?)
…
MOV CX,5 ;CX is the loop counter
MOV BX,OFFSET ASC ;BX points to ASCII data
MOV DI,OFFSET UNPACK ;DI points to unpacked BCD data
AGAIN: MOV AX,WORD PTR [BX] ;move next 2 ASCII numbers
to AX
AND AX,0F0F ;remove ASCII 3s (011)
MOV WORD PTR [DI],AX ;store unpacked BCD
ADD DI,2 ;point to next unpacked BCD data
ADD BX,2 ;point to next ASCII data
LOOP AGAIN
Dr.Ayman AbuBaker
ASCII to packed BCD Conversion
Dr.Ayman AbuBaker
ORG 0010H
VAL_ASC DB ‘47’
VAL_BCD DB ?
…
;reminder: the DB will put 34 in 0010H location and 37 in 0011H.
MOV AX,WORD PTR VAL_ASC ;AH=37 AL=34
AND AX,0F0FH ;mask 3 to get unpacked BCD
XCHG AH,AL ;swap AH and AL
MOV CL,4 ;CL=04 to shift 4 times
SHL AH,CL ;shift left AH to get AH=40H
OR AL,AH ;OR them to get packed BCD
MOV VAL_BCD,AL save the result
Dr.Ayman AbuBaker
Packed BCD to ASCII Conversion
Dr.Ayman AbuBaker
VAL1_BCD DB 29H
VAL3_ASC DW ?
….
MOV AL,VAL1_BCD
MOV AH,AL ;copy AL to AH. Now AH=29 and AL=29
AND AX,F00FH ;mask 9 from AH and 2 from AL
MOV CL,04 ;CL=04 for shift
SHR AH,CL ;shift right AH to get unpacked BCD
OR AX,3030H combine with 30 to get ASCII
XCHG AH,AL ;swap for ASCII storage convention
MOV VAL3_ASC,AX ;store the ASCII
Dr.Ayman AbuBaker
MACROS
• Macros are predefined functions which involve a
group of instructions to perform a special task
which can be used repeatedly.
• MACRO definition:
name MACRO dummy1,dummy2,…,dummyN
…
…
…
ENDM
Dr.Ayman AbuBaker
SUM MACRO DAT1,DAT2
MOV AL,DAT1
ADD AL,DAT2
ENDM
.MODEL SMALL
org 100h
.STACK 64
.DATA
COUNTER EQU 06
.CODE
MAIN:
MOV AX,@DATA
MOV DS,AX
SUM 3,5 Dr.Ayman AbuBaker
Shift and Rotate Instructions
SHR/SAL: shift logical left/shift arithmetic left
SHR: shift logical right
SAR: shift arithmetic right
ROL: rotate left
ROR: rotate right
RCL: rotate left through carry
RCR: rotate right through carry
Logical vs Arithmetic Shifts
• A logical shift fills the newly created bit position with
zero:
0
CF
CF
Dr.Ayman AbuBaker
Shift Instructions
Mnemo Meaning Format Operation Flags Affected
-nic
SAL/SH Shift SAL/SHL D, Count Shift the (D) left by the CF,PF,SF,ZF
L arithmetic number of bit positions AF undefined
Left/shift equal to count and fill the OF undefined
Logical vacated bits positions on if count ≠1
left the right the original
MSD/or with zeros
Shift the (D) right by the CF,PF,SF,ZF
SHR SHR D, Count number of bit positions AF undefined
Shift equal to count and fill the OF undefined
logical vacated bits positions on if count ≠1
right the left with zeros
Destination Count
Register 1
Register CL
Memory 1
Memory CL
Dr.Ayman AbuBaker
Dr.Ayman AbuBaker
SHL Instruction
• The SHL (shift left) instruction performs
a logical left shift on the destination
operand, filling the lowest bit with 0.
0
CF
• Operand types:
SHL reg,imm8
SHL mem,imm8
SHL reg,CL
SHL mem,CL Dr.Ayman AbuBaker
Fast Multiplication
Shifting left 1 bit multiplies a number by 2
mov dl,5 Before: 00000101 =5
Dr.Ayman AbuBaker
SHR Instruction
• The SHR (shift right) instruction performs a logical right
shift on the destination operand. The highest bit position is
filled with a zero.
0
CF
CF
MOV DL,-80
SAR DL,1 ; DL = -40
SAR DL,2 ; DL = -10
Dr.Ayman AbuBaker
Rotate Instructions
Mnem Meaning Format Operation Flags Affected
-onic
ROL Rotate ROL D,Count Rotate the (D) left by the CF
Left number of bit positions equal OF undefined
to Count. Each bit shifted out if count ≠ 1
from the left most bit goes back
into the rightmost bit position.
ROR Rotate ROR D,Count Rotate the (D) right by the CF
Right number of bit positions equal OF undefined
to Count. Each bit shifted out if count ≠ 1
from the rightmost bit goes
back into the leftmost bit
position.
RCL Rotate RCL D,Count Same as ROL except carry is CF
Left attached to (D) for rotation. OF undefined
through if count ≠ 1
Carry
RCR Rotate RCR D,Count Same as ROR except carry is CF
right attached to (D) for rotation. OF undefined
through Dr.Ayman AbuBaker if count ≠ 1
Carry
ROL Instruction
• ROL (rotate) shifts each bit to the left
• The highest bit is copied into both the Carry flag and
into the lowest bit
• No bits are lost
CF
MOV Al,11110000b
ROL Al,1 ; AL = 11100001b
MOV Dl,3Fh
ROL Dl,4 ; DL = F3h
Dr.Ayman AbuBaker
ROR Instruction
• ROR (rotate right) shifts each bit to the right
• The lowest bit is copied into both the Carry flag and
into the highest bit
• No bits are lost
CF
MOV AL,11110000b
ROR AL,1 ; AL = 01111000b
MOV DL,3Fh
ROR DL,4 ; DL = F3h
Dr.Ayman AbuBaker
RCL Instruction
• RCL (rotate carry left) shifts each bit to the left
• Copies the Carry flag to the least significant bit
• Copies the most significant bit to the Carry flag
CF
CLC ; CF = 0
MOV BL,88H ; CF,BL = 0 10001000b
RCL BL,1 ; CF,BL = 1 00010000b
RCL BL,1 ; CF,BL = 0 00100001b
Dr.Ayman AbuBaker
RCR Instruction
• RCR (rotate carry right) shifts each bit to the right
• Copies the Carry flag to the most significant bit
• Copies the least significant bit to the Carry flag
CF
STC ; CF = 1
MOV AH,10H ; CF,AH = 00010000 1
RCR AH,1 ; CF,AH = 10001000 0
Dr.Ayman AbuBaker
Rotate Instructions
Destination Count
Register 1
Register CL
Memory 1
Memory CL
Dr.Ayman AbuBaker
Flag control instructions
MNEM- MEANING OPERATION Flags
ONIC Affected
CLC Clear Carry Flag (CF) 0 CF
STC Set Carry Flag (CF) 1 CF
CMC Complement Carry (CF) (CF)l CF
Flag
CLD Clear Direction Flag (DF) 0
SI & DI will be auto incremented while string DF
instructions are executed.
STD Set Direction Flag (DF) 1
SI & DI will be auto decremented while string DF
instructions are executed.
CLI Clear Interrupt Flag (IF) 0 IF
Dr.Ayman AbuBaker
Compare Instruction, CMP
Mnemoni Meaning Format Operation Flags Affected
c
CMP Compare CMP D,S (D) – (S) is used in setting CF, AF, OF,
or resetting the flags PF, SF, ZF
Allowed Operands
Destination Source
(D) = (S) ; ZF=0
Register Register
(D) > (S) ; ZF=0, CF=0 Register Memory
(D) < (S) ; ZF=0, CF=1 Memory Register
Register Immediate
Memory Immediate
Dr.Ayman AbuBaker Accumulator Immediate
String?
• An array of bytes or words located in
memory
• Supported String Operations
– Copy (move, load)
– Search (scan)
– Store
– Compare
Dr.Ayman AbuBaker
String Instruction Basics
• Source DS:SI, Destination ES:DI
Dr.Ayman AbuBaker
String Instructions
Instruction prefixes
Dr.Ayman AbuBaker
Instructions
Mnemo- meaning format Operation Flags
Nic effect-
ed
MOVS Move string MOVSB/ ((ES)0+(DI)) ((DS)0+(SI)) none
DS:SI ES:DI MOVSW (SI) (SI) ± 1 or 2
(DI) (DI) ± 1 or 2
Dr.Ayman AbuBaker
Mnemo- meaning format Operation
Nic
SCAS Scan string SCASB/ Set flags as per
AX – ES:DI SCASW (AL or AX) - ((ES)0+(DI))
(DI) (DI) ± 1 or 2
Dr.Ayman AbuBaker