简体   繁体   English

使用NASM汇编输出整数

[英]Printing integers in assembly with NASM

I'm trying to print '12345' using printf in assembly with nasm. 我正在尝试使用带有nasm的汇编中的printf打印'12345'。 It keeps printing age. 保持印刷时代。 I'm basing this off of a lab we did where we printed a counter digit (just a single digit) and it worked. 我以实验室为基础,在该实验室中我们打印了一个计数器数字(仅一个数字),并且它起作用了。

Must I use the divide by 10 method or is this close to how it should be setup to print '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

The answer depends on the operation system. 答案取决于操作系统。 In Windows x64 assembly, instead of passing arguments to the stack, you use some of the registers. 在Windows x64程序集中,您可以使用某些寄存器,而不是将参数传递给堆栈。 Move the first argument, format, to rcx and move the second argument, rax, to rdx. 将第一个参数format移至rcx,将第二个参数rax移至rdx。 With Linux, use rdi instead of rcx and rsi instead of rdx. 在Linux中,使用rdi代替rcx,使用rsi代替rdx。

Are you simply trying to print 12345 to your terminal. 您是否只是想在终端上打印12345。 Perhaps I missed something. 也许我错过了一些东西。

section .data
        fmt:    db      `%d\n`
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           

output: 输出:

$ ./user3866044_001
12345

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM