C calling conventions
i have been doing alot of coding in ASM recently and i need some info on c calling conventions i thought all i had to do was pop off the arguments... but it is giving me errors...
temp = func1(a,b);
--------------
func1 proc
pop eax ; var a
pop ecx ; car b
add ax,cx ; add them
ret ; value is in eax as it is supposed to be
func1 endp
anyway, code like above is giving me errors when it retuns, so something must be wrong... what else do i have to do to the stack?
ThanX,
RanXacT
RanXact@yahoo.com
Simplify it a bit by just using inline. Then var a and var b will be declared as variables - much simpler!
well the function is in MASM, i am linking w/ MSVC++ and the function is MUCH more complex than this one, and has 8 arguments (all call by reference). if i made it inline i would not have access to the variables in the object it is in....
*a better example would be*
temp = func1(a,b);
--------------
func1 proc
pop edx ; IP (get return address)
pop eax ; var a
pop ecx ; car b
; do somthing with input variables
mov eax, return_value
push edx ; IP (put return value back on top)
ret ; value is in eax as it is supposed to be
func1 endp
this is still giving me errors though... it says i am screwing up esp somewhere... but what else do i need to do with the stack?
ThanX,
RanXacT
*a better example would be*
temp = func1(a,b);
--------------
func1 proc
pop edx ; IP (get return address)
pop eax ; var a
pop ecx ; car b
; do somthing with input variables
mov eax, return_value
push edx ; IP (put return value back on top)
ret ; value is in eax as it is supposed to be
func1 endp
this is still giving me errors though... it says i am screwing up esp somewhere... but what else do i need to do with the stack?
ThanX,
RanXacT
RanXact@yahoo.com
you need to declare funcs with the C calling stuff
it was something like
func1 proc C CALLABLE (parms)
it''s a been a few years since i''ve done anything like this and i cant find any of my old asm files on this machine to verify this. the CALLABLE is most likely some other word. and the (parms) is in some other syntax.
this is so that it knows how to pass the parms from C. you should be able to find this on some asm site or MASM documentation.
it was something like
func1 proc C CALLABLE (parms)
it''s a been a few years since i''ve done anything like this and i cant find any of my old asm files on this machine to verify this. the CALLABLE is most likely some other word. and the (parms) is in some other syntax.
this is so that it knows how to pass the parms from C. you should be able to find this on some asm site or MASM documentation.
Carl "trixter"[email=carl@trixoft.com]carl@trixoft.com[/email]http://www.trixoft.com
i tried putting esp into another register an use it ithout modifiing it, which for some readon started working, but is totally wrong..
soo... i know when the proc is called, IP is put on the stack, and the parameters i want are below it. and after the return, it tries to readjust the stack.... this is confusing.
IP <- esp
param1
param2
to get to parameters i have to go past IP and i have to have IP on top when i return because it has the return address...
but when i get back, it seems to assume i have the stack pointer below the first parameter.. but then i wouldn't know where to return to... i am confusing myself...
anyawy... i am confused
RanXacT
Edited by - ranxact on 2/23/00 4:15:11 PM
Edited by - ranxact on 2/23/00 4:17:12 PM
soo... i know when the proc is called, IP is put on the stack, and the parameters i want are below it. and after the return, it tries to readjust the stack.... this is confusing.
IP <- esp
param1
param2
to get to parameters i have to go past IP and i have to have IP on top when i return because it has the return address...
but when i get back, it seems to assume i have the stack pointer below the first parameter.. but then i wouldn't know where to return to... i am confusing myself...
anyawy... i am confused
RanXacT
Edited by - ranxact on 2/23/00 4:15:11 PM
Edited by - ranxact on 2/23/00 4:17:12 PM
RanXact@yahoo.com
ok.. i seem to have pieced it together from the MSVC dissasembler... although i am not a whiz at ASM i think i loads esp into another variable and uses the copy to fetch the parameters...
func1 proc
push ebp
mov ebp, esi
add ebp, 8 ; to get past IP and EBP
mov eax, [ebp] ; this gets parameter 1
mov ecx, [ebp+4] ; this gets parameter 2
......
mov eax, RETURN_VALUE
pop ebp
ret
this seems to work, but i am not sure if it is the way one is supposed to do it.... if there is a better or more correct way.. please post...
ThanX
RanXacT
RanXact@yahoo.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement