0

I've never tought it will be the time to ask my first question here, but here I am.

I have to learn assembly as part of my University studies, and searching the internet didn't bring me an answer to my problem. The problem is that I get segmentation fault core dumped when I try even this simple code:

.text                        

hello_str:                    
    .asciz "Hello, world!\n"                      

.global  main                      
main:
    
    movq %rsp, %rbp     
    movq $0, %rax
    movq hello_str, %rdi

    call printf

 

I use gcc -no-pie -o hello.o hello.s and then ./hello.o Commenting call printf makes it run, but without output(obviously). The question is what am I doing wrong? My pc is working on 64 bits has the Windows 10 OS but I use the Windows Subsystem for Linux to compile the program.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Even using $hello_str gives the same error – Cosmin Anton Sep 03 '20 at 18:27
  • 2
    Probably a stack alignment or calling convention issue... – Macmade Sep 03 '20 at 18:33
  • Make sure the first argument needs to be passed in rdi, and check this answer: https://stackoverflow.com/questions/12678230/how-to-print-argv0-in-nasm/12679627#12679627 – Macmade Sep 03 '20 at 18:37
  • yes, it does. I found out the problem and I will also put this as the answer. I needed to put: pushq %rbp at the beggining – Cosmin Anton Sep 03 '20 at 18:43
  • `movq hello_str, %rdi` is a qword load. Maybe you meant `movq $hello_str, %rdi` to pass the address? (Or better, `lea hello_str(%rip), %rdi`). Aligning the stack isn't sufficient to make this code work. Also, without a `ret` after the call, this will obviously crash. – Peter Cordes Sep 04 '20 at 00:26
  • @Macmade: x86-64 System V does pass the first arg in RDI, IDK why you're linking a 32-bit question. – Peter Cordes Sep 04 '20 at 00:27
  • @PeterCordes I've mentioned already that even using $hello_str gives the same error, but yes you are right. I meant $hello_str – Cosmin Anton Sep 04 '20 at 07:00
  • Well yeah, it makes sense that you need both things to be correct. Either a bad pointer or stack misalignment will make printf crash. Stuff like this matters. Your self-answer to this question implies that adding `push %rbp` to the exact code in the question would make something that worked. But that's obviously not the case, making it still not helpful for future readers to copy as an example. – Peter Cordes Sep 04 '20 at 07:13
  • @PeterCordes yes, you are right. I modified my answer – Cosmin Anton Sep 04 '20 at 07:25

1 Answers1

0

So I found out the answer. The thing is that I needed to put before everything this: pushq %rbp And modify hello_str to $hello_str

I don't know exactly what it does, but it seems to be working with it there.

  • 3
    As mentioned by Macmade this is likely due to stack alignment. The call to `main` leaves `rsp` pointing 8 bytes past a 16-byte boundary. The ABI requires you to change `rsp` by 8 to align it again before you make another call. Though saving and restoring `rbp` may also be needed anyway (the code calling `main` may need this). – ecm Sep 03 '20 at 19:32