1

I recently started programming in assembly language and could get something wrong. This code is supposed to write out "21947392":

section .data
    nl db 10

section .bss
    number resb 19
    .end resb 1

section .text
    GLOBAL start

start:
    mov rdi, 21947392
    call _printNumber

    mov rax, 0x2000001
    xor rdi, rdi
    syscall

_printNumber:
    mov rcx, 10
    mov rsi, number.end
    mov rax, rdi
_loop:
    xor rdx, rdx
    div rcx
    add rdx, 48
    mov [rsi], rdx
    dec rsi
    cmp rax, 0
    jne _loop

    mov rdi, rsi
    inc rdi
    mov rsi, number.end
    sub rsi, rdi
    call _print

    mov rdi, nl
    mov rsi, 1
    call _print

    ret

_print:
    mov rax, 0x2000004
    mov rdx, rsi
    mov rsi, rdi
    mov rdi, 1
    syscall
    ret

It is written for macOS x64 on NASM and it only prints "2" (and seems to even differ from one assembly to another)... Here are the commands I use in terminal:

nasm -f macho64 -o printNumber.o printNumber.asm
ld printNumber.o -o printNumber
./printNumber

Please, help find what's wrong.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
korochun
  • 145
  • 1
  • 1
  • 11
  • 2
    `mov [rsi], rdx` moves 8 bytes (64-bits) from RDX to the address RSI. You want to move the lower byte in `dl` . How about `mov [rsi], dl`. I'd recommend using a debugger like `lldb` to debug your program. – Michael Petch Jun 03 '18 at 17:20
  • 2
    working version: [How do I print an integer in Assembly Level Programming without printf from the c library?](https://stackoverflow.com/a/46301894). – Peter Cordes Jun 03 '18 at 17:28

0 Answers0