Accessing passed in argument in assembly
I am new to assembly programming and am having issues passing and accessing a data list in a function. I am working though a pdf I downloaded from the www titled Programming Ground Up by Jonathan Bartlett. Anyway, I can create a data list but when I pass it to my function I can only get the first value' all of the rest seem to be garbage but they do seem consistent between executions. I have been trying to figure this out for a couple days and scoured the www looking for answers but haven't found any. So here I am.
The task is to push the data list onto the stack call the function pull the argument off the stack grab an element off the argument pass it back in eax
I am using GAS on x86 Linux
data_list: .long 7,6,5,4,3 data_items: .long .section .text .global _start _start: movl data_list, %eax pushl %eax call pass # move eax to ebx movl %eax, %ebx # set exit movl $1, %eax # call kernel int $0x80 .type pass, @function pass: # save off ebp pushl %ebp # move esp to ebp movl %esp, %ebp # get argument movl 8(%ebp), %ebx # move ebx to our variable movl %ebx, data_items # set edi to then index we want movl $4, %edi # get element from data and put it into our return value movl data_items(,%edi,4), %eax done: # reset esp movl %ebp, %esp # pop ebp popl %ebp # return to caller ret
I am expecting it to return 3 but i get 116. This leads to be believe that it's only passing a copy of the first element in the data list as if edi is 0 I get the correct value.