0

I don't know why I cannot print anything with printf in GAS assembly if \n(LF - newline) is missing at the end of the string. If I put the newline char \n the line prints, but if I remove \n the line doesn't print. Can someone tell me why?

.extern printf
.section .data

hello:
    .string "Hello!" # doesn't print this way when \n is missing

.section .text

.globl _start

_start:
    nop
    movl $hello, %edi
    movl $0, %eax
    call printf

_end:    
    movq $60, %rax  #use the _exit syscall
    movq $0, %rdi   #return error code 0
    syscall         #make syscall
Jester
  • 56,577
  • 4
  • 81
  • 125
morophla
  • 11
  • 2
  • 6
    If you wish to use the C library (that `printf` is a part of), use `main` as entry point and do not use the `exit` syscall. You are not letting the C library shut down properly so the buffered line is not printed. PS: next time if you see your question comes out with broken formatting please fix it yourself. Use the preview as appropriate. – Jester May 25 '16 at 20:21
  • Try `.asciz` instead of `.string` in the definition of the string to output. – zx485 May 25 '16 at 20:21
  • Sorry Jester for not meeting you high standards. – morophla May 25 '16 at 20:25
  • 1
    It's a pretty low standard to use proper formatting. You did see it [came out as a horrible mess](http://stackoverflow.com/revisions/37446595/1), right? And poor _zx485_ had to fix it for you. You are asking for help, it's basic courtesy to post properly. – Jester May 25 '16 at 21:26
  • 2
    Jester is correct, you should consider using `main` as an entry point and link against the _C_ runtime. You could then use `ret` to return to the _C_ runtime that called main (rather than `exit` syscall) which would properly flush the output buffers, and exit the program. If you are adamant on using `_start` as an entry point then rather than using the `exit` syscall you should call the `exit` function (via `call exit`). Put the return code into `%edi`. – Michael Petch May 25 '16 at 23:40
  • Those aren't Jester's high standards, those are the standards for interfacing assembly and C. – David Hoelzer May 26 '16 at 02:56

1 Answers1

-1
pushl %ebp
lea   hello, %edi
movl  %esp, %ebp /*<----*/
call  printf
popl  %ebp
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 01 '17 at 14:39