有人可以告诉我如何用 1 字节长的缓冲区读取 10 字节吗?

发布于 2024-10-27 22:26:02 字数 1189 浏览 2 评论 0原文

有人可以告诉我如何用 1 字节长的缓冲区读取 10 字节吗?

我的编程环境是 ubuntu linux、emacs、汇编、at&t 语法

我可以读取文件并将读取的数据保存到缓冲区(1 字节),

但如何读取文件的下一个 1 字节并保存到缓冲区?

我编辑内容来粘贴我的努力

.section .data

    .section .bss
    .lcomm buffer,1

    .section .text
    .global _start

_start:
    movl %esp,%ebp
    subl $8,%esp
    #8(%ebp) is 2nd arg == input
    #12(%ebp) is 3rd arg == output

    #open,read,open,write,close
    movl $5,%eax
    movl 8(%ebp),%ebx
    movl $0,%ecx
    movl $0666,%edx
    int $0x80
    #%eax contains input's fd
    #movl to first local var
    movl %eax,-4(%ebp)

    movl $5,%eax
    movl 12(%ebp),%ebx
    movl $03101,%ecx
    movl $0666,%edx
    int $0x80
    #eax contains output's fd
    #movl to second local var
    movl %eax,-12(%ebp)

    #read 1 byte from file 1st byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80

    #read 1 byte from file I expect 2nd byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80

    #buffer contains 1 byte of file
    subb $48, buffer
    movl buffer,%ebx
    movl $1,%eax
    int $0x80

could somebody tell me how to read 10 bytes with 1 byte long buffer?

my programming environment is ubuntu linux, emacs, assembly, at&t syntax

I can read file and save read data to buffer(1byte)

but how can I read next 1 byte of file and save to buffer?

I edit the content to paste my effort

.section .data

    .section .bss
    .lcomm buffer,1

    .section .text
    .global _start

_start:
    movl %esp,%ebp
    subl $8,%esp
    #8(%ebp) is 2nd arg == input
    #12(%ebp) is 3rd arg == output

    #open,read,open,write,close
    movl $5,%eax
    movl 8(%ebp),%ebx
    movl $0,%ecx
    movl $0666,%edx
    int $0x80
    #%eax contains input's fd
    #movl to first local var
    movl %eax,-4(%ebp)

    movl $5,%eax
    movl 12(%ebp),%ebx
    movl $03101,%ecx
    movl $0666,%edx
    int $0x80
    #eax contains output's fd
    #movl to second local var
    movl %eax,-12(%ebp)

    #read 1 byte from file 1st byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80

    #read 1 byte from file I expect 2nd byte of data
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl $buffer,%ecx
    movl $1,%edx
    int $0x80

    #buffer contains 1 byte of file
    subb $48, buffer
    movl buffer,%ebx
    movl $1,%eax
    int $0x80

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2024-11-03 22:26:02

jcomeau@intrepid:/tmp$ nasm -f elf -o test.o test.nasm
jcomeau@intrepid:/tmp$ ld -o test test.o
jcomeau@intrepid:/tmp$ ./test
0  # this is what I typed
jcomeau@intrepid:/tmp$ 
jcomeau@intrepid:/tmp$ echo $?
48
jcomeau@intrepid:/tmp$ cat test.nasm
global _start
_start:
 push eax ; for buffer
 mov ecx, esp
 mov edx, 1 ; # bytes to read
 mov ebx, 0 ; stdin
 mov eax, 3 ; read
 int 0x80
 mov ebx, [ecx] ; what we just read
 mov eax, 1 ; exit
 int 0x80

您仍然需要为 10 个字符设置循环并转换为 ATT 语法,但这应该可以帮助您开始。


jcomeau@intrepid:/tmp$ nasm -f elf -o test.o test.nasm
jcomeau@intrepid:/tmp$ ld -o test test.o
jcomeau@intrepid:/tmp$ ./test
0  # this is what I typed
jcomeau@intrepid:/tmp$ 
jcomeau@intrepid:/tmp$ echo $?
48
jcomeau@intrepid:/tmp$ cat test.nasm
global _start
_start:
 push eax ; for buffer
 mov ecx, esp
 mov edx, 1 ; # bytes to read
 mov ebx, 0 ; stdin
 mov eax, 3 ; read
 int 0x80
 mov ebx, [ecx] ; what we just read
 mov eax, 1 ; exit
 int 0x80

you still need to set up the loop for your 10 chars and convert to ATT syntax, but this ought to get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文