Reputation: 11
cmp eax, 1
je .L3
cmp eax, 4
je .L4
jmp .L9
.L3:
mov DWORD PTR [ebp-16], 1
jmp .L5
.L4:
mov DWORD PTR [ebp-16], 2
jmp .L5
I need some help figuring out how exactly to convert this to C code. I've tried doing a nested if statement but it converts to
cmp eax, 1
je .L3
cmp eax, 4
je .L3
The code I tried was this
if (var1 != 1)
{
if(var1!=4)
{
var2=1;
}
}
Entire Assembly code for context
push ebp
mov ebp, esp
sub esp, 16
mov DWORD PTR [ebp-4], 4
mov DWORD PTR [ebp-16], 0
mov DWORD PTR [ebp-8], 0
mov eax, DWORD PTR [ebp-4]
cmp eax, 1
je .L3
cmp eax, 4
je .L4
jmp .L9
.L3:
mov DWORD PTR [ebp-16], 1
jmp .L5
.L4:
mov DWORD PTR [ebp-16], 2
jmp .L5
.L9:
mov DWORD PTR [ebp-16], 3
.L5:
mov DWORD PTR [ebp-12], 0
jmp .L6
.L7:
mov eax, DWORD PTR [ebp-12]
add DWORD PTR [ebp-8], eax
add DWORD PTR [ebp-12], 1
.L6:
mov eax, DWORD PTR [ebp-12]
cmp eax, DWORD PTR [ebp-16]
jl .L7
mov eax, 0
leave
ret
Upvotes: 1
Views: 102
Reputation: 12668
That code should be assimilated to:
if (var1 == 1)
goto l3;
else if (var1 == 4) // we use the same value in eax so the same variable is being checked
goto l4;
else goto l9;
l3: var2 = 1; goto l5;
l4: var2 = 2; goto l5;
l9: var2 = 3;
l5: ...
I should use a switch
statement, as all the tests depend on the value of the var1
variable (indeed, the value of var1
is evaluated and stored into eax
, and this is the value compared all the time) giving:
switch (var1) {
case 1: var2 = 1; break;
case 4: var2 = 2; break;
default: var2 = 3; break;
}
Upvotes: 1