Q:

Assembly function basics?

I need a little bit of help understanding some things on functions in assembly.  I can make a simple function that takes in a parameter or 2 and then returns something like a number by placing it in a register %eax.  My question is how do you return a list?  I am creating a function that will search through a defined data set of maybe 100 numbers and I need to keep running totals of the number of numbers in particular ranges (0-9, 10-19, 20-29, etc) so a variable for each range.  Since the number of registers is finite, I will obviously have to create space for the local variables by adjusting the stack and what not.  But, how do I return all of these values? I would assume you would return an address in %eax pointing to where these variables are, but since the values were local and created on the stack, wouldn't I not be able to access these?  I mean I guess you could, but that goes against everything I know about local variables and their scope.  Are these values supposed to be put on the heap or something?  I haven't been taught anything involving the heap for assembly, so I don't know if that's what I'm supposed to do or I'm not understanding something...?  Any help is appreciated.  Thanks.

A:

If you know how many elements in the list/array there are going to be, you can just allocate memory for it either in that function (use C's malloc() or an equivalent) or outside of it (again, use malloc() or declare the array/list as a variable or a set of variables referencing one another) and pass the function the address of the first element of the list/array.
Alternatively, you could build the list/array element by element in the function and return from it the address of the first element. Naturally, you'll need to allocate the first element and then grow the allocation to have some room for subsequent elements (use C's realloc()) or you can allocate all elements individually (using C's malloc()) and chain them (that is, the elements would need to include a pointer/address for the purpose of chaining them into a list).

