preload preload preload preload
Showing posts with label Jump. Show all posts
Showing posts with label Jump. Show all posts

Sunday, January 24, 2010

Assembly Language Part 3

For 8086 programs to perform useful tasks, there must be a way to make decisions and repeat sections of the code. Inthis section we will discuss the the basic flow control instructions of MASM

In assembly program flow is managed by jumping isntructions. There are 2 types of jump

1) Unconditional
             The JMP instruction causes an unconditional transfer of control. The basic form is-
            JMP label

2)Conditional
            The transfer of control depends on the previous instruction. It is often done by CMP. for example
          CMP operand1,operand2
          Jxx    end

Here the jump Jxx can be any of the following -


Jcc Instructions for Signed Comparisons
Instruction
Description
Condition
Aliases
Opposite
JG
Jump if greater (>)
Sign = Ovrflw or Zero=0
JNLE
JNG
JNLE
Jump if not less than or equal (not <=)
Sign = Ovrflw or Zero=0  
JG
JLE
JGE
Jump if greater than or equal (>=)
Sign = Ovrflw
JNL
JGE
JNL
Jump if not less than (not <)
Sign = Ovrflw
JGE
JL
JL
Jump if less than (<)
Sign Ovrflw
JNGE
JNL
JNGE
Jump if not greater or equal (not >=)
Sign Ovrflw  
JL
JGE
JLE
Jump if less than or equal (<=)
Sign Ovrflw or Zero = 1
JNG
JNLE
JNG
Jump if not greater than (not >)
Sign Ovrflw or Zero = 1
JLE
JG
JE
Jump if equal (=)
Zero = 1
JZ
JNE
JNE
Jump if not equal ()
Zero = 0
JNZ
JE






Jcc Instructions That Test Flags
Instruction
Description
Condition
Aliases
Opposite
JC
Jump if carry
Carry = 1
JB, JNAE
JNC
JNC
Jump if no carry
Carry = 0
JNB, JAE
JC
JZ
Jump if zero
Zero = 1
JE
JNZ
JNZ
Jump if not zero
Zero = 0
JNE
JZ
JS
Jump if sign
Sign = 1
-
JNS
JNS
Jump if no sign
Sign = 0
-
JS
JO
Jump if overflow
Ovrflw=1
-
JNO
JNO
Jump if no Ovrflw
Ovrflw=0
-
JO
JP
Jump if parity
Parity = 1
JPE
JNP
JPE
Jump if parity even
Parity = 1
JP
JPO
JNP
Jump if no parity
Parity = 0
JPO
JP
JPO
Jump if parity odd
Parity = 0
JNP
JPE








Jcc Instructions for Unsigned Comparisons
Instruction
Description
Condition
Aliases
Opposite
JA
Jump if above (>)
Carry=0, Zero=0
JNBE
JNA
JNBE
Jump if not below or equal (not <=)
Carry=0, Zero=0
JA
JBE
JAE
Jump if above or equal (>=)
Carry = 0
JNC, JNB
JNAE
JNB
Jump if not below (not <)
Carry = 0
JNC, JAE
JB
JB
Jump if below (<)
Carry = 1
JC, JNAE
JNB
JNAE
Jump if not above or equal (not >=)
Carry = 1
JC, JB
JAE
JBE
Jump if below or equal (<=)
Carry = 1 or Zero = 1
JNA
JNBE
JNA
Jump if not above (not >)
Carry = 1 or Zero = 1
JBE
JA
JE
Jump if equal (=)
Zero = 1
JZ
JNE
JNE
Jump if not equal ()
Zero = 0
JNZ
JE




 As we have seen earlier that the conditional jump isntruction is often followed by the CMP instruction. Wnen CPU executes the CMP instruction it compares the first operand with the second operand. Then if the condition described by the following jump instruction is true - a change of flow control is occured. You may want to see the example -

       CMP AX,BX
       JL  axislower    ;  If AX is lower than BX then control transferred to axislower

The TEST instruction
    The TEST instruction performs an AND operation, but does not store
the result. It only sets the FLAGS register based on what the result would
be (much like how the CMP instruction performs a subtraction but only sets
FLAGS).

TEST DESTINATION, SOURCE


Effect on flags -
SF,ZF, PF - Reflect the result
AF  -  Undefined
CF,OF - 0                    

The test instruction can be used to examine individual bits in a operand. For that case the mask(source) should contain 1s in the desired bit positions and 0s in the rest. See the pseudocode and MASM code -

IF AL is EVEN
   THEN JUMP TO END_X

MASM code-

TEST AL,1
JZ END_X

END_X:
  

If we wish to check the 1st bit we TEST it by 1. (here the mask is 1 i.e = 0000 0001 in binary. And we know that an even number shall have 1 in the 1st bit position.)
The and product should be zero if 1st bit is 0, the AL would then remain same but the ZF (zero flag = 0 if result is zero) is 0. So the JZ instruction shall be executed.

 Further reading:
 The art of Assembly Language             URL:  http://www.arl.wustl.edu/

 The Intel Microprocessor Architecture, Programming & Interfacing
    by  Barry B. Brey

Assembly Language Part 5

Looping instructions in Assembly



What comes to your mind after watching the loop at the left? That's it the Head is Touching the Tail at some point.

In assembly a loop is a sequence of instructions that is repeated like the loop to the left.
The final instruction (Tail) jumps (touches) to the first instruction (Head).

The basic form of looping in Assembly is

REPEAT UNTIL
REPEAT
      STATEMENTS
UNTIL CONDITION

For example if we want to read all input characters given by the user until he/she inputs

    MOV AH,1
REPEAT_:
    INT 21H
    CMP AL,' '
    JNE REPEAT_ 

Some other frequently used looping structures are-


FOR LOOP
In these kind of lop structures the instructions are repeated for known number of times. pseudocode -

FOR loop_count times DO
     statements
END_FOR

In assembly we use the instruction LOOP to implement for loop. The general form is-

LOOP header_label

Here header label is the name or label of the instruction block to be executed in the loop. The LOOP instruction uses CX as the counter so it has to be defined first. When the program executes LOOP it first checks if CX = 0 if true it continues to execute the next instructions. If the CX !=0 then control is transferred to the "header_label" , at the same time it also decreases the value of CX by 1. 

Let us implement it in the program to print 100 '*' in assembly.

MOV CX,80
MOV AH,2
MOV DL,'*'

TOP:                            ; header_label
   INT 21H
   LOOP TOP              



WHILE LOOP
These kind of loops depends on a condition. pseudocode -


WHILE   condition DO
   statements
END_WHILE

The condition is checked at the top of the loop. If the condition is satisfied the the statements are executed. If false  the program skips to whatever follows. It is possible that the condition is never satisfied (even at the initial stage) in that case the body is never executed.

Lets count the number of character in the input line.

MOV DX,0
MOV AH,1
INT 21H

WHILE_:
     CMP AL,13
     JE END_WHILE
     INC  DX
     INT 21H
     JMP WHILE_

END_WHILE:


The basic difference between WHILE and REPEAT loop is that using WHILE loop gives opportunity to skip the whole process even at the initial stage if the condition is false.
On the other hand the statements of REPEAT has to be done atleast once. REPEAT is useful on other cases as it leads to shorter coding.

Further reading:
Assembly Language Programming and Organization of IBM PC
                                                                    Ytha Yu
                                                                    Charles Marut
http://www.cs.princeton.edu/