I compiled a program in C which just sums all integers up to input. For example if input is 4 the output is 4+3+2+1=10.
I am having a bit of a trouble understanding the assembly x86 version of this program.
I wrote all the comments myself, please be so kind to indicate what I got right/wrong and how you would describe what each lines does. Through your comments I will be able to absorb a DEEPER understand of what the cpu exactly does, as for the moment I can't say that I fully understand what is going on here. Anyway, here it is. All comments are welcome.
.LC0:
.byte 0x25,0x64,0x0 ; 2 digits / integers that our program will output
main:
pushl %ebp ; we save %ebp for later usage
movl %esp,%ebp ; we set register %ebp to point to the stack frame
subl $12,%esp ; subtracts 18 bytes from the stack pointer (esp). This allocates 18 bytes of space on the stack to be used for variables.
movl $0,-12(%ebp)
leal -4(%ebp),%eax ; subtracks -4 from the memory address of ebp and stores it at register eax
pushl %eax ; we store register eax for later usage
pushl $.LC0
call __isoc99_scanf ; reads from io port / waiting for key input
movl $1,-8(%ebp)
leal 8(%esp),%esp ; adds +8 to stack pointer memory address
.L2:
movl -4(%ebp),%edx
cmpl -8(%ebp),%edx ; compares our input number with an incremented number
jl .L3 ; if incremented number is equal or bigger than input number goto .L3
movl -8(%ebp),%edx
addl %edx,-12(%ebp)
incl -8(%ebp)
jmp .L2 ; loop / another addition to our input
.L3:
pushl -12(%ebp)
pushl $.LC0 ; we push the argument to print function
call printf ; prints result on screen
xorl %eax,%eax ; sets %eax to zero
leave ; leave copies the frame pointer to the stack point and releases the stack space formerly used by a procedure for its local variables. leave pops the old frame pointer into (E)BP, thus restoring the caller's frame.
ret ; returns to address located on the top of the stack```