Displaying a number - assembly code not working Linux, x64 (NASM)

ε祈祈猫儿з 提交于 2020-01-06 15:06:41

问题


I am learning assembly on Linux (NASM) x64 machine (I don't have access to 32 or 16 bit machine), and I am trying to display number on screen (reverse of number according to code but that's a start).

Number is predefined in section .data -> num.

I am quite a newbie at assembly programming and due to the lack of material on x64 assembly (really, cant find much, and all I was able to find was quite confusing) I am unable to resolve the issue.

The issue is that the code compiles an links with no errors/warnings, but it just displays some spaces (not even newline). If I remove the call _newl code from _disprem, those spaces are also gone. There is not even segment fault or something.

By the way, algorithm to get the remainder (to get the digits in a number) is num - (num / 10) * 10

section .data
    num:        dq      102 ;my default number to get the reverse of (for now)
    nl:         db      0x0a
    nlsize:     equ     $-nl
    ten:        dq      10

section .bss
    rem:        resq        1
    remsize:    equ     $-rem

section .text
    global _start

_start:

    cmp qword [num], 0
        jng _exit       ;jump to _exit if num is not greater than 0
    mov rax, [num]      ;move the number to rax
    mov rbx, [num]      ;move the number to rbx as well so that i have original number in register to subtract and get the remainder
    mov rcx, [ten]      ;move 10 to rcx to be the divisor
    div rcx             ;divide number in rax by 10
    mov [num], rax      ;get the quotient to get the remaining number for quotient
    mul rcx             ;multiply number in rax by 10
    sub rbx, rax        ;subtract rbx - rax and store the value in rax (right?)
    mov [rem], rbx      ;get the remainder from rax. this must be done right after div (WHY??????????)
    call _disprem       ;call _disprem to display the remainder... call returns the flow back to the caller right?
    jmp _start          ;get to the loop again

_exit:
    mov rax, 60
    mov rdi, 0
    syscall

_newl:
    mov rax, 1
    mov rdi, 1
    mov rsi, nl
    mov rdx, nlsize
    syscall
    ret

_disprem:
    mov rax, 1
    mov rdi, 1
    add qword [rem], 0x0000000000000030 ;since the rem variable is quadword (64 bit)
    mov rsi, rem
    mov rdx, remsize
    syscall
    sub qword [rem], 0x0000000000000030 ;get me my original number back plz thanks
    call _newl
    ret

来源:https://stackoverflow.com/questions/37634431/displaying-a-number-assembly-code-not-working-linux-x64-nasm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!