Uploaded by Ayhan AbdulAziz

LEC#6-PROGRAM CONTROL TRANSFER-UP2021

advertisement
LEC # 6
PROGRAM CONTROL TRANSFER
INSTRUCTION GROUP
10/16/2020
Raafat S Habeeb
1
Objective:
Upon completion of this lecture the student will be able to
1- Use both conditional and unconditional jump instructions to
control the flow of a program.
2- Use the CALL and RET instructions to include procedures in the
program structure.
3- Write a structure program using module technique
10/16/2020
Raafat S Habeeb
2
Program Control Instructions
Branch Instructions:
 These instructions cause change in the sequence of the execution





of instruction.
This change can be conditional and unconditional.
The conditions are represented by flags.
A conditional jump instructions are based upon numerical tests of
the flag bits.
If control is transferred to a memory location within the current
code segment, it is NEAR. This is called Intrasegment. Only IP
register must be updated.
If control is transferred outside the current code segment, it is
FAR. This is called Intersegment jump CS and IP registers must be
updated.
10/16/2020
Raafat S Habeeb
3
Branch Instructions:
Conditional Jumps
• Conditional jump instructions can be divided into:
1. Jumps based on comparisons of unsigned operands: JA,
JB, JAE, JBE, JE, and JNE instructions.
2. Jumps based on comparisons of signed operands: JG, JL,
JGE, JLE, JE, and JNE instructions.
Note:
– Above and below are used with unsigned numbers
– Greater and less are used with signed numbers
10/16/2020
Raafat S Habeeb
4
Conditional Jumps
. The 8086 Conditional Jump Instructions are:
Mnemonic
"Jump IF …"
Condition Tested
JLE/JNG
((SF xor OF) or ZF) = 1
less or equal/not greater
JNC
CF = 0
not carry
JNE/JNZ
ZF = 0
not equal/ not zero
JNO
OF = 0
not overflow
JNP/JPO
PF = 0
not parity/ parity odd
JNS
SF = 0
not sign
JO
OF = 1
overflow
JP/JPE
PF = 1
parity/ parity equal
JS
SF = 1
sign
Raafat S Habeeb
5
Conditional Jumps
Mnemonic
"Jump IF …"
Condition Tested
JA/JNBE
(CF = 0) and (ZF = 0)
above/ not below nor zero
JAE/JNB
CF = 0
above or equal/ not below
JB/JNAE
CF = 1
below/ not above nor equal
JBE/JNA
(CF or ZF) = 1
below or equal/ not above
JC
CF = 1
carry
JE/JZ
ZF = 1
equal/ zero
JG/JNLE
((SF xor OF) or ZF) = 0
greater / not less nor equal
JGE/JNL
(SF xor OF) = 0
greater or equal/ not less
JL/JNGE
(SF xor OF) = 1
less /not greater nor equal
10/16/2020
Raafat S Habeeb
6
Conditional Jumps
 Control is transferred to a new memory location if a certain




condition is met. The Flag register is one that indicates the
current condition.
All conditional jumps are SHORT jumps. The target address
must be within -128 (backward) to + 127 (forward) bytes of the
IP.
The conditional jump is a two-byte instruction; one op-code and
the other is a value between 00 to FF (offset address range).
In a backward jump, the second byte is the 2’s complement of
the displacement value. The target address = IP of the
instruction after the jump instruction + the second byte value.
Similarly, in a forward jump, the target address = IP of the
following instruction + the second byte value.
Raafat S Habeeb
7
Branch Instructions:
1- Unconditional JUMP
10/16/2020
Raafat S Habeeb
8
2 Byte instruction
Short Jumps:
Offset address represent displacement of the address, it is 1 byte long ,
so this type of jump allows branch to memory location within +127
( forward) to – 128 (backward) bytes from the address following the
jump
JMP(OPCODE)
Offset Address
Byte 1
Byte 2
10000
JMP
CS: 1000h
10001
04
IP: 0002h
10002
New IP = IP+04
10003
10004
10005
10006
10/16/2020
JUMP HERE
Raafat S Habeeb
9
Example of a Forward Jump
0005
0008
000A
000C
000E
0010
0012
8A
3C
72
3C
77
24
88
47 02
61
06
7A
02
DF
04
AGAIN:
NEXT:
MOV
CMP
JB
CMP
JA
AND
MOV
AL,[BX]+2
AL,61H
NEXT
AL,7AH
NEXT
AL,0DFH
[SI],AL
 The NEXT label address is(000CH+0006H=0012).
 The target address is 6 bytes from the IP of
the next instruction.
Raafat S Habeeb
10
Example of a Backward Jump
1067:0000
1067:0003
1067:0005
1067:0008
1067:000D
1067:000F
1067:0010
1067:0011
1067:0013
1067:0016
1067:0018




B8
8E
B9
BB
02
43
49
75
A2
B4
CD
66 10
D8
05 00
00 00
07
NEXT:
FA
05 00
4C
21
MOV
MOV
MOV
MOV
ADD
INC
DEC
JNZ
MOV
MOV
INT
AX,1066H
DS,AX
CX,0005
BX,0000
AL,[BX]
BX
CX
NEXT
[0005],AL
AH,4C
21
The jump or label address is(0013+FA=000D).
FA is the 2’s complement of -6.
The target address is -6 bytes from the IP of the next instruction.
In general:
displacement=targt address-(Jump address+2)
Raafat S Habeeb
11
10/16/2020
Raafat S Habeeb
12
Near jump:
3 BYTES INSTRUCTION
Displacement Address is 2 byte long, so this allows a branch or jump
within ± 32Kbyte ( i.e any where in the code segment)
JMP ( OPCODE)
Byte1
10000
10001
10002
10003
10004
10005
10/16/2020
DSP LOW
ADDRESS
Byte 2
JMP
02
00
DSP HIGH
ADDRESS
Byte 3
CS = 1000h
IP = 0003
New IP =IP+02
JUMP HERE
Raafat S Habeeb
13
10/16/2020
Raafat S Habeeb
14
10/16/2020
Raafat S Habeeb
15
2 - Conditional Jump instructions
These instructions test a specific flag, if it is true then jump to label otherwise continue
As you can see there are some instructions that do that same thing, that's correct, they
even are assembled into the same machine code, so it's good to remember that when
you compile JE instruction - you will get it disassembled as: JZ.
Instruction
Description
Condition
JZ , JE
Jump if Zero (Equal).
ZF = 1
JC
Jump if Carry
CF = 1
JS
Jump if Sign.
SF = 1
JO
Jump if Overflow.
OF = 1
JPE, JP
Jump if Parity Even.
PF = 1
JNZ , JNE
Jump if Not Zero (Not
Equal).
ZF = 0
JNC
Jump if Not Carry
CF = 0
JNS
Jump if Not Sign.
SF = 0
JNO
Jump if Not Overflow.
OF = 0
JPO, JNP
Jump if Parity Odd (No
Parity).
PF = 0
10/16/2020
Raafat S Habeeb
16
3- Iteration Instruction:
10/16/2020
Raafat S Habeeb
17
Unconditional LOOP Instruction
LOOP
Example:
MOV
MOV
NEXT: MOV
INC
MOV
INC
LOOP
Dest
BX, OFFSET ARRAY ; Point BX at first element in array
CX, 40
; Load CX with number of elements
AL, [BX]
; Get element from array
AL
; Increment the content of AL
[BX], AL
; Put result back in array
BX
; Increment BX to point to next location
NEXT
; Repeat until all elements adjusted
 if CX ≠ 0, it jumps to the address indicated by the label
 If CX becomes 0, the next sequential instruction executes
Raafat S Habeeb
18
CALL Statement
 CALL instruction is used to call a procedure.
 It is used to perform tasks that need to be performed frequently.
 It makes programs more structured.
 CALL may be NEAR (i.e. the target address in the current segment) or FAR
(i.e. the target address out the current segment).
 The following is a NEAR CALL example: (different IP, same CS)
12B0:0200 BB 12 95
MOV
12B0:0203
E8 FA 00
12B0:0206
B8 2F 14
BX, 9512
CALL 0300
MOV
AX, 142F
 Displacement = Target address – (CALL address + 3)
Raafat S Habeeb
19
CALL Statement
 The IP address of the instruction after the CALL is saved on the stack as
shown in the following figure.
 IP will be 0206, which belongs to the “MOV AX, 142F” instruction.
 A RET instruction directs the CPU to POP the top 2 bytes of the stack into
the IP and resume executing at offset address 0206.
 For every PUSH there must be a POP.
12B0:0300
12B0:0301
... ...
12B0:0309
12B0:030A
53
...
...
5B
C3
PUSH BX
...
...
... ...
POP BX
RET
Raafat S Habeeb
20
4- Procedure (SUBROTINE)
CALL instruction
IP >> STACK SP = SP+2
Subroutine Address >>> IP
10/16/2020
Raafat S Habeeb
21
4- Procedure (SUBROTINE) Continue
RET instruction
TOP of STACK >> IP
10/16/2020
SP = SP-2
Raafat S Habeeb
22
CALL Statement
 CALL
 M[SS:SP-1]  IP(H)
 M[SS:SP-2] IP(L)
 SP  SP-2
 RET
 IP(L)  M[SS:SP]
 IP(H)  M[SS:SP+1]
 SP  SP+2
Raafat S Habeeb
23
Example: Factorial
CODE
SEGMENT
ASSUME CS:CODE, DS:CODE
ORG
100H
START: MOV
AX, CS
MOV
DS, AX ; Initialize DS=CS
MOV
BX, 03 ; Number to find its factorial
CALL
FACT
HLT
; Procedure to find factorial of a number
; Input data in BX register. Result in AX register
FACT:
MOV
AX,01
CMP
BX,0
; Factorial of 0 is 1
JZ
RETN
NEXT:
MUL
BX
DEC
BX
JNZ
NEXT
RETN: RET
CODE
ENDS
END
START
Raafat S Habeeb
24
Type of Procedures:
A- Simple Procedure
STACK
MAIN
PROGRAM
MP
U
J o
RE
t
U
ED
C
O
PR
CALL
PROCEDURE
SUB1:
RE
MAIN
PROGRAM
10/16/2020
TU
MA To RN
IN
PR
OG
Raafat S Habeeb
EQU 4000H
ORG 00H
MOV SP,STACK
------------------------------------------PUSH reg.
CALL SUB1
POP reg.
---------------------------------------------------------HLT
=========
=========
=========
=========
RET
END CODE
25
B-Nested Procedure
STACK
MAIN
PROGRAM
P
M
JU o E 1
t R
DU
E
C
O
PR
CALL 1
PROC1:
CALL 2
PROCEDURE 2
RE
MAIN
PROGRAM
P
M
JU o E 2
t R
DU
E
C
O
PR
TU
MA To RN
IN
PR
OG
RE
PR
PROC2:
TU
RN
t
OC o
ED
UR
E
1
PROC3:
10/16/2020
Raafat S Habeeb
EQU 4000H
ORG 00H
MOV SP,STACK
------------------------------------------PUSH reg.
CALL PROC1
POP reg.
--------------------------------------HLT
=========
=========
PUSH reg
CALL PROC2
POP reg
=========
RET
=======
=======
PUSH reg
CALL PROC3
POP reg
=======
RET
========
========
========
RET
26
Parameter passing:
Parameters can be passed between main program and
procedure in four methods
1-In Register
2- In Memory location
3- With pointers in Register
4- In stack
10/16/2020
Raafat S Habeeb
27
1- Passing Parameter in Register:
The following program example explain this method of
program compute square of data
parameters passing. The
; Program to compute square of data Passing parameter in Register
.MODEL TINY
0000
CODE SEGMENT
ASSUME CS:CODE,DS:CODE
0100
ORG 100H
0100 0156
VALUE DW
0156H
0102 0002
SQUARE DW
2 DUP(0)
= 4000
TOP_OF_STACK EQU 4000H
;
ORG 00H
MOV AX,CS
;
MOV DS,AX
MOV SP,TOP_OF_STACK
; SP =4000h
MOV AX,VALUE
; AX=0156h
CALL MYSQUARE
MOV SI,OFFSET SQUARE
; SI= 0102h
MOV [SI],AX
;
0000
0000 8C C8
START:
0002 8E D8
0004 BC 4000
0007 A1 0100 R
000A E8 001A R
000D BE 0102 R
0010 89 04
(square)=Al,(Square+1)=Ah
0012 46
0013 46
0014 89 14
0016 B4 4C
0018 CD 21
10/16/2020
INC SI
INC SI
MOV [SI],DX
MOV AH,4CH
INT 21H
Raafat S Habeeb
;(Square+2)=Dl,(Square+3)=Dh
28
Passing Parameter in Register
001A
001B
001D
001E
9C
F7 E0
9D
C3
001F
MYSQUARE:
CODE
PUSHF
MUL AX
POPF
RET
;
( Continue)
;GET BACK FLAGS
ENDS
END START
10/16/2020
Raafat S Habeeb
29
2- Passing Parameter in Memory
; PROGRAM TO COMPUTE SQUARE PASSING PARAMETER IN MEMORY
;
START:
;
MAQUAR:
CODE
10/16/2020
MOV AX,CS
MOV DS,AX
MOV SP,TOP_OF_STACK
CALL MSQUAR
MOV AH,4CH
INT 21H
; DEFINE SEGMENT
PUSHF
PUSH AX
MOV AX,VALUE
MUL AX
MOV SI,OFFSET SQUARE
MOV [SI],AX
INC SI
INC SI
MOV [SI],DX
POP AX
POPF
RET
ENDS
END START
; SAVE FLAGS IN STACK
;
; GET VALUE OF DATA
; FIND SQUARE
; OFFSET ADDRESS OF SQUARE
; SAVE LSWORD INTO MEMORY
; UPDATE POINTER
Raafat S Habeeb
;INTILIZE SYACK POINTER
; GET SQUARE OF THE DATA
; SAVE MSWORD INTO MEMORY
; GET BACK FLAGS
30
SELF STUDY
From Books
1-THE 80X86 IBM PC AND COPATIBLE COMPUTER V1
BY: Muhammad Ali Mazidi CH 2 Sections: 2.4
OR
2-The Intel Microprocessor BY: Barry B. Brey CH 6
HOME WORK
From Book 1 P 78-80
Problem 9,10,13,15,16
10/16/2020
Raafat S Habeeb
31
Download