0

The following code works. However, it does NOT work if I remove the #1 line. I do not understand why storing $0 in %rax makes it work.

.section .data
msg: .ascii "Hello world!\n"

.text
.globl main:

main:
movq $msg, %rdi
movq $0, %rax     #1
call printf

The following code seg faults

.section .data
msg: .ascii "Hello world!\n"

.text
.globl main:

main:
movq $msg, %rdi
call printf

I read that to do a sys call an integer value that designates the call is required in %rax. I have done this using write() and it worked beautifully. However, examples I find for printf don't seem to have this requirement.

Any help with this matter is appreciated. I can explain more if necessary

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
donsiuch
  • 343
  • 3
  • 8
  • 18

1 Answers1

3

The value in rax needs to be the number of floating point parameters being passed using a function that supports a variable number of arguments.

This document should help, see section 3.5.7 on variable argument lists.

When a function taking variable-arguments is called, %rax must be set to the total number of floating point parameters passed to the function in vector registers

ecm
  • 2,583
  • 4
  • 21
  • 29
Steve
  • 7,171
  • 2
  • 30
  • 52