I have the following program to print a number via the printf
function:
format: .ascii "Your number is: %d.\n\0"
.globl main
main:
// printf('%d', 55);
mov $format, %rdi
mov $55, %rsi
mov $0, %eax
// call printf with 16-byte aligned stack:
sub $8, %rsp
call printf
add $8, %rsp
// return 0;
mov $0, %eax
ret
$ gcc -no-pie int.s -o int; ./int
Your number is: 55.
I have a few questions about this as I was writing this:
- Does the
sub $8...add $8
work fine to preserve alignment? For example, the same as doingpush %rbp...pop %rbp
. - I tried adding in some
.data
and.rodata
and.text
directives/sections but each time I would get a warning/error. Why aren't those allowed when invoking an assembly program viagcc
? How, for example, does C know that "format" is.data
and "main" is in.text
? - Is
mov $0, %eax; ret
the proper way to exit theC
main function from assembly? - Finally, what modifications would I need to make this program run without doing
-no-pie
?