Microprocessor Systems and Interfacing (Eee342) : Dr. Omer Chughtai Assistant Professor, CUI, Wah Campus Email
Microprocessor Systems and Interfacing (Eee342) : Dr. Omer Chughtai Assistant Professor, CUI, Wah Campus Email
INTERFACING (EEE342)
2
ARITHMETIC/LOGICAL INSTRUCTIONS
3
AND/OR/XOR/NOT
Clearing, Setting, and Toggling bits of an Operand
All logical operations use form: <op> D, S (D) = (D) <op> (S)
4
AND/OR/XOR/NOT
5
SHL / SAL / SHR / SAR
Shift instructions slide the bits in a location given by the destination operand to the
left or to the right.
sal and shl are left shifts;
number 1
immediate operand
the register specification CL
All shift instructions store last bit shifted out in carry flag (CF)
6
SHL / SAL / SHR / SAR
MOV AX, 1234H
SHL AX, 1
SHR AX, CL
8
SHL / SAL / SHR / SAR
9
SHRD/SHLD
SHRD and SHLD are the Shift Right and Left Double precision instruction,
respectively
Usage: SHLD dest,src,count
SHRD dest,src,count
Modifies flags: CF PF SF ZF (OF,AF undefined)
10
ROL AND ROR
ROL and ROR have the ability to rotate the contents of either an
internal register or a storage location in memory.
All rotate instructions store last bit shifted out in carry flag (CF)
MOV AX, 1234H
MOV CL, 02H
ROL AX, 1
ROR AX, CL
11
RCL AND RCR
RCL BX,CL
Before execution
Dest = (BX) = 0x1234 = 0001 0010 0011 01002
Count = 0x04, CF = 0
Note that the value returned to bit 0 is the prior content of CF and not bit
31. The value shifted out of 31 goes into the carry flag.
Last value rotated out of MSB retained in carry flag
First rotate loads prior value of CF at the LSB
After execution
Dest = (BX) = 0x2340 = 0010 0011 0100 00002
CF = 1
12
EXAMPLES
Given AL = 0x43, CL = 0x04, and CF = 0, show the state of AL after each instruction
in the sequence below:
ROR AL, 2
AL = 01000011 rotated right by 2
= 11010000 = 0xD0
CF = last bit rotated in = 1
ROL AL, CL
AL = 11010000 rotated left by 4
= 00001101 = 0x0D
CF = last bit rotated in = 1
RCR AL, 3
(AL,CF) = 00001101 1 rotated right by 3
= 011000011
CF = 1, AL = 011000012 = 0x61
RCL AL, 4
(CF,AL) = 101100001 rotated left by 4
= 0 00011011
CF = 0, AL = 000110112 = 0x1B 13
CONTROL FLOW AND JUMP INSTRUCTIONS
Jump operations
Unconditional Jump No decision making required
Conditional Jump Decision required
14
CONTROL FLOW AND JUMP INSTRUCTIONS
15
BRANCH PROGRAM STRUCTURE – IF-THEN-ELSE
IF <condition> The comparison is actually done by subtracting the
source byte or word from the destination byte or word
THEN
Statement;
ELSE
Statement;
17
CMP
Given the instructions below, what are the resulting register values if:
AX = 0010H, BX = 0010H
AX = 1234H, BX = 4321H
What type of high-level program structure does this sequence
demonstrate?
Instructions
CMP AX, BX
JE L1
ADD AX, 1
JMP L2
L1: SUB AX, 1
L2: MOV [100H], AX
18
ANSWER
First case: AX = BX = 0010H
CMP AX, BX Shows AX == BX
JE L1 Cond. true—jump to L1
ADD AX, 1
JMP L2
L1: SUB AX, 1 AX = AX – 1 = 000F
L2: MOV [100H], AX Store 000F at DS:100H
19
ANSWER CONT.
Second case: AX = 1234H, BX = 4321H
CMP AX, BX Shows AX < BX
JE L1 Cond. false—no jump
ADD AX, 1 AX = AX + 1 = 1235H
JMP L2
L1: SUB AX, 1
L2: MOV [100H], AX Store 1235 at DS:100H
CMP SI, DI
JE SAME
ADD CX, DX ; Executed if ZF = 0
: ; If (SI not equal to DI)
:
SAME: SUB BX, AX ; Executed if ZF = 1
; If (SI = DI)
21
EXAMPLES
After a CMP instruction, OF, SF, ZF and CF are set appropriately.
MOV AL, 5
MOV BL, 5
CMP AL, BL
For signed comparisons, the S (sign) and O (overflow) flags, taken together, have the
following meaning:
If ((S=0) and (O=1)) or ((S=1) and (O=0)) then dest < src
If ((S=0) and (O=0)) or ((S=1) and (O=1)) then dest >= src
22
JUMP INSTRUCTIONS FOR SIGNED NUMBERS
Instruction Description Condition Opposite Instruction
24
SUBROUTINES
Subroutine: special program segment that can be “called” from any point
in program
A subroutine is also known as a procedure
25
CALL AND RET
26
STRUCTURE OF A SUBROUTINE
Elements of a subroutine
Save of information to Stack – PUSH
Main body of subroutine – Multiple instructions
Restore of information from stack – POP
Return to main program – RET
Save of information
Must save contents of registers/memory locations to be used or other program parameters (FLAGS)
PUSH, PUSHF
Main body
Retrieve input parameters passed from main program via stack – stack pointer indirect address
Performs the algorithm/function/operation required of the subroutine
Prepare output parameters/results for return to main body via stack – stack pointer indirect
addressing
Restore information
Register/memory location contents save on stack at entry of subroutine must be restored before
return to main program – POP, POPF
27
X86 SUBROUTINES
Specify starting point with pseudo-op SQUARE PROC
<name> PROC
PUSH AX ; Save AX to stack
May save state/allocate variables at start MOV AL, BL ; Copy BL to AL
If so, will restore at end of subroutine
IMUL BL ; AX = BL * AL
Last instruction returns to saved address ; = original BL squared
Always RET
MOV BX, AX ; Copy result to BX
Pseudo-op after RET indicates routine end POP AX ; Restore AX
<name> ENDP
RET
SQUARE ENDP
28
EXAMPLE
Write a procedure named SQUARE that squares the contents of BL and
places the result in BX
Solution:
;Subroutine: SQUARE
;Description: (BX) = square of (BL)
SQUARE PROC
PUSH AX ; Save the register to be used
MOV AL, BL ; Place the number in AL
IMUL BL ; Multiply with itself
MOV BX, AX ; Save the result
POP AX ; Restore the register used
RET
SQUARE ENDP 29
CALL/RETURN
30
EXAMPLE
Assuming AX = 2 and BX = 4, show the results of the following sequence:
Assume the addresses of the first three instructions are 0005, 0008, and 0009,
respectively
CALL SUM
RET ; End main function
SUM PROC
MOV DX, AX ; DX = AX = 2
ADD DX, BX ; DX = DX + BX = 2 + 4 = 6
RET
SUM ENDP
31
SAVING STATE
May need to save state before routine starts
Overwritten registers (that aren’t return values)
FLAGS
32
RESTORING STATE
Removing data from TOS: POP
Data removed from TOS; SP incremented
Basic POP removes word/double word
Directly removing flags: POPF
Removing all 16-/32-bit general purpose registers: POPA/POPAD
33
REVISITING SUBROUTINE EXAMPLE
SQUARE PROC
PUSH AX ; Save AX to stack
MOV AX, BX ; Place the number in AL
IMUL BL ; Multiply with itself
MOV BX, AX ; Save the result
POP AX ; Restore AX
RET
SQUARE ENDP
34
PUSH AND POP OPERATIONS
35
Operand (S or D)
Register
PUSH AND POP OPERATIONS Seg-reg (CS illegal)
Memory
PUSH AX
((SP) – 1) (AH)
((SP) – 2) (AL)
(SP) (SP) – 2
POP AX
(AL) ((SP))
(AH) ((SP) + 1)
(SP) (SP) + 2 36
PUSH AND POP OPERATIONS
37
REVIEW: SUBROUTINES
Subroutines: low-level functions
When called, address of next instruction saved
Return instruction ends routine; goes to that point
May need to save state on stack
x86 specifics
CALL <proc>: call procedure
<proc> typically label
Saves address of next instruction to stack
Usually done at start of loop, since no iterations should occur unless condition is true
39
LOOOPS AND LOOP-HANDLING INSTRUCTIONS
The 8088 micro-processor has three instructions for implementing loop
operations
40
LOOOPS AND LOOP-HANDLING INSTRUCTIONS
Load count for the number of repeats
NEXT MOV CX, COUNT
:
: Body of routine that is repeated
:
LOOP NEXT Loop back to label NEXT if count not zero
42
FACTORIAL IN C
int fact(int n)
{
int i;
int fact = 1;
for (i = n; i > 1; i--)
fact *= i;
return fact;
}
If the Given Number is a 16-bit number, the AX register is automatically used as the second parameter
and the product is stored in the DX:AX register pair.
This means that the DX register holds the high part and the AX register holds the low part of a 32-bit
number. 43
FACTORIAL
Assumptions –
Starting address of program: 0400
Input memory location: 0500
Output memory location: 0600 and 0601
44
Starting address of program: 0400
Input memory location: 0500
Output memory location: 0600 and 0601
FACTORIAL
ADDRESS MNEMONICS COMMENTS
Algorithm –
03FC MOV CX, [0500] CX [0500]
1. Input the Number whose factorial is to be find
and Store that Number in CX Register 0400 MOV AX, 0001 AX 0001
(Condition for LOOP Instruction)
0404 MOV DX, 0000 DX 0000
2. Insert 0001 in AX(Condition for MUL
Instruction) and 0000 in DX 0408 MUL CX DX:AX AX * CX
3. Multiply CX with AX until CX become Zero(0)
040C LOOP 0408 Go To [0408] till CX00
using LOOP Instruction
4. Copy the content of AX to memory location 0410 MOV [0600], AX [0600] AX
0600
0414 MOV [0601], DX [0601] DX
5. Copy the content of DX to memory location
0601 0418 HLT Stop Execution
6. Stop Execution 45
FACTORIAL
Explanation –
MOV CX, [0500] loads 0500 Memory location content to CX Register
46
EXAMPLES
Given a number N in the range 0<N≤5, write a program that computes its
factorial and save the result in memory location FACT. (N! = 1*2*3*…*N)
Note that 0! = 1
Write a program that compares the elements of two arrays, A(I) and B(I).
Each array contains 100 16-bit signed numbers. Compare the
corresponding elements of the two arrays until either two elements are
found to be unequal or all elements of the arrays have been compared and
found to be equal.
Assume that the arrays start in the current data segment at offset addresses
A00016 and B00016, respectively.
If the two arrays are found to be unequal, save the address of the first
unequal element of A(I) in the memory location with offset address FOUND
in the current data segment otherwise, write all 0s into this location.
48
EXAMPLES
MOV CX, 64H ;Set up array counter
MOV SI, 0A000H ;Set up source array pointer
MOV DI, 0B000H ;Set up destination array pointer
GO_ON:
MOV AX, [SI]
CMP AX, [DI];Compare the next element
JNE MIS_MATCH ;Skip on a mismatch
ADD SI, 2 ;Update pointers and counter
ADD DI, 2
DEC CX
JNZ GO_ON ;Repeat for the next element
MOV [FOUND], 0H ;If arrays are identical, save a zero
JMP DONE
MIS_MATCH:
MOV [FOUND],SI ;Else, save the mismatch address
DONE: --- ---
49
EXAMPLES
Write an Assembly language code using microprocessor 8086 to
represent the following expression with different modes and structures
(1. The operands are available in the registers, 2. One of the operand is in
Memory, 3. The contents need to store in the stack).
Y = 5X2 + 4X ; (where x is the 8-bit input)
50
EXAMPLES
Given an array A(I) of 100 16-bit signed numbers stored in memory
starting at address A00016, write a program to generate two arrays from
the given array such that one array P(J) consists of all the positive
numbers and the other N(K) contains all the negative numbers. Store the
array of positive numbers in memory staring at offset address B00016 and
the array of negative numbers straining at offset address C00016
51
EXAMPLES
MOV CX, 64H ;Set up the counter
MOV AX, 0H ;Set up the data segment
MOV DS, AX
MOV BX, A000H ;Pointer for the given array
MOV SI, B000H ;Pointer for the +ve array
MOV DI, C000H ;Pointer for the - ve array
AGAIN:
MOV AX, [BX] ;Get the next source element
CMP AX, 0H ;Skip if positive
JGE POSTV
NEGTV:
MOV [DI], AX ;Else place in - ve array
INC DI
INC DI
JMP NXT ;Skip
POSTV:
MOV [SI], AX ;Place in the +ve array
INC SI
INC SI
NXT:
DEC CX ;Repeat for all elements
JNZ AGAIN
HLT 52
MAXIMUM VALUE FUNCTION IN C
53
SWAP IN C
54
STRINGS AND STRING-HANDLING INSTRUCTIONS
String A series of data words (or bytes)
What is array?
55
STRINGS AND STRING-HANDLING INSTRUCTIONS
MOVS −This
LODS −
CMPS −
LODS −
STOS −
SCAS − Thisinstruction
This instructionstores
instruction moves
compares
loads
compares
loads 1 Byte,
from
from
datathe
two
from Word
memory.
memory.
contents
data Ifor
register
items
If ofDoubleword
the
the
(AL,
ainoperand
operand
register
memory.
AX, or(AL,
is data
is EAX)
Data
of
of one
AX
one from
could
to
byte,
or
byte, ititmemory
memory.
EAX)
be of
is
iswith
loadedlocation
aloaded
byte
thesize,
into
into toAL
contents
the
word
the another.
ALof
orregister,
register,
an
doubleword.
item if
in
if the
the
memory.
operand
operand is
is one
one word,
word, itit
is
is loaded
loaded into
into the
the AX
AX register
register and
and aa doubleword
doubleword is is loaded
loaded into
into the
the EAX
EAX register.
register.
lowest-to-highest
CLD clear direction flag 0 auto-incrementing
address
highest-to-lowest
STD set direction flag 1 auto-decrementing
address
Direction Flag determines the direction in which string operations will proceed. 56
STRINGS AND STRING-HANDLING INSTRUCTIONS
OPCODE OPERAND EXPLANATION EXAMPLE
repeat the given instruction
REP instruction REP MOVSB
till CX != 0
repeat the given instruction
REPE instruction REPE
while CX = 0
57
STRINGS AND STRING-HANDLING INSTRUCTIONS
OPCODE OPERAND EXPLANATION EXAMPLE
moves contents of
MOVD none double word given by MOVD
DS:SI into ES:DI
moves the byte at
LODSB none address DS:SI into AL; LODSB
SI is incr/decr by 1
58
STRINGS AND STRING-HANDLING INSTRUCTIONS
OPCODE OPERAND EXPLANATION EXAMPLE
moves contents of double
MOVD none word given by DS:SI into MOVD
ES:DI
moves the byte at address
LODSB none DS:SI into AL; SI is incr/decr LODSB
by 1
moves the word at address
LODSW none DS: SI into AX; SI is LODSW
incr/decr by 2
moves the double word at
LODSD none address DS:SI into EAX; SI LODSD
is incr/decr by 4
moves contents of AL to byte
STOSB none address given by ES:DI; DI STOSB
is incr/dec by 1
moves the contents of AX to
STOSW none the word address given by STOSW
ES:DI; DI is incr/decr by 2
59
STRINGS AND STRING-HANDLING INSTRUCTIONS
OPCODE OPERAND EXPLANATION EXAMPLE
moves contents of
EAX to the DOUBLE
STOSD none WORD address given STOSD
by ES:DI; DI is
incr/decr by 4
compares byte at
ES:DI with AL and
SCASB none SCASB
sets flags according to
result
compares word at
SCASW none ES:DI with AX and SCASW
sets flags
compares double word
SCASD none at ES:DI with EAX SCASD
and sets flags
60
STRINGS AND STRING-HANDLING INSTRUCTIONS
OPCODE OPERAND EXPLANATION EXAMPLE
compares byte at
CMPSB none ES:DI with byte at CMPSB
DS:SI and sets flags
compares word at
CMPSW none ES:DI with word at CMPSW
DS:SI and sets flags
compares double word
at ES:DI with double
CMPSD none CMPSD
word at DS:SI and sets
flags
61
MOVSB/MOVSW
62
STRINGS AND STRING-HANDLING INSTRUCTIONS
Move string instruction
Used to move an element of data between a source and destination location in
memory:
General format:
MOVSB—move string byte
MOVSW—move string word
DF = 0 autoincrement
DF = 1 autodecrement 63
STRINGS AND STRING-HANDLING INSTRUCTIONS
Compare String and Scan String – CMPSB/CMPSW and SCASB/SCASW
MOV AX, DATASEGADDR Application example—block move
MOV DS, AX 1. Initialize DS & ES
MOV ES, AX
MOV SI, BLK1ADDR 2. Load SI and DI with block starting addresses
65
EXAMPLE: COPY 100 BYTES FROM ARRAY1 TO ARRAY2, USING MOVSB
INSTRUCTION.
LEADI, Array2 ; Starting address of Destination
LEASI, Array1 ; Starting address of Source
MOV CX, 100 ; Number of elements = 100
PUSH DS
POP ES ; make ES=DS
CLD ; set SI and DI to auto-increment
next: MOVSB
LOOP next
66
EXAMPLES
Memory Content Memory Dest Content
MOV AX,5000 H Source
MOV DS,AX
MOV SI,2500H 52500 42 62500 98
MOV AX,6000H 52501 45 62501 34
MOV ES,AX
52502 34 62502 34
MOV DI,2500H 52503 23 62503 23
CLD
MOV CX,0004H
REPNE CMPSB DS:[SI] -ES:[DI] and increment DIand SI (REPNE)
HLT (CX=0004h)
(DS=5000h), (SI=2500h), (DI=2500h), (ES=6000h)
3
1
2 THtime
4rdND
st
Time
timeREPNE
Time REP
REPNE
CMPSB
CMPSB
CMPSB will not happen because locations are
DS:[SI] -
DS:[SI]
no longer-ES:[DI] i.e.
ES:[DI] i.e.
NE [52502]-[62502]
[52500]-[62500] i.e.
[52501]-[62501] i.e. 34-34
i.e. 45-34
42-98(E)
(NE)
SI =2503
=2501 and DI = 2503
=2502 2501
2502
CX=0003h (this is because of REP prefix)
CX=0002h
CX=0001h 67
EXAMPLES
68