簡體   English   中英

NASM Linux程序集打印整數

[英]NASM Linux Assembly Printing Integers

我試圖在linux上的nasm程序集中打印單個數字整數。 我目前編寫的內容很好,但沒有任何內容寫入屏幕。 任何人都可以向我解釋我在這里做錯了什么嗎?

section .text
    global _start

_start:
    mov ecx, 1          ; stores 1 in rcx
    add edx, ecx        ; stores ecx in edx
    add edx, 30h        ; gets the ascii value in edx
    mov ecx, edx        ; ascii value is now in ecx
    jmp write           ; jumps to write


write:
    mov eax, ecx        ; moves ecx to eax for writing
    mov eax, 4          ; sys call for write
    mov ebx, 1          ; stdout

    int 80h             ; call kernel
    mov eax,1           ; system exit
    mov ebx,0           ; exit 0
    int 80h             ; call the kernel again 

這是添加,而不是存儲:

add edx, ecx        ; stores ecx in edx

這將ecx復制到eax,然后用4覆蓋它:

mov eax, ecx        ; moves ecx to eax for writing
mov eax, 4          ; sys call for write

編輯:

對於'寫'系統調用:

eax = 4
ebx = file descriptor (1 = screen)
ecx = address of string
edx = length of string

在回顧了另外兩個答案之后,這就是我最終想出來的。

sys_exit        equ     1
sys_write       equ     4
stdout          equ     1

section .bss
    outputBuffer    resb    4       

section .text
    global _start

_start:
    mov  ecx, 1                 ; Number 1
    add  ecx, 0x30              ; Add 30 hex for ascii
    mov  [outputBuffer], ecx    ; Save number in buffer
    mov  ecx, outputBuffer      ; Store address of outputBuffer in ecx

    mov  eax, sys_write         ; sys_write
    mov  ebx, stdout            ; to STDOUT
    mov  edx, 1                 ; length = one byte
    int  0x80                   ; Call the kernel

    mov eax, sys_exit           ; system exit
    mov ebx, 0                  ; exit 0
    int 0x80                    ; call the kernel again

從男人2寫

ssize_t write(int fd, const void *buf, size_t count);

除了已指出的其他錯誤之外,write()還會獲取指向數據和長度的指針 ,而不是您嘗試提供的寄存器中的實際字節本身。

因此,您必須將數據從寄存器存儲到存儲器並使用該地址(或者如果它當前是常量,則不要將數據加載到寄存器中,而是加載其地址)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM