Forum: ARM programming with GCC/GNU tools Dealing with C varargs in Assembly routines

Author: Tat Wan (Company: Universiti Sains Malaysia) (tcwan)
Posted on:

Rate this post
0 useful
not useful

I understand the basic issues involved with C-to-Assembly interfacing, 
but I'm stumped by how varags is handled in Assembly, say for an ioctl() 
handling routine.
extern int ioctl(int driverenum, unsigned long requestenum, ...);

int test_ioctl() {
  return 1;

gcc -fomit-frame-pointer is used when compiling to give the following 
ARM assembly output:
        .file   "ioctl.c"
        .align  2
        .global test_ioctl
        .type   test_ioctl, %function
        @ args = 0, pretend = 0, frame = 0
        @ frame_needed = 0, uses_anonymous_args = 0
        str     lr, [sp, #-4]!
        sub     sp, sp, #8
        mov     r3, #5
        str     r3, [sp, #0]
        mov     r3, #6
        str     r3, [sp, #4]
        mov     r0, #1
        mov     r1, #2
        mov     r2, #3
        mov     r3, #4
        bl      ioctl
        mov     r3, #1
        mov     r0, r3
        add     sp, sp, #8
        ldmfd   sp!, {pc}
        .size   test_ioctl, .-test_ioctl
        .ident  "GCC: (GNU) 4.4.1"

My question is, how can I determine the number of varargs? Is that a 
limitation of using -fomit-frame-pointer? If I don't specify 
'-fomit-frame-pointer', I can deduce the number of varargs which spilled 
onto the stack using the difference between fp and sp, but I don't see 
how that can be done for the following:
1. The number of varargs fits within r0-r3
2. -fomit-frame-pointer is used (which is normally the case)


Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Tat Wan wrote:

> My question is, how can I determine the number of varargs?

It must be deduced from the other parameters.  For the very typical
printf case, it can only be known while parsing the format string.
For your ioctl example, the number of arguments is only known by
dispatching the respective ioctl request.

It's up to the callee to know how to obtain the remaining arguments,
as the exact details are CPU and ABI specific.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.