Thanos Papas
Thanos Papas

Reputation: 11

Assembly to C code

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();
}

Upvotes: 1

Views: 640

Answers (3)

Mike Kwan
Mike Kwan

Reputation: 24447

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:

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 612784

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:

Upvotes: 4

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132974

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

Upvotes: 0

Related Questions