Why does this give me a segfault

Discussion in 'Computer Programming, Emulation, and Game Modding' started by StackMasher, Dec 19, 2016.

Dec 19, 2016
  1. StackMasher
    OP

    Newcomer StackMasher Member

    Joined:
    Nov 29, 2016
    Messages:
    40
    Country:
    Antarctica
    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
     
  2. MarcusCarter

    Member MarcusCarter GBAtemp Fan

    Joined:
    Jan 7, 2012
    Messages:
    374
    Location:
    Indianapolis, IN
    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.
     
  3. StackMasher
    OP

    Newcomer StackMasher Member

    Joined:
    Nov 29, 2016
    Messages:
    40
    Country:
    Antarctica
    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
     
  4. sarkwalvein

    Member sarkwalvein GBAtemp Psycho!

    Joined:
    Jun 29, 2007
    Messages:
    3,704
    Location:
    Germany
    Country:
    Argentina
    Just checking, but are you using Linux or Windows?
     
  5. StackMasher
    OP

    Newcomer StackMasher Member

    Joined:
    Nov 29, 2016
    Messages:
    40
    Country:
    Antarctica
    Linux
     
  6. memcpy

    Newcomer memcpy Newbie

    Joined:
    Jan 2, 2017
    Messages:
    4
    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.
     

Share This Page