Can you please explain what the following program does?
Execution begins at address 0
init: irmovl Stack, %esp # Set up Stack pointer
irmovl Stack, %ebp # Set up base pointer
jmp Main # Execute main program
# Array of 4 elements
array: .long 0xd
Main: irmovl $4,%eax
pushl %eax # Push 4
pushl %edx # Push array
call rSum # Sum(array, 4)
/* $begin rsum-ys */
# int Sum(int *Start, int Count)
rSum: pushl %ebp
pushl %ebx # Save value of %ebx
mrmovl 8(%ebp),%ebx # Get Start
mrmovl 12(%ebp),%eax # Get Count
andl %eax,%eax # Test value of Count
jle L38 # If <= 0, goto zreturn
addl %edx,%eax # Count--
pushl %eax # Push Count
pushl %eax # Push Start+1
call rSum # Sum(Start+1, Count-1)
addl %edx,%eax # Add *Start
jmp L39 # goto done
L38: xorl %eax,%eax # zreturn:
L39: mrmovl -4(%ebp),%ebx # done: Restore %ebx
rrmovl %ebp,%esp # Deallocate stack frame
popl %ebp # Restore %ebp
/* $end rsum-ys */
Stack: # The stack goes here
Do you have a specific question?
The comments seem to make a fairly good job of explaining what is going on.
Does it add the values of the 4 element array given?
Looks like it. But it's reallty weird syntax (looks like x86, but not quite), and it's doing it in a very weird way - why would you spend that many instructions on adding 4 numbers together.
It's x86 assembly in AT&T syntax, as used by the GNU assembler. And it's not a very weird way. It's pretty much equivalent to this C code:
i.e. a recursive array summation function. The main function then uses this function to sum up the global array.
int rSum(int *ar, int size)
return size == 0 ? 0 : rSum(ar+1, size-1) + ar;
The weird part is the recursion, not the separate function.
It may be a similar syntax to AT&T, but as far as I know, you don't differentiat different types of move instructions in standard AT&T, whilst this has irmov, rrmov to indiate "immedate to register" and "register to register" move.
And yes, I did understand that it's recursive, which is a solution, but hardly an efficient one.
The move differentiation is interesting indeed.
That's not standard AT&T assembly syntax. I dunno what it is.