1

Does printf has a limit to the number of values you can print?

Here is my code.

.data
.balign 4       
   string: .asciz "\n%d %d %d %d\n"        
.text
.global main
.extern printf

main:
    push    {ip, lr}        @ push return address + dummy register
                            @ for alignment
    ldr     r0, =string     @ get address of string into r0
    mov     r1, #11
    mov     r2, #22
    mov     r3, #33
    mov     r4, #444
    bl      printf          @ print string and pass params
                            @ into r1, r2, and r3
    pop     {ip, pc}        @ pop return address into pc

When I compile and execute this code it prints this:

11 22 33 1995276288

As you can see, the value in R4 does not print the right value.

I don't know why?

zx485
  • 28,498
  • 28
  • 50
  • 59
Daniel Robert Webb
  • 109
  • 1
  • 1
  • 5
  • 1
    Because only the first 4 arguments are passed in registers. Sounds like you forgot to read the calling convention documentation. – Jester Nov 05 '16 at 19:15

1 Answers1

9

Only the first 4 arguments are passed in registers (r0-r3) on the ARM -- any additional args are passed on the stack. Check out the procedure call ABI for details.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226