0% found this document useful (0 votes)
101 views68 pages

Microprocessor Systems and Interfacing (Eee342) : Dr. Omer Chughtai Assistant Professor, CUI, Wah Campus Email

This document provides an overview of microprocessor systems and interfacing concepts. It discusses various arithmetic, logical, shift, rotate, and control flow instructions used in microprocessors. These include addition, subtraction, multiplication, and division instructions. It also covers logical operations like AND, OR, XOR. Shift instructions like SHL, SHR, and rotate instructions like ROL, ROR are explained. Control flow instructions for conditional and unconditional jumps are discussed along with examples of IF-THEN-ELSE structures. The flags updated by different instructions and their usage in signed and unsigned comparisons are also summarized.

Uploaded by

Abdul Rab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views68 pages

Microprocessor Systems and Interfacing (Eee342) : Dr. Omer Chughtai Assistant Professor, CUI, Wah Campus Email

This document provides an overview of microprocessor systems and interfacing concepts. It discusses various arithmetic, logical, shift, rotate, and control flow instructions used in microprocessors. These include addition, subtraction, multiplication, and division instructions. It also covers logical operations like AND, OR, XOR. Shift instructions like SHL, SHR, and rotate instructions like ROL, ROR are explained. Control flow instructions for conditional and unconditional jumps are discussed along with examples of IF-THEN-ELSE structures. The flags updated by different instructions and their usage in signed and unsigned comparisons are also summarized.

Uploaded by

Abdul Rab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

MICROPROCESSOR SYSTEMS AND

INTERFACING (EEE342)

Dr. Omer Chughtai

Assistant Professor, CUI, Wah Campus


Email: omer.chughtai@cuiwah.edu.pk
RECAP

 Flags : CF, AF, SF, ZF, PF, OF  Multiplication instructions



 MUL S  unsigned multiplication
Addition instructions
 ADD AX,BX  AX = AX + BX
 IMUL S  signed multiplication
 Only CF, OF updated
 ADC AX,BX  AX = AX + BX + CF  If upper half of result = 0, CF & OF = 0
 INC AX  AX = AX + 1
 Otherwise, CF & OF = 1

 Subtraction instructions  Division instructions


 SUB AX,BX  AX = AX – BX  DIV S  unsigned division
 SBB AX,BX  AX = AX – BX – CF
 DEC AX  AX = AX – 1  IDIV S  signed division
 NEG AX  AX = -AX = 0 - AX
 Flags undefined
 Overflow causes exception

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)

MOV AL, 0x55

 AND  Logical AND AND AL, 0x1F = 00010101 = 0x15

 OR  Logical inclusive-OR OR AL, 0xC0 = 11010101 = 0xD5

 XOR  Logical exclusive-OR XOR AL, 0x0F = 11011010 = 0xDA

 NOT  Logical NOT NOT AL = 00100101 = 0x25

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;

 sar and shr are right shifts

 The source code format of any shift instruction is


s–– destination, count
 count operand can be a

 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

 Result has been multiplied by 2 (2n)

 MOV AX, 1234H


 MOV CL, 2

 SHR AX, CL

 Result has been divided by 2 (2n)


7
SHL / SAL / SHR / SAR

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

 MOV AX, 1234H


 MOV CL, 4

 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;

; CMP AX, BX means AX - BX


CMP AX, BX
JE EQUAL
... …………………. ; Next instruction if (AX) ≠ (BX)
… ………………….
EQUAL … ………………….; Next instruction if (AX) = (BX)
… ………………….
16
CMP

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

High-level program structure: if/else statement


If part: compare + jump (if (AX == BX))
Else part: what follows if condition false
Unconditional jump used to skip “if” part
Both parts have same exit (L2)
20
CMP

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

The answer will be ZF=1. Means equal


 The syntax of a subtract instruction is:
 sub dest, src; dest = dest - src and set flags
 The generic form of the instruction is:
 cmp dest, src; perform dest - src and set flags
 nothing is stored

 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

Jump if Equal (=).


JE , JZ ZF = 1 JNE, JNZ
Jump if Zero. <> - sign means
not equal
Jump if Not Equal (<>).
JNE , JNZ ZF = 0 JE, JZ
Jump if Not Zero.

Jump if Greater (>). ZF = 0


JG , JNLE Jump if Not Less or and JNG, JLE
Equal (not <=). SF = OF

Jump if Less (<).


JL , JNGE Jump if Not Greater or SF <> OF JNL, JGE
Equal (not >=).

Jump if Greater or Equal


JGE , JNL (>=). SF = OF JNGE, JL
Jump if Not Less (not <).

Jump if Less or Equal


ZF = 1
(<=).
JLE , JNG or JNLE, JG
Jump if Not Greater (not
SF <> OF
>).
23
JUMP INSTRUCTIONS FOR UNSIGNED NUMBERS
Instruction Description Condition Opposite Instruction
Jump if Equal (=).
JE , JZ ZF = 1 JNE, JNZ
Jump if Zero.
Jump if Not Equal (<>). <> - sign means
JNE , JNZ ZF = 0 JE, JZ
Jump if Not Zero. not equal
Jump if Above (>). CF = 0
JA , JNBE Jump if Not Below or and JNA, JBE
Equal (not <=). ZF = 0
Jump if Below (<).
Jump if Not Above or
JB , JNAE, JC CF = 1 JNB, JAE, JNC
Equal (not >=).
Jump if Carry.
Jump if Above or Equal
(>=).
JAE , JNB, JNC Jump if Not Below (not CF = 0 JNAE, JB
<).
Jump if Not Carry.
Jump if Below or Equal
CF = 1
(<=).
JBE , JNA or JNBE, JA
Jump if Not Above (not
ZF = 1
>).

24
SUBROUTINES
 Subroutine: special program segment that can be “called” from any point
in program
 A subroutine is also known as a procedure

 A return instruction must be included at the end of the subroutine to


initiate the return sequence to the main program environment
 CALL and RET instructions

 PUSH and POP instructions

25
CALL AND RET

Mnemonic Meaning Format Operation

Execution continues from the address of


the subroutine specified by the operand.
CALL Subroutine call CALL Operand Information required to return back to
the main program such as IP and CS are
saved on the stack

RET Return to the main program by restoring


RET Return or IP (and CS for far-proc). If operand is
RET Operand present, it is added to the contents of SP

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

 Calling subroutine: CALL <proc>


 Address of next instruction saved on stack
 Either IP or EIP (instruction pointer)
 When function ends, use return instruction (RET)
 Jumps to saved return address (IP/EIP)

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

 Placing data on stack: PUSH


 Store data “above” current TOS; decrement SP
 Stack grows toward lower addresses
 New SP points to start of data just stored

 Basic PUSH stores word or double word


 Directly storing flags: PUSHF
 Storing all 16-/32-bit general purpose registers: PUSHA/PUSHAD

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

 POP instructions generally executed in reverse order of corresponding


PUSH instructions

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

Mnemonic Meaning Format Operations Flag Affected


PUSH Push word PUSH S ((SP))  (S) None
onto stack (SP)  (SP) – 2
POP Pop word off POP D (D)  ((SP)) None
stack (SP)  (SP) + 2

 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

Mnemonic Meaning Operations Flag Affected


PUSHF Push flags on ((SP))  (Flags) None
to stack (SP)  (SP) – 2
POPF Pop flags (Flags)  ((SP)) OF, DF, IF, TF, SF, ZF, AF, PF, CF
from stack (SP)  (SP) + 2

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

 RET: return from procedure


 Saving state to stack: push instructions
 Store data “above” current TOS; decrement SP
 Basic PUSH stores word or double word

 Directly storing flags: PUSHF

 Storing all 16-/32-bit general purpose registers: PUSHA/PUSHAD

 Restoring state: POP/POPF/POPA/POPAD


38
LOOPS
 For loops are essentially three parts
 Initializing loop index
 Checking boundary condition
 Similar idea to if-then-else statements, but simpler
 If condition is false, end of loop

 Branch to label at first instruction after loop

 Usually done at start of loop, since no iterations should occur unless condition is true

 End of loop then contains jump back to beginning

 Incrementing loop index

 While loops only require the boundary condition check

39
LOOOPS AND LOOP-HANDLING INSTRUCTIONS
 The 8088 micro-processor has three instructions for implementing loop
operations

Mnemonic Meaning Format Operations

LOOP Loop LOOP Short-label (CX)  (CX) – 1


Jump is initiated to location defined by short-label if
(CX) ≠ 0;
otherwise execute next sequential instruction
LOOPE/ Loop while equal/loop while LOOPE/LOOPZ short- (CX)  (CX) – 1
LOOPZ zero label Jump to location defined by short-label if (CX) ≠ 0
and (ZF) = 1;
otherwise execute next sequential instruction
LOOPNE/ Loop while not equal/loop while LOOPNE/LOOPNZ (CX)  (CX) – 1
not zero
LOOPNZ short-label Jump to location defined by short-label if (CX) ≠ 0
and (ZF) = 0;
otherwise execute next sequential instruction

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

MOV AX, DATASEGADDR


MOV DS, AX
MOV SI, BLK1ADDR
MOV DI, BLK2ADDR
MOV CX, N
NXTPT: MOV AH, [SI]
MOV [DI], AH
INC SI
INC DI
LOOP NXTPT
HLT 41
LOOOPS AND LOOP-HANDLING INSTRUCTIONS
These five instructions are used to initialize internal
MOV DL, 05H registers
MOV AX, 0A00H After initialization, a data segment is setup
at physical address : 0A00016
MOV DS, AX SI points to the memory location at offset 000016 in this
MOV SI, 0H DS
DL contains the data 510 and CX register contains the
MOV CX, 0FH loop count 1510

The first instruction in the loop, increments the value in SI by 1


AGAIN:INCSI Therefore, SI points to the memory address 0A001 16
Next instruction compares the contents of memory location with DL
CMP [SI], DL If Data held at 0A00116 are 510, the zero flag is set ; otherwise , it is reset.
LOOPNE then decrements CX (making it E16) and then checks for CX = 0 or
ZF = 1
LOOPNE AGAIN Else Program control is returned to the instruction with the label AGAIN

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 CX00
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

 MOV AX, 0001 loads AX register with 0001

 MOV DX, 0000 loads DX register with 0000

 MUL CX multiply AX with CX and store result in DX:AX pair

 LOOP 0408 runs loop till CX not equal to Zero

 MOV [0600], AX store AX register content to memory location 0600

 MOV [0601], DX store DX register content to memory location 0601

 HLT stops the execution of program

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

MOV AL, 1H ; Initial value of result


MOV AH, 0H ; Initial value of result
MOV CL, 0H ; Start multiplying number
NXT:
MOV DL, N ; Last number for multiplication
CMP CL, DL ; Skip if done
JE DONE
INC CL ; Next multiplying number
MUL CL ; Result  Result * number
JMP NXT ; Repeat
DONE: MOV [FACT], AX ; Save the result
47
EXAMPLES

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

int max(int v1, int v2)


{
if (v1 > v2)
return v1;
else
return v2;
}

53
SWAP IN C

void swap(int *a, int *b)


{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

54
STRINGS AND STRING-HANDLING INSTRUCTIONS
 String  A series of data words (or bytes)
 What is array?

 8088 permit a programmer to implement operations such as to move


data from one block of memory to a block elsewhere in memory.
 Used for comparing the elements of two strings or to scan a string of data
elements stored in memory to look for a specific value
 There are five basic string instructions
 Move Byte or Word String (MOVSB/MOVSW)
 Compare string (CMPSB/CMPSW)
 Scan String (SCASB/SCASW)
 LOAD String (LODSB/LODSW)
 Store String (STOSB/STOSW)

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

repeat the given instruction


REPZ instruction REPZ
while ZF = 1

repeat the given instruction


REPNE instruction REPNE
while CX != 0

repeat the given instruction


REPNZ instruction REPNZ
while ZF = 0

moves contents of byte


MOVSB none MOVSB
given by DS:SI into ES:DI

moves contents of word


MOVSW none MOVSW
given by DS:SI into ES:DI

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

 This instruction copies a byte or a word from a location in the data


segment [DS:SI] to a location in the extra segment [ES:DI].
 For multiple byte/word movement, the value stored in the CX register by
the user functions as a counter. After each move, SI and DI are
automatically adjusted to point to the next source and destination
respectively.
 The Direction Flag (DF) value determines whether SI and DI are to be
incremented (DF = 0) or decremented (DF = 1) after each move.
 The MOVSB instruction tells the assembler to move data as bytes; the
MOVSW implies the string is to be moved as words.
 Usage MOVS dest, src (This usage is misleading; movement of data still
happens from DS:SI to ES:DI)
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

 Operation: Copies the content of the source to the destination; auto


increments/decrements both the source and destination addresses
((DS)0+(SI))  ((ES)0+(DI))
(SI) ±1 or 2  (SI)
(DI) ±1 or 2  (DI)
 Direction flag determines increment/decrement

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

MOV DI, BLK2ADDR


3. Load CX with the count of elements in the string
MOV CX, N
CLD
4. Set DF (Direction Flag) for auto-increment
NXTPT: MOVSB
LOOP NXTPT 5. Loop on string move to copy N elements
HLT MOVSB and LOOP replaces multiple move and
increment/decrement instructions
64
EXAMPLE: COPY 100 BYTES FROM ARRAY1 TO ARRAY2, USING THE
MOV INSTRUCTION.
LEADI, Array2 ; Starting address of Destination
LEA SI, Array1 ; Starting address of Source
MOV CX, 100 ; Number of elements = 100
Next: MOV AL, [SI] ; AL  [SI]
MOV [DI], AL ; [DI]  AL
INC SI ; SI + 1
INC DI ; DI + 1
LOOP Next ; Next element
...

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

STOSB AL ES:[DI]


After string opr, if DF = 0, then DI is Incremented by 1and vice versa

STOSW AX ES:[DI]


TO BE SPECIFIC
ALES:[DI]
AHES:[DI+1]
IF DF = 0, then DI is incremented by ??
2
If DF = 1, then DI is decremented by 2

68

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy