Need some help converting assembly code to C. To my understanding it is a while loop with condition (a < c) but I do not understand the body of the while loop.
movl $0, -8(%ebp) # variable B is at ebp - 8
movl $0, -4(%ebp) # variable A is at ebp - 4
jmp .L3
.L2
movl 8(%ebp), %eax # parameter C is at ebp + 8
addl $2, %eax
addl %eax, %eax
addl %eax, -8(%ebp)
addl $1, -4(%ebp)
.L3
movl -4(%ebp), %eax
cmpl 8(%ebp), %eax
jl .L2
Also explain why you did what you did, thanks.
This is what I got so far
int a,b = 0;
while (a < c) {
c += 4 + 2*c;
a++;
}
If I did all that correctly, then the only thing I don't understand is the line
addl %eax, -8(%ebp)
addl %eax, -8(%ebp) will add the value in eax to the value stored at ebp-8. If you can understand the other add instructions then it's just the same. There's no add 4 intruction so I don't know how you can get the expression 4 + 2*c
movl $0, -8(%ebp) # B = 0
movl $0, -4(%ebp) # A = 0
jmp .L3
.L2
movl 8(%ebp), %eax # eax = C
addl $2, %eax # eax = C + 2
addl %eax, %eax # eax *= 2
addl %eax, -8(%ebp) # B += eax
addl $1, -4(%ebp) # A++
.L3
movl -4(%ebp), %eax
cmpl 8(%ebp), %eax
jl .L2
So the result is as below
int a, b = 0;
while (a < c) {
b += (c + 2)*2;
a++;
}
which is simply
int a = c, b = c*(c+2)*2;
Related
So I'm pretty much a noob in IA32 assembly language. I tried compiling this C function into IA32 assembly (-mpreferred-stack-boundary=2):
__attribute__((cdecl))
int odd_sum(int a[], int n, int sum) {
if (n == 0) return sum;
else if ((a[n-1] % 2) == 0)
return odd_sum(a, n-1, sum);
else return odd_sum(a, n-1, sum + a[n-1]);
}
and the GCC outputs this:
.file "test.c"
.text
.globl _odd_sum
.def _odd_sum; .scl 2; .type 32; .endef
_odd_sum:
LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $12, %esp
cmpl $0, 12(%ebp)
jne L2
movl 16(%ebp), %eax
jmp L3
L2:
movl 12(%ebp), %eax
addl $1073741823, %eax
leal 0(,%eax,4), %edx
movl 8(%ebp), %eax
addl %edx, %eax
movl (%eax), %eax
andl $1, %eax
testl %eax, %eax
jne L4
movl 12(%ebp), %eax
leal -1(%eax), %edx
movl 16(%ebp), %eax
movl %eax, 8(%esp)
movl %edx, 4(%esp)
movl 8(%ebp), %eax
movl %eax, (%esp)
call _odd_sum
jmp L3
L4:
movl 12(%ebp), %eax
addl $1073741823, %eax
leal 0(,%eax,4), %edx
movl 8(%ebp), %eax
addl %edx, %eax
movl (%eax), %edx
movl 16(%ebp), %eax
addl %eax, %edx
movl 12(%ebp), %eax
subl $1, %eax
movl %edx, 8(%esp)
movl %eax, 4(%esp)
movl 8(%ebp), %eax
movl %eax, (%esp)
call _odd_sum
L3:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE0:
.ident "GCC: (MinGW.org GCC-8.2.0-3) 8.2.0"
What I am not able to comprehend are these 2 lines:
addl $1073741823, %eax
leal 0(,%eax,4), %edx
I understand those 2 lines should have something to do with the a[n-1], but I can't seem to be able to understand what exactly they do in the process. Can someone help me with this problem please?
It is just a fancy way of computing the offset into the array a[n-1].
1073741823 is 0x3fffffff. If n is 3, for example, it will add them and get 0x40000002. Then it multiplies by 4 with the second instruction, which results in 0x00000008, discarding the top bits.
So we are left with an offset of 8 bytes, which is exactly the offset (in bytes) that you need for a[n-1], i.e. a[2] (when the size of an int is 4 bytes).
To get a more understandable output with the -S flag:
create assembler code:
c++ -S -fverbose-asm -g -O2 (other optimizaton flags) test.cc -o test.s
create asm interlaced with source lines:
as -alhnd test.s > test.lst
I am trying to convert this assembly code into a C snippet.
movl $0, -4(%ebp) # 4
movl -4(%ebp), %eax
sall $2, %eax
addl 8(%ebp), %eax
movl (%eax), %eax
cmpl 12(%ebp), %eax
jg .L6
.L6:
nop
Here's what I have so far, but I think something is wrong. The line "movl (%eax), eax" confuses me in particular.
int local = 0;
if ((int*)((local << 2) + param1) > parameter2) {
; // do nothing
}
Your interpretation of movl %(eax), %eax is correct, but that of the line addl 8(%ebp), %eax is not. The correct code whould be something like this:
// parameter1 is an int* at 8(%ebp)
// parameter2 is an int at 12(%ebp)
int local = 0; // at -4(%ebp)
if (parameter1[local] > parameter2) {
; // nop
} else {
// whatever is betwween jg and .L6
}
Recent times I am having a look at assembly IA32, I did a simple toy example:
#include <stdio.h>
int array[10];
int i = 0;
int sum = 0;
int main(void)
{
for (i = 0; i < 10; i++)
{
array[i] = i;
sum += array[i];
}
printf("SUM = %d\n",sum);
return 0;
}
Yes, I know it is not very recommended to use global variables. I compiled the above code without optimizations and using the flag -s, I got this assembly :
main:
...
movl $0, %eax
subl %eax, %esp
movl $0, i
.L2:
cmpl $9, i
jle .L5
jmp .L3
.L5:
movl i, %edx
movl i, %eax
movl %eax, array(,%edx,4)
movl i, %eax
movl array(,%eax,4), %eax
addl %eax, sum
incl i
jmp .L2
Nothing too fancy and easy to understand, it is a normal while loop. Then I compiled the same code with -O2 and got the following assembly :
main:
...
xorl %eax, %eax
movl $0, i
movl $1, %edx
.p2align 2,,3
.L6:
movl sum, %ecx
addl %eax, %ecx
movl %eax, array-4(,%edx,4)
movl %edx, %eax
incl %edx
cmpl $9, %eax
movl %ecx, sum
movl %eax, i
jle .L6
subl $8, %esp
pushl %ecx
pushl $.LC0
call printf
xorl %eax, %eax
leave
ret
In this case it transformed in a do while type of loop. From the above assembly what I am not understanding is why "movl $1, %edx" and then "movl %eax, array-4(,%edx,4)".
The %edx starts with 1 instead of 0 and then when accessing the array it does -4 from the initial position (4 bytes = integer) . Why not simply ?
movl $0, %edx
...
array (,%edx,4)
instead of starting with 1 if you need to do -4 all the time.
I am using "GCC: (GNU) 3.2.3 20030502 (Red Hat Linux 3.2.3-24)", for educational reasons to generate easily understandable assembly.
I think I finally get the point, I test with:
...
int main(void)
{
for (i = 0; i < 10; i+=2)
{
...
}
}
and got:
movl $2, %edx
and with for (i = 0; i < 10; i +=3) and got :
movl $3, %edx
and finally with (i = 1; i < 10; i +=3) and got:
movl $4, %edx
Therefore, the compiler is initializing %edx = i (initial value of i) + incrementStep;
I have this IA32 assembly language code I'm trying to convert into regular C code.
.globl fn
.type fn, #function
fn:
pushl %ebp #setup
movl $1, %eax #setup 1 is in A
movl %esp, %ebp #setup
movl 8(%ebp), %edx # pointer X is in D
cmpl $1, %edx # (*x > 1)
jle .L4
.L5:
imull %edx, %eax
subl $1, %edx
cmpl $1, %edx
jne .L5
.L4:
popl %ebp
ret
The trouble I'm having is deciding what type of comparison is going on. I don't get how the program gets to the L5 cache. L5 seems to be a loop since there's a comparison within it. I'm also unsure of what is being returned because it seems like most of the work is done is the %edx register, but doesn't go back to %eax for returning.
What I have so far:
int fn(int x)
{
}
It looks to me like it's computing a factorial. Ignoring the stack frame manipulation and such, we're left with:
movl $1, %eax #setup 1 is in A
Puts 1 into eax.
movl 8(%ebp), %edx # pointer X is in D
Retrieves a parameter into edx
imull %edx, %eax
Multiplies eax by edx, putting the result into eax.
subl $1, %edx
cmpl $1, %edx
jne .L5
Decrements edx and repeats if edx != 1.
In other words, this is roughly equivalent to:
unsigned fact(unsigned input) {
unsigned retval = 1;
for ( ; input != 1; --input)
retval *= input;
return retval;
}
I am trying to translate the following:
Action:
pushl %ebp
movl %esp, %eax
subl $0x32, %esp
movl $0x0, -0x8(%eax)
movl $0x0, -0x4(%eax)
movl -0x4(%eax), %eax
cmpl $0x32(%eax), %ebp
movl -0x4(%ebp), %eax
sall $0x2, %ebp
addl 0x8(%ebp), %ebp
movl (%ebp), %ebp
addl %ebp, -0x8(%eax)
addl $0x1, -0x4(%eax)
What is the best way to translate this code?
For the original question:
mov -0x4(%ebp), %eax # eax = z;
mov 0xc(%ebp), %edx # edx = y;
mov (%edx, %eax, 4), %eax # eax = *(edx + eax + 4)
add $0x3, %eax # eax += 3
movb $0x41, (%eax) # *eax = 'A'
Dry-running the statements give:
y[z + 4][3] = 'A';