pushl %ebp
movl %esp,%ebp
pushl %ebx
movl 8(%ebp),%ebx
leal 2(%ebx),%edx
xorl %ecx,%ecx
cmpl %ebx,%ecx
jge .L4
.L6:
leal 5(%ecx,%edx),%edx
leal 3(%ecx),%eax
imull %eax,%edx
incl %ecx
cmpl %ebx,%ecx
jl .L6
.L4:
movl %edx,%eax
popl %ebx
movl %ebp,%esp
popl %ebp
ret

trying to make code in c then compiling it with the -S -m32 flags to see if i get the same output in the assemble file but its kind of confusing


its supposed to look something like this

int func(int x)
{
int i;
int result = _____________;
for( ________; ________; i++ ) {
__________________;
__________________;
}
return result;

}

to convert your code to C, firstly you need to separate your code
- you have function so there are some part are common to all function
- if you have parameter you have code like 8(%ebp) or C(%ebp)
- and then try to translate each line to C then combine them

here I explain your asm code

pushl %ebp		; #start of call
movl %esp,%ebp	        ; #
pushl %ebx              ; backup ebx

movl 8(%ebp),%ebx       ; mov first parameter to ebx
leal 2(%ebx),%edx       ; result (edx) = first param + 2
xorl %ecx,%ecx          ; i = 0 (ecx as a for counter)
cmpl %ebx,%ecx          ; check i with first parameter
jge .L4                 ; if greater or equal to first param jump .L4
.L6:
leal 5(%ecx,%edx),%edx  ; result = result + i + 5
leal 3(%ecx),%eax       ; eax = i + 3 (used in the next instruction)
imull %eax,%edx         ; result = result * (i + 3)
incl %ecx               ; i++ (increment for counter)
cmpl %ebx,%ecx          ; check i with first parameter
jl .L6	                ; if less jump to .L6
.L4:

movl %edx,%eax         ; return result

popl %ebx              ; restor ebx
movl %ebp,%esp         ; #end of calling
popl %ebp              ; #
ret                    ; #

and converted to C can be something like this

int func(int x)
{
	int i;
	int result = x + 2;

	for (i = 0; i < x; i++){
		result = result + i + 5;
		result = result * (i + 3);
	}
	return result;
}
This article has been dead for over six months. Start a new discussion instead.