2

I tried to make some simple assembly code to print out a number with the assembly. I try to it with a printf function call. Then I run the code I get no output and when I debug it with gdb I get a message that says:

__printf (format=0x601040 "Result: %d") at printf.c:28
28  printf.c: No such file or directory.

I compile it with:

gcc -g -c print.s -o file.o

And then linked it with:

gcc file.o -o file

When I searched on this problem I found that I should test with

gcc file.o -o file -lc /lib64/ld-linux-x86-64.so.2

But the result was the same.

My code

.data
    OUT:    .asciz  "Result: %d"  #The output string

.text
.global main

main:
    jmp     print

print:
    mov     $OUT,   %rdi    # Our preperation string
    mov     $5,     %rsi    # Value to be printed
    xor     %rax,   %rax    # zero out
    call    printf          # call the function
    #TODO: Jump back

exit:
    mov     $60,    %rax    # Call the exit syscall
    mov     $0,     %rdi    # error code 0
    syscall                 # Interupt kernel and make the syscall
melpomene
  • 84,125
  • 8
  • 85
  • 148
Olof
  • 776
  • 2
  • 14
  • 33
  • 1
    My guess would be output buffering. Try calling `exit(0)`. – melpomene Oct 29 '16 at 15:15
  • 3
    the exit (60) syscall doesn't flush the _C_ libraries output and input buffers before closing. If building with the _C_ library and using _main_ as an entry point then you can do a _RET_ instruction to do proper _C_ shutdown or you can call the libc `exit` function. – Michael Petch Oct 29 '16 at 15:29
  • The problem was the output buffering. Calling `ret` after the printf solved it. – Olof Oct 29 '16 at 16:38
  • 2
    The “no such file or directory” warning comes from gdb trying to step through the source code of `printf` but that source code not being present on your system. – fuz Oct 29 '16 at 16:44
  • @MichaelPetch Please write an answer so OP can accept it. – fuz Oct 29 '16 at 16:45
  • 1
    @FUZxxl The question has been asked before. Someone should find the duplicate and mark this if they are so concerned, or the OP can self answer. I can't be bothered. That comment was pretty much a cookie cutter response. – Michael Petch Oct 29 '16 at 16:46

0 Answers0