Friday 16 February 2018


CENG 222 – Computer Organization
Lab Work 5

Jump and Labels

JMP instruction allows to “jump” into an address of a instruction in our program. You can make loops by using JMP. Usage of JMP:

JMP <address or label name>

In assembly programs, we generally use labels to mark a starting point of a code. They end with a colon sign (“:”). Example:

L1:
           
            MOV …
            SUB …
           
            JMP L1            ; Jump into the first instruction after the label “L1”.

Testing conditions and Conditional Jump

In order to test a condition, you can use SUB or AND instructions. For example you can test A = B, A > B, A >= B, A < B and A <=B conditions using SUB. You can also test the status of any bit of a register by using AND. These instructions will modify the flag bits according to the results they produce. Therefore, you can make a conditional jump using flag bits’ status. However, SUB and AND instructions also store the result into the destination register. If you just want to test the condition without modifying operands, you need to use CMP and TEST instructions. These instructions are used to modify just the flag bits.

CMP is the same as SUB but it does not modify the destination. TEST is the same as AND but it does not modify the destination. However they both modify the flag bits.
Analyze the code below:

           
            MOV CX, 0
L1:
            CMP CX, 10
            JE L2                           ; Go to L2 if CX = 10
            INC CX
            JMP L1                        ; Go to L1 (CMP instruction)
L2:
           

Some of conditional jump instructions are:

JE, JNE, JZ, JNZ, JG, JNG, JL, JNL, JLE, JGE, JNLE, JNGE

JE:       Jump if equal to
JNE:    Jump if not equal to
JZ:       Jump if zero
JNZ:    Jump if not zero
JG:      Jump if greater than
JNG:   Jump if not greater than
JL:       Jump if less than
JNL:    Jump if not less than
JLE:    Jump if less than or equal to
JGE:    Jump if greater than or equal to
JNLE: Jump if not less than nor equal to (same as JG)
JNGE: Jump if not greater than nor equal to (same as JL)

Please note that conditional jump instructions are useless alone, but if you use an instruction that modifies flag bits, before the jump instruction, you can make IF…THEN…ELSE structures. For example:

            CMP var1, var2           ; Compare var1 and var2
            JG L1                           ; Jump to L1 if var1>var2

                                            ; “IF” block
                                            ; These instructions will be executed if var1<=var2

            JMP L2                        ; End of “IF” block, we need to jump L2
L1:

                                            ; “ELSE” block
                                            ; These instructions will be executed if var1>var2

L2:                                           ; End of “IF…ELSE” block


LOOP instruction

LOOP instruction allows creating a simple loop using CX register as a counter.

Usage:
LOOP <address of label>

You need to initialize CX first for the number of repentance of the loop. Analyze the code below:

            MOV CX, 10              ; Loop 10 times
L1:
           
                                            ; Instructions in the loop
           
            LOOP L1

LOOP instruction simply decrements CX and tests if it is zero. If not zero, then jump. Otherwise, continue.

EXPERIMENTS
1.      Find the sum [1+2+3+4+5+...+10] by using both compare-jump combination and loop instruction.
2.      Find the sum [1+3+5+..+17] by using both compare-jump combination and loop instruction.
3.      Find the sum of numbers between 0 and 20 that are not divisible by 4
4.      Write a program to find the factorial of the number given in AL register. (Assume that the number is sufficiently small so that the factorial wont be larger than 32 bits)

No comments:

Post a Comment