Why does this give me a segfault

StackMasher

Well-Known Member
Member
Joined
Nov 29, 2016
Messages
136
Reaction score
65
Trophies
0
Age
23
XP
410
Country
I'm learning amd64 assembly (gas syntax) and I'm trying to write a hello world program based on this, but when I run it I get a segmentation fault. Here's the code:
Code:
.data
   str:
     .ascii "Hello World!\n"
.text
   .globl main
   .extern printf
   main:
     movq str,%rdi
     call printf
     ret
 
Does gas syntax even recognize regex? If not, you should replace '\n' with '\12'. If you are running the code as a .s file, the 3rd line would need to read as follows:
Code:
     .ascii "Hello World!\12\0"

This is because ASCII strings in a C routine need to be null terminated, hence the '\0'. Apply this fix, then tell me if it works.
 
Does gas syntax even recognize regex? If not, you should replace '\n' with '\12'. If you are running the code as a .s file, the 3rd line would need to read as follows:
Code:
     .ascii "Hello World!\12\0"

This is because ASCII strings in a C routine need to be null terminated, hence the '\0'. Apply this fix, then tell me if it works.
I explicitly null terminated it like you said and the segfault is still there
Code:
.data
   str:
     .ascii "Hello World!\12\0"
.text
   .globl main
   .extern printf
   main:
     movq str,%rdi
     call printf
     ret
 
Try with lea (change movq), printf takes an address to string. Because you don't pass variable args list you need to zero rax.
 

Site & Scene News

Popular threads in this forum