S
S
sos1mple2020-05-13 15:02:50
macOS
sos1mple, 2020-05-13 15:02:50

How to display the result of addition in Nasm (MACOS, 64bit)?

Good afternoon!
Please tell me, I'm trying to add numbers in NASM. Here is the code:

global start

section .text

start:
    mov rax, 90     ; move our first number into eax
    add rax, 5
    mov [qword msg], rax
    call write_result ; Выводим результат
    mov rax, 0x02000001 ; Системный вызов для выхода
    xor rdi, rdi ; Код - 0
    syscall ; Выходим

write_result:
    mov      rax, 0x02000004
    mov      rdi, 1
    mov      rsi, msg
    mov      rdx, 13
    syscall
    ret

section .data
    a: dw 5
    b: dw 30
    c: dw 65
    d: dw 67
    t: dw 0
    msg: dw 0, 10


I launch, issues: "_". How can I make it output a number?
5ebbe1aaa4d3c955718013.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-05-13
@sos1mple

How can I make it output a number?

Convert the number to string form and output this string form. The write system call prints the bytes as is.
"as"? -- for example, in a loop, taking the remainder of division by 10 (or whatever base you want), adding '0' (or 'a' - 10, if it's 10 or more) to the remainder, and writing the result to a buffer, starting at end. Something like that:
mov rbx, 10
    mov rsi, str + 20
l1:
    mov rdx, 0
    idiv rbx
    add dl, '0'
    mov byte [rsi], dl
    add rsi, -1
    test rax, rax
    jnz l1
; rsi + 1 указывает на начало строки
; длина строки -- str + 20 - rsi

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question