Why does this give me a segfault

StackMasher

Well-Known Member
OP
Member
Joined
Nov 29, 2016
Messages
136
Trophies
0
Age
20
XP
370
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
 

Maq47

Lord of Pyro
Member
Joined
Jan 7, 2012
Messages
1,242
Trophies
1
Location
Your basement
Website
idont.have.one
XP
3,291
Country
United States
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.
 

StackMasher

Well-Known Member
OP
Member
Joined
Nov 29, 2016
Messages
136
Trophies
0
Age
20
XP
370
Country
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
 

memcpy

New Member
Newbie
Joined
Jan 2, 2017
Messages
4
Trophies
0
Age
35
XP
88
Country
Poland
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

General chit-chat
Help Users
  • No one is chatting at the moment.
    K3Nv2 @ K3Nv2: Well start walking towards them +1