1

This Assembly code:

cmp [Variable1], 10
jae AlternateBlock
call SomeFunction
jmp AfterIfBlock
cmp [Variable1], 345
jne AfterIfBlock
call SomeOtherFunction

equals to this C code?:

if (variable1 >= 10)
{
    goto AlternateBlock;
    SomeFunction();
    goto AfterIfBlock;
}
else if (Variable1 != 345)
{
    goto AfterIfBlock;
    SomeOtherFunction();
}

3 Answers 3

5

More succinctly:

if( variable1 < 10 ) {
  SomeFunction();
} else if( variable1 == 345 ) {
  SomeOtherFunction()
}

Explanation:

cmp [Variable1], 10
jae AlternateBlock     ; if variable1 is >= 10 then go to alternate block
call SomeFunction      ; else fall through and call SomeFunction(). ie. when variable1 < 10
jmp AfterIfBlock       ; prevent falling through to next conditional
cmp [Variable1], 345
jne AfterIfBlock       ; if variable1 is not equal to 345 then jump to afterifblock
call SomeOtherFunction ; else fall through to call SomeOtherFunction

If you take some time to understand it you should see it's semantically equivalent to the C code. Perhaps this helps.

cmp [Variable1], 10
jb @f
call SomeFunction
jmp end
  @@:
cmp [Variable1], 345
jnz end
call SomeOtherFunction
  end:
Sign up to request clarification or add additional context in comments.

Comments

4

No, it's probably more like this:

if (variable1 < 10)
    SomeFunction();
else if (Variable1 == 345)
    SomeOtherFunction();

But you've not included the labels in your assembler so I can't be sure. I've assumed the labels are like this:

    cmp [Variable1], 10
    jae AlternateBlock
    call SomeFunction
    jmp AfterIfBlock
@@AlternateBlock:
    cmp [Variable1], 345
    jne AfterIfBlock
    call SomeOtherFunction
@@AfterIfBlock:

1 Comment

@Mike Yes I've just spotted that when checking through again. Thanks.
0

No, it's not. If variable1 is less than 10, the assembly code will call SomeFunction, and the C code will not, it will jump to AlternateBlock

6 Comments

But why is it; if variable1 < 10, jae isn't jump if above or equal?
@Thanos: Yes, so bacause variable is less than 10, it will NOT jump anywhere and will go to the next statement, which is call someFunction
I mean that JAE isn't if (>=)?
@Thanos JAE is indeed jump if greater than or equals. Think about it!
Oh yes, now I understood, I have been stuck for a moment, thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.