开发者

Assembly to C code

开发者 https://www.devze.com 2023-03-14 12:53 出处:网络
This Assembly code: cmp [Variable1], 10 jae AlternateBlock call SomeFunction jmp 开发者_如何学运维AfterIfBlock

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


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:


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:


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

0

精彩评论

暂无评论...
验证码 换一张
取 消