gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Integrating ASM into C code

#2149 - animension - Thu Jan 30, 2003 4:53 am

Hi guys,

I'm trying to understand how C function parameters get passed into ASM code called by said C code. So far, I know that certain registers hold the values of the parameters and that only up to r3 gets stored directly into registers (all other parameters start on the stack). How would a function like this pass the values to an ASM function?

Code:

u32 myasmfunction(
  u32 param1,
  u32 param2,
  u32 param3,
  u32 param4,
  u32 param5
);



Which parameters would be stored into the registers directly and which ones into the stack? How does one pass the return value of the function back ( does it need to be in a particular register when "bx lr" is called?)
Do the parameters get bumped up one because there's a return value on the function?

I'd really like to know how to make C and ASM talk to each other so I can get low down on the nitty gritty ASM stuff :)

Thanks
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin

#2169 - Touchstone - Thu Jan 30, 2003 12:44 pm

Assuming you use GCC, check this out. http://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_21.html#SEC258

This is where your parameters go when calling your function:
Code:
param1 - r0
param1 - r1
param2 - r2
param3 - r3
param4 - sp+0
param5 - sp+4


Return value to your C function should be in r0. This would return 160. :)
Code:
mov   r0, #160
bx    lr

_________________
You can't beat our meat

#2187 - animension - Thu Jan 30, 2003 7:56 pm

Thanks! :)
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin