程序集在大数字之前打印字符

标签 assembly numbers output x86-64 data-conversion

这个程序可以很好地打印小数字,但不能打印大数字,我不知道为什么。

例如 print 1346269 将打印为“c1346269” print 40000 将打印为“40000”,就像它应该的那样。

这是宏:

%macro print 1
    mov rax, %1

    %%printInt:
        mov rcx, digit      ; set rcx to digit memory address
        mov rbx, 10         ; moving a newline into rbx
        mov [rcx], rbx      ; setting digit to rbx
        inc rcx             ; increment rcx position by one byte

    %%storeLoop:
        xor rdx, rdx        ; zero out rdx
        mov rbx, 10         
        div rbx             ; rax / rbx (10)

                            ; rdx holds the remainder of the divison
        add rdx, 48         ; add 48 to rdx to make in ascii character
                            
        mov [rcx], dl       ; get the character part of rdx
        inc rcx             ; increment digit position again

        cmp rax, 0
        jnz %%storeLoop     ; continue looping until rax is 0

    %%printLoop:
        push rcx

        ;perform sys write
        mov rax, SYSWRITE
        mov rdi, 1
        mov rsi, rcx
        mov rdx, 1
        syscall

        pop rcx
        dec rcx
        cmp rcx, digit      ; first byte of digit (10)
        jge %%printLoop

%endmacro

这是我的使用方法:

    _start:
        
        print 40000
        exit

最佳答案

storeLoop 以 inc rcx 结尾,将 RCX 留在最后一个字符后面。因此,printLoop 必须在调用系统调用之前预先递减指针。

%%printLoop:
    dec  rcx
    push rcx
    ...
    pop rcx
    cmp rcx, digit      ; first byte of digit (10)
    ja  %%printLoop

“长”数字和“短”数字之间的差异纯粹是因为内存中您打印的第一个地址处发生了什么(垃圾)。

关于程序集在大数字之前打印字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72473527/

相关文章:

java - 当我要求用户输入时,我无法从数组中删除元素

c - 三元运算符逻辑

bash - 不要在多个 bash 脚本中显示 pushd/popd 堆栈(安静的 pushd/popd)

GCC 编译器中的条件移动 (cmov)

assembly - 浮点程序给出无效结果

python - 有没有一种简单的方法可以用 Python 编写这个?

printing - write、print、pprint、princ 和 prin1 之间有什么区别?

c - 为什么在我清除输入缓冲区时它不换行?

assembly - 编译器和汇编器在计算机上的什么地方?

c - X86 32b 汇编 - 使用 atoll