Calling printf in x86-64 Linux requires RAX = 0?

Calling printf in x86-64 Linux requires RAX = 0?

Problem Description:

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

Solution – 1

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

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject