装配打印在大数字之前

发布于 2025-02-03 19:00:07 字数 1330 浏览 2 评论 0原文

该程序打印小数字,但不是大数字,我不知道为什么。

例如打印1346269将以“ C1346269”打印 打印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

This program prints small numbers fine but not big numbers and I don't know why.

For example print 1346269 will print out as "c1346269"
and print 40000 will print out as "40000" just like it's supposed to.

Here's the macro:

%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

And here is how I use it:

    _start:
        
        print 40000
        exit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-02-10 19:00:08

storeloop inc rcx结尾,将rcx 留在后面。因此,在调用syscall之前, printloop 必须 Predecrement 指针。

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

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

The storeLoop ends with an inc rcx, leaving RCX behind the last character. Therefore, the printLoop must pre-decrement the pointer before invoking the syscall.

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

The difference between 'long' and 'short' numbers is purely because of what happens to be in memory at the first address that you print from (garbage).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文