【问题标题】:assembly: stuck - reading in text file程序集:卡住 - 读取文本文件
【发布时间】:2013-02-25 21:58:41
【问题描述】:

以下是我希望是来自我的 NASM 程序的相关代码。一旦调用了 int 080h,调试器就会显示 eax 的 -9。我的 test.txt 中的文本是 321314145。我已经盯着这个看了好几个小时了,我在这里遇到了死胡同。为什么会这样?

%define BUFLEN 128
%define READLEN 3
%define SYSCALL_READ  3

    SECTION .bss                    ; uninitialized data section
buf:    resb READLEN                     ; buffer for read
rlen:   resb 4
newstr: resb BUFLEN

; read file name from arg

    ;
    pop     ebx         ;not using

    pop     ebx         ;not using

    pop     ebx         ;pop filename

; open file
;
    mov     eax, SYSCALL_OPEN
    mov     ecx, STDIN
    int     080h

    mov     eax, SYSCALL_READ ; read function
    mov     ebx, eax      ; Arg: file descriptor
    mov     ecx, buf      ; Arg: address of buffer
    mov     edx, READLEN      ; Arg: buffer length
    int     080h

【问题讨论】:

  • 你怎么好像在打开stdin,而你的文本却在一个文件中?
  • 我只是使用常量,它指向0,是只读模式
  • 使用strace ./a.out 来跟踪您的系统调用并查看您将垃圾作为参数传递给open(2),因此您当然会得到-ERRNO 返回值而不是fd。

标签: file assembly file-io x86 nasm


【解决方案1】:
mov     eax, SYSCALL_READ ; read function
mov     ebx, eax      ; Arg: file descriptor

你在开玩笑吧?

-9 是“错误文件号”(根据 errno.h)。确保您的 sys_open 成功,然后将 eax 放入 ebx.... 在更改 eax 之前!

【讨论】:

  • 测试 eax,eax 显然是在升旗。 jns 不会跳
猜你喜欢
  • 2019-09-27
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多