使用NASM在程序集中打印整数



我正在尝试在带有nasm的程序集中使用printf打印'12345'。它保持印刷年龄。我是基于我们在一个实验室里做的,我们打印了一个计数器数字(只有个位数),它起了作用。

我必须使用除以10的方法吗?或者这是否接近打印"12345"的设置方式

    bits 64
    global main
    extern printf


    section .text
main:
    ;function setup
    push    rbp
    mov     rbp, rsp
    sub     rsp, 32
    ;
    lea     rdi, [rel message]
    mov     al, 0
    call    printf
;mov    rdi,format
;push count
;push format    
mov rax, 12345
push rax
push format 
;mov    al,0
call    printf
;add esp,8  
;ret

    ; function return
    mov     eax, 0
    add     rsp, 32
    pop     rbp
    ret
    section .data
message: db      'Lab 3 - Modified hello program',0x0D,0x0a,'COSC2425 - Pentium assembly language',0x0D,0x0a,'Processed with NASM and GNU gcc',0x0D,0x0a
count   dq  12345
format  db  '%d',10,0

答案取决于操作系统。在Windows x64程序集中,您可以使用一些寄存器,而不是将参数传递到堆栈。将第一个参数format移动到rcx,将第二个参数rax移动到rdx。在Linux中,使用rdi代替rcx,使用rsi代替rdx。

您只是想在终端上打印12345吗。也许我错过了什么。

section .data
        fmt:    db      `%dn`
section .text
        global main
        extern printf
main:
        ;  x86_64 rdi rsi rdx rcx r8 r9
        mov rsi, 12345
        call _write
_exit:
        mov rax, 60
        xor rdi, rdi
        syscall
_write:
        push rbp
        mov rbp, rsp
        lea rdi, [fmt]
        xor rax, rax
        call printf
        xor rax, rax
        leave
        ret           

输出:

$ ./user3866044_001
12345

最新更新