Why does this give me a segfault

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

  1. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    118
    57
    Nov 29, 2016
    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

    MarcusCarter Goodra the Great

    Member
    455
    109
    Jan 7, 2012
    United States
    Indianapolis, IN
    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

    StackMasher GBAtemp Regular

    Member
    118
    57
    Nov 29, 2016
    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

    sarkwalvein More coffee, please!

    Member
    GBAtemp Patron
    sarkwalvein is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    5,414
    5,842
    Jun 29, 2007
    Germany
    Niedersachsen
    Just checking, but are you using Linux or Windows?
     
  5. StackMasher
    OP

    StackMasher GBAtemp Regular

    Member
    118
    57
    Nov 29, 2016
    Linux
     
  6. memcpy

    memcpy Newbie

    Newcomer
    4
    0
    Jan 2, 2017
    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.