Tuesday, May 15, 2018

Lab 03: Program Flow Control Instructions

Lab 03: Program Flow Control Instructions
OBJECTIVE
To learn to change the sequence of execution of a program by using program flow control instructions.
A. Flag Registers:
The flag register is a special 16-bit register with individual bit positions assigned to show the status of CPU or the result of arithmetic operations. Each relevant bit position is given a name other positions are undefined:
There are two basic types of flags: control flag and status flags
Control Flag: Individual bits may be sent in the flag register by the programmer to control the CPU’s operation. These are Direction, Interrupt and Trap flags
Status Flags: reflect the outcome of an arithmetic or logical operation performed by the CPU. These are overflow, carry, sign, zero, auxiliary carry and parity flags
1.    Overflow: The flag is set when the signed result of an arithmetic operation may be too large to fit into the destination area.
1= OV, (Overflow) and 0= NV, (No Overflow)
2.    Carry: This flag is set when the result of an arithmetic operation produces a carry or a borrow has been generated out of the MSB.
1= CY, (Carry) and 0= NC, (No carry)
3.    Sign: set when the result of an arithmetic or logical operation is negative
1= NG, (Negative) and 0= PL, (Positive)
4.    Zero: set when an arithmetic or logical operation results in zero
1=ZR, (Zero) and 0= NZ, (Not Zero)
5.    Auxiliary Carry: It indicates when an arithmetic carry or borrow has been generated out of the four LSB bits, or lower nibble.
1= AC, (Auxiliary Carry) and 0= NA, (No Auxiliary Carry)
6.    Parity Flag: reflects the number of bits in the result of an operation that are set
PE = 1 (Even number of bits changed) and PO = 0 (Odd number of bits changed)
Note: The Status of these flags can be used to make a decision to change the program flow control. The decision can be either TRUE of FALSE. Based on this decision the program executes/jumps to a certain line of code.
B. Program Flow Control
Controlling the program flow is a very important thing, this is where your program can make decisions according to certain conditions.
1.    Unconditional Jumps
2.    Conditional Jumps
1. Unconditional Jumps:
The basic instruction that transfers control to another point in the program is JMP.
The basic syntax of JMP instruction:
Syntax: JMP label
To declare a label in your program, just type its name and add ":" to the end, label can be any character combination but it cannot start with a number, for example here are 3 legal label definitions:
label1:
label2:
a:
Label can be declared on a separate line or before any other instruction, for example:
x1:
MOV AX, 1
x2: MOV AX, 2
Here is an example of JMP instruction:
main proc
    mov ax,5          ;set AX to 5
    mov bx,2                    ;set BX to 2
    jmp calc            ;go to ‘Calc’
    back:
    jmp stop           ;go to ‘Stop’
    calc:
    add ax,bx          ;Add BX to AX
    jmp back           ;go to ‘Back’
    stop:
main endp
Another example:
main proc                    
    jmp label1  ;go to 'label1'
    mov ah,2
    mov dl,'k'
    int 21h
        label1:
    mov ah,2
    mov dl,'p'
    int 21h
main endp
Short Conditional Jumps
Unlike JMP instruction that does an unconditional jump, there are instructions that do a conditional jumps (jump only when some conditions are in act). These instructions are divided in three groups, first group just test single flag, second compares numbers as signed, and third compares numbers as unsigned.
Instruction
Operands
Description








CMP






REG, memory
memory, REG
REG, REG
memory, immediate
REG, immediate
Compare.
Algorithm:
operand1 - operand2
result is not stored anywhere, only flags affected
If
operand1 - operand2 > 0    ZF=0, Sign=0
operand1 - operand2 < 0    ZF=0, Sign=1
operand1 - operand2 = 0    ZF=1
Example:
MOV AL, 5
MOV BL, 5
CMP AL, BL  ; ZF = 1 (so equal)

C
Z
S
O
P
A
r
r
r
r
r
r






JE






Label








Short Jump if first operand is Equal to second operand (as set by CMP instruction). Signed/Unsigned.
Algorithm:
if ZF = 1 then jump
Example:
   include 'emu8086.inc'
   main proc
   MOV AL, 5
   CMP AL, 5
   JE  label1
   PRINT 'AL is not equal to 5.'
   JMP exit
label1:
   PRINT 'AL is equal to 5.'
exit:
  
C
Z
S
O
P
A
unchanged







JG







Label

Short Jump if first operand is Greater then second operand (as set by CMP instruction). Signed.
Algorithm:
if (ZF = 0) and (SF = OF) then jump
Example:
   include 'emu8086.inc'
   ORG 100h
   MOV AL, 5
   CMP AL, -5
   JG  label1
   PRINT 'AL is not greater -5.'
   JMP exit
label1:
   PRINT 'AL is greater -5.'
exit:
  
C
Z
S
O
P
A
unchanged







JGE






Label
Short Jump if first operand is Greater or Equal to second operand (as set by CMP instruction). Signed.
Algorithm:
if SF = OF then jump
Example:
   include 'emu8086.inc'
   ORG 100h
   MOV AL, 2
   CMP AL, -5
   JGE  label1
   PRINT 'AL < -5'
   JMP exit

label1:
   PRINT 'AL >= -5'
exit:
  
C
Z
S
O
P
A
unchanged







JNE






Label
Short Jump if first operand is Not Equal to second operand (as set by CMP instruction). Signed/Unsigned.
Algorithm:
if ZF = 0 then jump
Example:
   include 'emu8086.inc'

   ORG 100h
   MOV AL, 2
   CMP AL, 3
   JNE  label1
   PRINT 'AL = 3.'
   JMP exit
label1:
   PRINT 'Al <> 3.'
exit:
  
C
Z
S
O
P
A
unchanged






JZ






Label
Short Jump if Zero (equal). Set by CMP, SUB, ADD, TEST, AND, OR, XOR instructions.
Algorithm:
if ZF = 1 then jump
Example:
   include 'emu8086.inc'
   ORG 100h
   MOV AL, 5
   CMP AL, 5
   JZ  label1
   PRINT 'AL is not equal to 5.'
   JMP exit
label1:
   PRINT 'AL is equal to 5.'
exit:
  
C
Z
S
O
P
A
unchanged

More Conditional Jumps:

JA
JAE
JB
JBE
JC
JL
JLE
JNA
JNAE
JNBE
JNC
JNG
JNGE
JNL
JNLE
JNO
JNP
JNS
JO
JP
JPE
JPO
JS
JNB
JNZ



C. LOOPS:
The loop instruction is the easiest way to repeat a block of statements a specific number of times. CX is automatically used as a counter and is decremented each time the loop repeats. Its syntax is: loop destination
Instruction
Operands
Description






LOOP






Label
Decrease CX, jump to label if CX not zero. If CX = 0, loop ends.
Algorithm:
  • CX = CX - 1
  • if CX <> 0 then
    • jump
else
    • no jump, continue
Example:
   include 'emu8086.inc'

   main proc
   MOV CX, 5
label1:
   PRINT 'loop!'
   LOOP label1
   Main endp
C
Z
S
O
P
A
unchanged

More Loops:

LOOPE
LOOPNE
LOOPNZ
LOOPZ


Lab Code: Count the entered characters before user presses space bar.

.model small
.stack 100h
.data
.code
main proc
    mov cl,0 ; set counter to zero
    label1:
    mov ah,1
    int 21h
    cmp al,' ' ; this is single space showing ASCII code for space bar
          ;         cmp al,0dh ; this is the lab task
    je label2
    inc cl
    jmp label1
    label2:
    mov ah,2    ;display the count
    mov dl,cl
    add dl,30h ; ASCII conversion
    int 21h
    main endp
end main

Program Flow Control:
Input
                                                     F
          Compare       with spacebar
     T
Display
Lab Task: Count the entered characters before user presses Enter key.
Hint: The ASCII code for ENTER key is 0dh


THE END