如何打印已读取的字节数以使用 Assembly 进行控制台?

How can I print how many bytes has been read to console with Assembly?

这是我的代码 (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

我们在这里将我们的(希望是个位数)return 值从 sys_read 转换为 ASCII 数字,将其存储在 whatread,然后告诉 sys_write从 whatread 写入,就好像它是一个指向 1 个字符的字符串的指针(它是)。

并使用 echo aaa | ./buffer > output.txt

进行测试