简体   繁体   中英

Printing integers in assembly with NASM

I'm trying to print '12345' using printf in assembly with nasm. 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'

    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. Move the first argument, format, to rcx and move the second argument, rax, to rdx. With Linux, use rdi instead of rcx and rsi instead of rdx.

Are you simply trying to print 12345 to your terminal. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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