仅使用Linux系统调用在x86-64程序集中显示数字

时间:2016-06-07 09:58:02

标签: linux assembly nasm x86-64

Previous version of this question(这个问题最初有一个不同的问题,即使现在的代码与此问题中的代码相同)

我正在尝试使代码在Linux 64位NASM中的控制台上显示一个数字,没有使用c / c ++函数(纯汇编)。代码编译和链接很好,但它不会给出输出...

它只显示换行符一段时间,然后显示' 7'永远。我是大会的新手,所以我不知道出了什么问题。请帮忙......以下是代码:

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    ;for getting ascii value (48 is ascii 0 in decimal) to convert the rem to character
    mov rdx, remsize
    syscall
    sub qword[rem], 0x0000000000000030 ;get me my original number back plz thanks
    call _newl
    ret

0 个答案:

没有答案