0

I have to make a puts function in assembly intel macho64 I would like to understand what and why i didn't print the \n ! in my main.c i'm calling puts twice but i got my text without a \n

I compile with :

nasm -f macho64 ft_puts.s

I make a putstr function to print a string :

%define SYSCALL(nb) 0x2000000 | nb
%define STDOUT  1
%define WRITE   4

section .text
    global  _ft_putstr
    extern  _strlen

_ft_putstr:
    push    rbp
    mov     rbp ,   rsp
    mov     rbx ,   rdi

    cmp     rbx ,   0x0
    je      exit
    mov     rdi ,   rbx

    call    _strlen
    mov     rdx ,   rax
    mov     rdi ,   STDOUT
    mov     rsi ,   rbx
    mov     rax ,   SYSCALL(WRITE)
    syscall

exit:
    leave
    ret

Then my puts function :

%define SYSCALL(nb) 0x2000000 | nb
%define STDOUT  1
%define WRITE   4

section .rodata
    retline  db 10
  nullstr  db "(null)"

section .text
    global  _ft_puts
  extern  _ft_putstr

_ft_puts:
    push    rbp
    mov     rbp ,   rsp
    mov     rbx ,   rdi

    cmp     rbx ,   0x0
    je      null

print:
    mov     rdi ,   rbx
    call    _ft_putstr

print_retline:
  lea   rdi , [rel retline]
    call  _ft_putstr

exit:
    mov rax ,   '10'
    leave
    ret

null:
  lea   rdi , [rel nullstr]
  call  _ft_putstr
  jmp   print_retline
albttx
  • 3,444
  • 4
  • 23
  • 42
  • 1
    Shouldn't your strings be NUL-terminated? How else is `strlen` supposed to find their lengths? – Michael Oct 13 '15 at 21:25
  • no @Michael it's changing nothing to add a null after my string .. and yes, strlen is a c function to get the length of a string (cf : man strlen) PS: if you have no idea, please upvote – albttx Oct 13 '15 at 21:38
  • You might be hitting the [known nasm bug](http://stackoverflow.com/a/31480510/547981). – Jester Oct 13 '15 at 22:14
  • @Jester This code work https://github.com/elieteyssedou/ASM/blob/master/ft_puts.s ! but i want to understand why mine doesn't – albttx Oct 13 '15 at 22:33

0 Answers0