Samara University College of Engineering and Technology Computer Science
Samara University College of Engineering and Technology Computer Science
Chapter 4
1
Outline
2
CHAPTER OBJECTIVES
Upon completion of this chapter, you will be able to:
4
Data movement instructions
AAH AD5F8H
0728H
2EH AD5F7H
Example: DS=ACDEH, 0727H
00H AD5F6H
0726H
SI=0724H MOV AX, [SI] 55H AD5F5H
Physical Address
0725H
02H AD5F4H
0724H
= ACEDx10H + 0724H 0723H
72H AD5F3H
AX=5502H
6
PUSH and POP
PUSH and POP Instructions
Both are important to store and retrieve data from the
LIFO structure (stack).
PUSH Operand
It pushes the operand into top of stack.
Example:
PUSH BX
POP Des
It pops the operand from top of stack to Des.
Example:
POPAX
7
PUSH
In 8088 and 8086 PUSH always transfer 2 bytes of data to the
stack.
It has the following syntax:
PUSH src
Src: could be any 2 byte register or memory location or
immediate value.
How does PUSH executes?
Simply when we push 2 bytes of data to the stack, the most
significant byte is stored in stack segment pointed to by SP-
1, and the least significant to location pointed by SP-2, see
the following example
8
PUSH…
PUSH AX
SS:[SP-1] ← AH, 1st(most significant) byte.
SS:[SP-2] ← AL, 2nd(least significant) byte.
After PUSH operation, SP ← SP – 2
Example:
Let AX=31F2H, SP=100H, SS=2000H,
CX=FFFFH, BX=413AH
SS:SP-2 =2000:00FEH F2
AX
SS:SP-1 =2000:00FFH
31 F2 31
SS:SP =2000:0100H
Stack segment
6AB3 6AB3
03800
037FF
EAX 6AB3 6A B3
037FE
ESP 07FE
6AB3
03000
CS
DS
0300
SS +
10
POP
How does POP executes?
POP : performs the inverse operation of PUSH
POP : removes data from stack and place it into target 16-
bit register.
POP BX
BL ← SS:[SP]
BH ← SS:[SP+1]
after POP operation, SP← SP+2
POP CS ; is not a valid instruction
11
POP…
The POP BX instruction, showing how data are removed
from the stack.
POP BX
12
LEA
LEA stands for load effective address.
LEA Register, Src
It loads a 16-bit register with the offset address of the data
specified by the Src.
LEA AX, NUMB
AX ← operand offset address MUMB
Example:
LEA BX, [DI] loads offset address specified by [DI]
(contents of DI) into BX register
MOV BX, [DI] loads the data stored at the memory
location addressed by [DI] into BX register
MOV BX, DI = LEA BX, [DI]
13
LDS
LDS Des, Src
It loads 32-bit pointer from memory source to destination
register and DS.
The offset is placed in the destination register and the segment
is placed in DS.
To use this instruction the word at the lower memory address
must contain the offset and the word at the higher address must
contain the segment.
Example:
LDS BX, [DI]
If DS= 1000H, DI = 1000H
The LDS BX, [DI] instruction loads register BX from
addresses 11000H and 11001H and register DS from locations
11002H and 11003H. This instruction is shown at the point just
14
before DS changes to 3000H and BX changes to 127AH
LDS…
15
LES
LES Des, Src
It loads 32-bit pointer from memory source to destination register and ES.
The offset is placed in the destination register and the segment is placed in
ES.
To use this instruction the word at the lower memory address must contain
the offset and the word at the higher address must contain the segment.
This instruction is very similar to LDS except that it initializes ES instead of
DS.
Example:
LES BX, [DI]
If ES= 1000H, DI = 1000H
The LES BX, [DI] instruction loads register BX from addresses 11000H and
11001H and register ES from locations 11002H and 11003H. This
instruction is shown at the point just before ES changes to 3000H and BX
changes to 127AH.
16
LES…
17
LAHF, SAHF, PUSHF, POPF
LAHF:
It transfer the lower byte of flag register to AH.
AH ← FLAG (Lower 8-bit)
SAHF:
It transfer the contents of AH to lower byte of flag
register.
FLAG (Lower 8-bit) ← AH
PUSHF:
Pushes flag register to top of stack.
POPF:
Prof. FPayeoz Fp. Ms. El-tSohusye stack top to flag
register. 18
XCHG
XCHG Des, Src
This instruction exchanges Src with Des.
It Exchanges contents of a register with any other register
or memory location.
It can not exchange segment registers.
It can not exchange two memory locations directly.
Use any addressing mode except immediate.
Example:
XCHG DX, AX(exchanges the contents of DX with AX)
Let DX= 3000H, AX = 1000H
After Execution this Instruction DX= 1000H, AX = 3000H
19
XLAT(Translate)
This instruction Converts the contents of the AL register
into a number stored in a memory table.
It performs the direct table lookup technique often used to
convert one code to another.
An XLAT instruction first adds the contents of AL to BX to
form a memory address within the data segment.
It copies the contents of this address into AL.
It is only instruction that adds an 8-bit to a 16-bit number
Example:
AL ← DS:[BX + AL]
20
XLAT(Translate)…
The operation of the XLAT instruction at the point just
before
6DH Pirosf. Flaoyeaz Fd. Me.dEl-Sionusyto AL.
21
IN and OUT
IN Accumulator, Port Address
This instruction transfers the operand from specified port to
accumulator register.
IN AX, 0028 H
An IN instruction transfers data from an external I/O
device into AL or AX
OUT Port Address, Accumulator
This instruction transfers the operand from accumulator to
specified port.
OUT 0028 H, AX
An OUT transfers data from AL or AX to an external I/O
device.
22
IN and OUT…
IN & OUT instructions perform I/O operations. Contents of AL or
AX are transferred only between I/O device and microprocessor.
Two forms of I/O device (port) addressing: Fixed-port addressing
allows data transfer between AL, or AX using an 8-bit I/O port
address.
Port number follows the instruction’s opcode. IN AX, 0028 H
OUT 0028 H, AX
Variable-port addressing allows data transfers between AL or AX and
a 16-bit port address. The I/O port number is stored in register DX,
which can be changed (varied) during the execution of a program.
MOV DX, 0028H IN AX, DX
OUT DX, AX
23
IN and OUT…
The signals found in the microprocessor-based system for an OUT
19H, AX instruction.
24
IN and OUT…
25
II. Arithmetic and Logic
Instructions
26
Addition(ADD and ADC)
29
Comparison(CMP)
The comparison instruction (CMP) is a subtraction that changes only the flag bits; the
destination operand never changes. A comparison is useful for checking the entire
contents of a register or a memory location against another value. A CMP is normally
followed by a conditional jump instruction, which test the condition of the flag bits
i. CMP CL,BL ; CL-BL
ii. CMP AX,SP ; AX-SP
iii. CMP [DI],CH ; CH subtracts from the byte contents of the data
segment memory location addressed by DI
Example 5-12:
CMP AL,10H ; compare AL against 10H
JAE SUBER ; if AL is 10H or above jump to location SUBER
Normally used:
JA(Jump Above)
JB(Jump Below)
JAE(Jump Above or Equal)
JBE(Jump Below or Equal) 30
Multiplication
Only modern microprocessor contain multiplication and division instruction. Earlier 8-
bit microprocessor could not multiply or divide without the use of a program that
multiplied or divided by using a series of shifts and addition or subtractions. Because
microprocessor manufacturers were aware of this inadequacy, they incorporated
multiplication and division instructions into the instruction sets of the newer
microprocessors CMP CL,BL ; CL-BL
i. CMP AX,SP ; AX-SP
ii. CMP [DI],CH ; CH subtracts from the byte contents of the data
segment memory location addressed by DI
Note:
i) Multiplication is performed on bytes or words
ii) The product after a multiplication is always a double-width product. If two 8-bit
numbers are multiplied they generate a 16-bit product. If two 16-bit numbers are
multiplied, they generate a 32- bit product
iii) Some flag bits [overflow (O) and carry (C) ] change when the multiply instruction
executes and produce predictable outcomes
31
Multiplication(8-bit Multiplication)
The multiplication instruction contains one operand because it always multiplies the
operand times the contents of ALCMP AX,SP ; AX-SP
i) MUL CL ; AL is multiplied by CL and the unsigned product is in AX
ii) IMUL DH ; AL is multiplied by DH and the signed product is in AX
Example 5-13
Write a short instruction to multiply the content of BL and CL. Load BL with 5, CL
with 10. Store the result in DX register
MOV BL,5 ;load data
MOV CL,10
MOV AL,CL ;position data
MUL BL ;multiply , result is in AX
MOV DX,AX ;hence move AX to DX
Use IMUL instead of MUL for signed 8-bit multiplication
32
Multiplication(16-bit Multiplication)
AX contains the multiplicand instead of AL
The 32-bit product appears in DX-AX instead of AX
DX register contains the Most Significant 16 bits of the
product
AX contains the Least Significant 16 bits
i) MUL CX ; AX is multiplied by CX and the unsigned
product is in DX-AX
ii) IMUL DI ; AX is multiplied by DI and the signed product
is in DX-AX
Note:
8086/8088 microprocessors can not perform immediate
multiplication
33
Division
Division occurs on 8-or 16-bit numbers
The dividend is always a double-width dividend that is
divided by the operand
This means that an 8-bit division divides a 16-bit number by
an 8-bit number; a 16-bit division divides a 32-bit number
by a 16-bit number
None of the flag bits change predictably for a division. A
division can result in two different types of errors
1) One is an attempt to divide by zero
2) Other is a divide overflow
For Example:
If AX=3000 is divided by 2, answer = 1500, which does not fit
34
into AL
Division(8-bit Division)
An 8-bit division uses the AX register to store the dividend that is divided by the
contents of any 8-bit register or memory location
The quotient moves into AL after division with AH containing a whole number
remainder
Zero-extension is needed in 8-bit division
i) DIV CL ; AX is divided by CL; the unsigned quotient is in AL and the remainder is in
AH
ii) IDIV BL ; AX is divided by BL; the signed quotient is in AL and the signed
remainder is in AH
Example 5-14
Divide number NUMB by number NUMB1. Store the quotient at ANSQ and remainder
at ANSR
MOV AL,NUMB ; get NUMB
MOV AH,0 ; zero-extend
DIV NUMB1 ; divide by NUMB1
MOV ANSQ,AL ; save quotient
35
MOV ANSR,AH ; save remainder
Division(16-bit Division)
DIV CX ; DX-AX is divided by CX and the unsigned
quotient is in AX and unsigned remainder is in DX
Example 5-15:
-100 is in AX. Divide by +9 in CX register
Solution:
MOV AX, -100 ; load -100
MOV CX, 9 ; load +9
CWD ; CWD convert word to double word
(i.e -100 in AX is converted to -100 in DX-AX)
IDIV CX ;quotient -11 will be in AX and remainder -1 in DX
36
BCD Arithmetic
DAA: Decimal Adjust after Addition
operations
44
Shift and Rotate Instructions…
Rotate:
Rotate instructions position binary data by rotating the
information in a register or memory location, either
from one end to another or through the carry
45
Shift and Rotate Instructions…
This figure
shows
The
operation
and
direction of
each rotate
in rotate
instruction
46
III. Program Control Instructions
47
Flag-Control Instructions
48
Jump group instructions
Allows programmer to skip program sections and branch to any part of memory for
the next instruction. (see the illustration below)
LOOP and conditional LOOP are also forms of the jump instruction
Branch/alternate route
49
Unconditional Jump
JMP Des:
This instruction is used for unconditional jump from one place to
another.
There are three types of unconditional jump(JMP) instruction:
1. Short Jump
2. Near Jump
3. Far Jump
Short and near jumps are intra-segment jumps(the jump which are
always between statements in the same code segment)
Far jump is inter-segment jump or jumps which can transfer control to
a statement in a different code segment.
50
Unconditional Jump
1. Short Jump
This instruction is used for unconditional jump from one place to
another.
Called relative jumps because they can be moved, with related
software, to any location in the current code segment without a
change.
jump address is not stored with the opcode
a distance, or displacement, follows the opcode
The short jump displacement is a distance represented by a
1byte(8bit) signed number whose value ranges between +127 and
–128 or the range of short jump is 256 byte(28 )
51
Unconditional Jump
1. Short Jump
52
Unconditional Jump
2. Near Jump
The near jump is similar to the short jump, except that the
distance is farther
53
Unconditional Jump
3. Far Jump
A far jump instruction obtains
a new segment and offset
address to accomplish the
jump
The far jump instruction
sometimes appears with the
FAR PTR directives
Another way to obtain a far
jump is to define a label as a
far label. A label is far only if it
is external to the current code
segment or procedure (i.e the
compiler sets it automatically)
Example:
JMP 0127: A300
Jump to CSX10+IP = A300X10+0127 = A3127 54
Conditional Jump
58
Conditional LOOPs
LOOP instruction also has conditional forms: LOOPE and LOOPNE
LOOPE (loop while equal) instruction jumps if CX != 0 while an equal
condition exists.
will exit loop if the condition is not equal or the
CX register decrements to 0
LOOPNE (loop while not equal) jumps if CX != 0 while a not-equal condition
exists.
will exit loop if the condition is equal or the CX register decrements to 0
In 80386 - Core2 processors, conditional LOOP can use CX or ECX as the
counter.
LOOPEW/LOOPED or LOOPNEW/LOOPNED override the instruction
mode if needed
Under 64-bit operation, the loop counter uses RCX and is 64 bits in width
Alternates exist for LOOPE and LOOPNE.
LOOPE same as LOOPZ
LOOPNE instruction is the same as LOOPNZ
In most programs, only the LOOPE and LOOPNE apply.
59
CONTROLLING THE FLOW OF THE PROGRAM
Easier to use assembly language statements .IF, .ELSE, .ELSEIF, and .ENDIF to control
the flow of the program than to use the correct conditional jump statement.
these statements always indicate a special assembly language command to MASM
Control flow assembly language statements beginning with a period available to
MASM version 6.xx, and not to earlier versions.
Other statements developed include .REPEAT–.UNTIL and .WHILE–.ENDW.
the dot commands do not function using the Visual C++ inline assembler
Never use uppercase for assembly language commands with the inline assembler.
some of them are reserved by C++ and will cause problems
WHILE Loops
Used with a condition to begin the loop.
the .ENDW statement ends the loop
The .BREAK and .CONTINUE statements are available for use with the while loop.
.BREAK is often followed by .IF to select the break condition as in .BREAK .IF AL == 0DH
.CONTINUE can be used to allow a DO–.WHILE loop to continue if a certain condition is met
The .BREAK and .CONTINUE commands function the same manner in C++.
REPEAT-UNTIL Loops
A series of instructions is repeated until some condition occurs.
The .REPEAT statement defines the start of the loop.
end is defined with the .UNTIL statement, which contains a condition
An .UNTILCXZ instruction uses the LOOP instruction to check CX for a repeat loop.
.UNTILCXZ uses the CX register as a counter to repeat a loop a fixed number of times
60
PROCEDURES
A procedure is a group of instructions that usually performs one task.
subroutine, method, or function is an important part of any system’s architecture
A procedure is a reusable section of the software stored in memory once, used as
often as necessary.
saves memory space and makes it easier to develop software
Disadvantage of procedure is time it takes the computer to link to, and return from it.
CALL links to the procedure; the RET (return) instruction returns from the procedure
CALL pushes the address of the instruction following the CALL (return address) on the stack.
the stack stores the return address when a procedure is called during a program
RET instruction removes an address from the stack so the program returns to the instruction
following the CALL.
A procedure begins with the PROC directive and ends with the ENDP directive.
each directive appears with the procedure name
PROC is followed by the type of procedure:
NEAR or FAR
In MASM version 6.x, the NEAR or FAR type can be followed by the USES statement.
USES allows any number of registers to be automatically pushed to the stack and popped from
the stack within the procedure
Procedures that are to be used by all software (global) should be written as far procedures.
Procedures that are used by a given task (local) are normally defined as near procedures.
Most procedures are near procedures.
61
INTRODUCTION TO INTERRUPTS
An interrupt is a hardware-generated CALL
externally derived from a hardware signal
Or a software-generated CALL
internally derived from the execution of an instruction or by some
other internal event
at times an internal interrupt is called an exception
Either type interrupts the program by calling an interrupt service
procedure (ISP) or interrupt handler.
Three different interrupt instructions available:
INT, INTO, and INT 3
Two instructions control the INTR pin.
The set interrupt flag instruction (STI) places 1 in the I flag bit.
which enables the INTR pin
The clear interrupt flag instruction (CLI) places a 0 into the I flag
bit.
Which disables the INTR pin
62
MACHINE CONTROL AND MISCELLANEOUS
INSTRUCTIONS
Controlling the Carry Flag Bit
The carry flag (C) propagates the carry or borrow in multiple-word/doubleword
addition and subtraction.
can indicate errors in assembly language procedures
Three instructions control the contents of the carry flag:
STC (set carry), CLC (clear carry), and CMC (complement carry)
WAIT
If the WAIT instruction executes while the BUSY pin = 1, nothing happens and the next
instruction executes.
pin inputs a busy condition when at a logic 0 level
if BUSY pin = 0 the microprocessor waits forthe pin to return to a logic 1
HLT
Stops the execution of software.
There are three ways to exit a halt:
by interrupt; a hardware reset, or DMA operation
NOP
When the microprocessor encounters a NOP, it takes a short time to execute.
A NOP may also find application in time delays to waste time.
63
.
Questions ?
64