如何打印已读取多少字节到带有程序集控制台的控制台



这是我的代码(buffer.asm

section .bss
    bufflen equ 2
    buff: resb bufflen
    whatreadlen equ 1
    whatread: resb whatreadlen
section .data
section .text
global main
main:
    nop
    read:
        mov eax,3           ; Specify sys_read
        mov ebx,0           ; Specify standard input
        mov ecx,buff        ; Where to read to...
        mov edx,bufflen     ; How long to read
        int 80h             ; Tell linux to do its magic
        mov esi,eax         ; copy sys_read return value to esi
        mov [whatread],eax  ; Store how many byte reads info to memory at loc whatread
        mov eax,4           ; Specify sys_write
        mov ebx,1           ; Specify standart output
        mov ecx,[whatread]  ; Get the value at loc whatread to ecx
        add ecx,0x30        ; convert digit in EAX to corresponding character digit
        mov edx,1           ; number of bytes to be written
        int 80h             ; Tell linux to do its work

当我这样称呼它时:

./buffer > output.txt < all.txt

(假设 all.txt 中有一些文本,例如"abcdef")

我希望在控制台中看到一个数字。但是我什么也没看到。我错过了什么?

你把一个值传递给sys_write而不是一个地址,这是行不通的。

改写这个:

main:
    nop
    read:
        mov eax,3           ; Specify sys_read
        mov ebx,0           ; Specify standard input
        mov ecx,buff        ; Where to read to...
        mov edx,bufflen     ; How long to read
        int 80h             ; Tell linux to do its magic
        mov esi,eax         ; copy sys_read return value to esi
        add eax, 30h        ;; Convert number to ASCII digit
        mov [whatread],eax  ; Store how many byte reads info to memory at loc whatread
        mov eax,4           ; Specify sys_write
        mov ebx,1           ; Specify standart output
        lea ecx,[whatread]  ;; Get the address of whatread in ecx
        mov edx,1           ; number of bytes to be written
        int 80h             ; Tell linux to do its work

在这里,我们将返回值从 sys_read 转换为 ASCII 数字,将其存储在 whatread 中,然后告诉sys_write从 whatread 写入,就好像它是指向 1 个字符的字符串的指针一样(确实如此)。

并用echo aaa | ./buffer > output.txt进行测试

最新更新