-
Assembly question
Hi,
Can you please explain what the following program does?
Code:
Execution begins at address 0
.pos 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
.align 4
array: .long 0xd
.long 0xc0
.long 0xb00
.long 0xa000
Main: irmovl $4,%eax
pushl %eax # Push 4
irmovl array,%edx
pushl %edx # Push array
call rSum # Sum(array, 4)
halt
/* $begin rsum-ys */
# int Sum(int *Start, int Count)
rSum: pushl %ebp
rrmovl %esp,%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
irmovl $-1,%edx
addl %edx,%eax # Count--
pushl %eax # Push Count
irmovl $4,%edx
rrmovl %ebx,%eax
addl %edx,%eax
pushl %eax # Push Start+1
call rSum # Sum(Start+1, Count-1)
mrmovl (%ebx),%edx
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
ret
/* $end rsum-ys */
.pos 0x400
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.
--
Mats
-
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:
Code:
int rSum(int *ar, int size)
{
return size == 0 ? 0 : rSum(ar+1, size-1) + ar[0];
}
i.e. a recursive array summation function. The main function then uses this function to sum up the global array.
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.
--
Mats
-
The move differentiation is interesting indeed.
-
That's not standard AT&T assembly syntax. I dunno what it is.