Tuesday, 21 July 2015

C style Conditional statement in Assembly language !

The Conditional statements is used o express decisions.In its most basic form, a decision is some sort of branch within the code that switches between two possible execution paths based on some condition.Normally (though not always), conditional instruction sequences are implemented with the conditional jump instructions.
Conditional instructions in HLL like C/C++ are:
1. if-else
2. switch
3. else-if




                                         
      
              


For example:
if(expression)
    statement1
else
    statement2

Here, the expression is evaluated; if is true, statement1 is executed. if it is false and there is an else part(else part is optional), statement2 is executed.

While Conditional instructions in Assembly are:
Instruction                   Jump Condition                           Test
JE                               Jump if Equal                               ZF=1
JNE                         Jump if Not Equal                        ZF=0
JG                            Jump if Greater                            ( ZF=0) AND (SF=OF)
JGE                           Jump if Greater or Equal              SF=OF
JL                           Jump if Less                                 SF?OF
JLE                      Jump if Less or Equal                  (ZF=1) OR (SF?OF)

Let us discuss the CMPL instruction before discussing the conditional instructions in Assembly.

cmpl Instruction: The CMPL instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making.

Consider the following C/C++ statement:
if(a == b)
{
    /* True Branch Code Here */
}
else
{
    /* False Branch Code Here */
}
/* At This Point, Reconverge */

A approach to converting this statement into assembly language produce:
movl a, %eax
movl b, %ebx

cmpl %eax, %ebx

je true_branch

jmp reconverge

true_branch:
    #True Branch Code Here
reconverge:
    #recoverge to this point

In the first two lines,we move a and b into registers for comparison.In the next line, we compare the registers eax and ebx. Or in other words, we simply compare the a and b.After comparing a and b,In the next instruction we check if a and b are equal   or not. if a and b are equal, then jump to label true_branch. After executing true_branch's code,the true_branch recoverge to reconverge label.
As you can see, since assembly language is linear, the blocks have to jump around each other. Recovergence is handled by the programmer, not the system.

Reference: 1. Implementing Common Control Structures in Assembly Language 
2. Programming from ground up.

If you like this post or have any question, please feel free to comment!

No comments:

Post a Comment

Blogger Widget