Download Program Control Instructions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Khaled A. Al-Utaibi
[email protected]







Introduction
Jump Instructions
Unconditional Jump Instruction
Conditional Jump Instructions
Loop Instructions
Procedures
Program Structures



The program control instructions direct the flow
of a program and allow the flow to change.
A change in flow often occurs after a decision
made with the instructions like CMP and TEST
instruction and is followed by conditional jump
instructions.
These slides explain the program control
instructions, including the jumps, calls, and
returns.

Jump instructions are classified into two groups:
−(1) Conditional Jumps
−(2) Unconditional Jumps



The main program control instruction, jump (JMP),
allows the programmer to skip sections of a program
and branch to any part of the memory for the next
instruction.
A conditional jump instruction allows the
programmer to make decisions based upon
numerical tests.
The results of numerical tests are held in the flag
bits, which are then tested by conditional jump
instructions.

The general format of the JMP (Unconditional Jump)
instruction is
−JMP label
−Where label is a program address identifier.





A label is any valid assembler identifier followed by a
colon.
Any instruction statement can be prefixed by a label.
The assembler treats a reference to a label as a
reference to the address of the instruction to which
that label is affixed (See Figure 1).
A label can also prefix a comment statement of can
appear on a line by itself (See Figures 2 and 3).
In such cases, labels are treated as references to the
address of the first instruction following the label.
(a)
(b)
(c)
ax_zero:
ax_zero:
JMP
.
.
MOV
.
.
ax_zero
AX,
0
JMP
ax_zero
.
.
; a comment
MOV
AX,
0
.
.
JMP
.
.
ax_zero
MOV
.
.
AX,
ax_zero:
0
Figure 1: Examples of (a) a label prefixing an instruction, (b) a
label prefixing a comment, (c) a label prefixing an empty line.

The conditional jump instructions test the
following flag bits:
−sign (SF),
−zero (ZF),
−carry (CF),
−parity (PF), and
−overflow (OF).


If the condition under test is true, a branch to
the label associated with the jump instruction
occurs.
If the condition is false, the next sequential step
in the program executes.

The general format of a conditional jump instruction
is
−Jxxx label
−Where:
 xxx describes the condition for which you are testing
 label is a program address identifier.



The syntax of the 8086 assembly language supports
31 conditional jump instructions (See Table 1).
15 of those instructions test a status flag or a
combination of status flags and jump to the address
indicated by label if the condition is true.
Another 15 conditional jumps are the converses of
the first 15; they force a jump if the condition is
false.
Table 1: List of 8086 conditional jump instructions.







When signed numbers are compared, use the JG, JL, JGE,
JLE, JE, and JNE instructions.
The terms greater than and less than refer to signed
numbers.
When unsigned numbers are compared, use the JA, JB,
JAB, JBE, JE, and JNE instructions.
The terms above and below refer to unsigned numbers.
The conditional jump instructions all test flag bits except
for JCXZ (jump if CX = 0)
Instead of testing flag bits, JCXZ directly tests the
contents of the CX register without affecting the flag bits
For the JCXZ instruction, if CX = 0, a jump occurs, and if
CX != 0, no jump occurs.


Conditional jump instructions are used together
with instructions that affect the status flags (CF,
OF, PF, SF, and ZF) and those which affect the CX
register.
These instructions include:
−Arithmetic Instructions (e.g., ADD, SUB, MUL, DIV, CMP,
INC, DEC)
−Logical Instructions (e.g., AND, OR, XOR, NOT, NEG)


Example 1: Write an assembly code to compare
two variables V1 and V2 and store the larger into
AH and the smaller into AL.
Solution: See Figure 2
data segment
v1 db 3
v2 db 7
ends
;
;
;
;
code segment
start:
lea ax, data
mov ds, ax
mov ah, v1
mov al, v2
cmp ah, al
ja done
xchg ah, al
done:
nop
ends
end start
; start code segment
;
;
;
;
;
;
;
;
;
;
start data segment
v1 = 3
v2 = 7
end data segment
load effective address of data segment
set ds = data segment
load v1 into ah
load v2 into al
compare the two numbers
if (ah > al) then goto done
else exchange ah & al
do nothing
end code segment
end program
Figure 2: Assembly code for Example 1.

The general format of a loop instruction is

The LOOP instruction can be used to count the iterations
of a loop.
The LOOP instruction is equivalent for the two
instructions

−LOOP label
−Where label is a program address identifier.
−DEC CX
−JNZ label



The major distinction between the above two
instructions and the LOOP instruction is that the later
does not affect the states the status flags.
The LOOP decrements the contents of the CX register and
if the result is not zero, directs program flow to the
instruction at address referenced by label.
Conversely, if the result of decrementing CX is zero,
program flow fall through to the next instruction.


Example 2: Write an assembly code to find the
largest value in a given array on n elements
(assume n = 5).
Solution: See Figure 3
data segment
a db 3
db 5
db 1
db 7
db 2
ends
; start data segment
; array a of 5 values
code segment
start:
lea ax, data
mov ds, ax
mov cx, 5
mov bx, 0
mov al, 0
next:
mov ah, a[bx]
inc bx
cmp ah, al
jbe skip
mov al, ah
skip:
loop next
ends
end start
; start code segment
; end data segment
;
;
;
;
;
;
;
;
;
;
;
;
;
load effective address of data segment
set ds = data segment
set cx = loop count
set bx = index of 1st element (0)
set al = 0 (assume al is the max value)
load a[bx] into ah
increment bx (next element index)
compare ah and al
if (ah <= al) then skip
else set al = ah
loop
end code segment
end program
Figure 3: Assembly code for Example 2.

The 8086 supports a couple of variations on the
LOOP instruction:
−LOOPZ (Loop While Zero)
−LOOPNZ (Loop While Not Zero)
−LOOPE (Loop While Equal)
−LOOPNE (Loop While Not Equal)

These variations allow program flow to be
controlled by both the CX register and the zero
flag (ZF) simultaneously.





The procedure (subroutine, method, or function) is
an important part of any computer system’s
architecture.
A procedure is a group of instructions that usually
performs one task.
A procedure is a reusable section of the software
that is stored in memory once, but used as often as
necessary.
This saves memory space and makes it easier to
develop software.
The only disadvantage of a procedure is that it takes
the computer a small amount of time to link to the
procedure and return from it.




The CALL instruction links to the procedure, and
the RET (return) instruction returns from the
procedure.
The stack stores the return address whenever a
procedure is called during the execution of a
program.
The CALL instruction pushes the address of the
instruction following the CALL (return address)
on the stack.
The RET instruction removes an address from the
stack so the program returns to the instruction
following the CALL.

A procedure can be defined as shown in Figure 3.
−The beginning of a procedure is defined with a PROC
(PROCedure) directive.
−Each procedure must be assigned a name, by which it
can be called from elsewhere in a program.
−The ending of a procedure is defined with an ENDP
(ENDProcedure) directive.
−Between the PROC and ENDP directives you can write
the body of your procedure.
procedure_name PROC
; procedure body
procedure_name END
Figure 4: Procedure definition.



The CALL instruction is used to invoke the code in a
subroutine.
The general format of a CALL instruction is
−CALL procedure_name
The CPU does several things when a CALL instruction is
executed:
−(1) It adjusts the contents of the IP register as if it were about
to execute the next instruction.
−(2) It stores the contents of the IP register in the program stack.
−(3) It adjusts the contents of the IP register again to point to the
1st instruction in the named procedure.


The result is that immediately after processing a CALL
instruction, the CPU begins executing the 1st instruction
in the named procedure.
From there on it continues executing instructions until it
encounters a RET instruction.


A RET(Return) instruction transfers program flow
back from a subroutine to its caller.
The general format of a RET instruction is
−RET

When the CPU encounters a RET instruction, it
perform the following actions.
−(1) It recovers the IP register contents stored in the
program stack by the CALL instruction.
−(2) It puts the restored value into the IP register and
continue execution.

This gets the CPU back to where it was when the
CALL instruction was encountered.

Example 3: Write a procedure to add the
elements of an array a.
−Assume that the sum will be in AX.
−Assume that the number of elements is (n = 5).
−Use BX as an index of the array.
−Use LOOP instruction to accomplish the task.

Solution: See Figure 4
data segment
a dw 3
dw 5
dw 1
dw 7
dw 2
ends
code segment
Sum proc
mov cx, 5
mov ax, 0
mov bx, 0
next:
add ax, a[bx]
inc bx
inc bx
loop next
ret
endp
start:
lea ax, data
mov ds, ax
call sum
ends
end start
; start data segment
; array a of 5 values
; end data segment
; start code segment
;
;
;
;
;
set cx = loop count
set ax = 0 (sum)
set bx = 0 (index of 1st element)
ax = ax + a[bx]
bx = bx + 2 (next element 2 bytes)
; loop
; return to caller
;
;
;
;
;
load effective address of data segment
set ds = data segment
call sum procedure
end code segment
end program
Figure 3: Assembly code for Example 2.
a = b;
mov bx, b
mov a, bx
a++;
a--;
inc a
dec a
a += b;
mov bx, b
add a, bx
a = b / 2;
mov
mov
div
mov
mov
ax, b
bx, 2
bl
ah, 0
a, ax
a = b * 2;
mov
mov
mul
mov
ax, 2
bx, b
bl
a, ax
if (a > b) {
a++;
}
mov ax, a
mov bx, b
_if_
:
cmp ax, bx
jbe _endif_
inc ax
_endif_:
nop
if (a > b) {
a++;
}
if (condition)
ii
mov ax, a
mov bx, b
_if_
:
cmp ax, bx
jbe _endif_
inc ax
_endif_:
nop
if (a > b) {
a++;
}
complemented
ii
condition
mov ax, a
mov bx, b
_if_
:
cmp ax, bx
jbe _endif_
inc ax
_endif_:
nop
if (a > b) {
a++;
}
body
ii
statement
mov ax, a
mov bx, b
_if_
:
cmp ax, bx
jbe _endif_
inc ax
_endif_:
nop
if (ax > bx) {
ax++;
}
you can use
any iilabels
of your choice
_if_
:
cmp ax, bx
jbe _endif_
inc ax
_endif_:
nop
if (a > b) {
a++;
}
else {
b++;
}
mov ax, a
mov bx, b
_if_
:
_else_ :
_endif_:
cmp
jbe
inc
jmp
inc
nop
ax, bx
_else_
ax
_endif_
bx
if (a > b) {
a++;
}
elseif (b > c) {
b++;
}
else {
c++;
}
_if_
:
_elseif_ :
_else_
_endif_
:
:
cmp
jbe
inc
jmp
cmp
jbe
inc
jmp
inc
nop
ax, bx
_elseif_
ax
_endif_
bx, cx
_else_
bx
_endif_
cx
while (a < b) {
a++;
}
_while_
:
_endwhile_:
cmp
jae
inc
jmp
nop
ax, bx
_endwhile_
ax
_while_
for (i = 3; i < 10; i++) {
a += i;
}
_next_
:
mov si, 3
mov cx, 7
mov ax, a
add ax, si
inc si
loop _next_
; 10 – 3 = 7