Skip to main content
gamedev.net
🔒 Locked

Assembler question

Started by Slug 2 replies 400+ views
Original Post
Slug
Slug
I need help to understand how the stack push the parameter when a funtion is calling; When you call a function like this for example: SomeFunc(float x, float y, float z) { x =+ 5; y =+ 5; z =+ 5; ... } At what stack pointer sp is pointing now, how can i make it point to x ? And can you show me how to use the push and pop instruction please? Dbl thanx in advance!
wendigo23
wendigo23
If I recall correctly... the parameters are pushed from right to left, so it would go:
push z
push y
push x
and then there is the function call
call SomeFunc
so after that whole sequence the stack pointer is pointing at the return address of the function. If you add 4 to the sp (stacks grow down on x86) it should be pointing at x.

If you look at the disassembly for a simple function you'll see something like:
push ebp
mov ebp, esp
... ;function here
pop ebp
what this does is copy the stack pointer into ebp for the duration of the function so that you can offset all your variables from that and still use the stack for other things (you don't have to worry about the sp changing all your variable offsets when you push something new). All your parameters would be referenced via ebp:
mov eax, [ebp + 4] ;copy x into eax
mov eax, [ebp + 8] ;copy y into eax
local variables are done in the opposite direction (but first you have to make room for them):
sub esp, 8 ;make room for two dwords of local variables
mov [ebp - 4], 16 ;set a local variable to 16
mov [ebp - 8], 0 ;set the other to zero

of course, I haven't done any of this in a long time, so I might be off on a few details.
JohnBolton
JohnBolton
The answers to your questions depend a lot on the CPU, the compiler, and the compiler settings. The only way to tell for sure is to look at the assembly output of the compiler.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
TheBluMage
TheBluMage
There are several different calling conventions, which will determine just how you pass parameters. This link provides a good summary of how they're passed and which function (calling or called) is responsible for clearing the stack. It might answer part of your question anyway.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.