我开发了以下代码来打开一个名为in.put
的输入文件,并从中读取一个字符。但是gdb
调试器显示读取单个字符会在-9
寄存器返回eax
。读取错误的位置显示在代码中。我无法弄清楚我错过了什么,有人知道吗:
section .data ;initialized data
;input file
input db 'in.put'
inputhandle: dd 0x00 ;place-holder for input file handle
section .bss ;uninitialized data
;prepare memory location for reading a character on file
charbuff: resb 0x01
section .text ;instructions
global _start
_start:
;open file at read-write permission
mov ecx, 1 ;read-write
mov ebx, input ;file name pointer
mov eax, 5 ;open
int 0x80
;check if open is good
cmp eax, 0
jle end ;if eax<=0 , jump end
mov [inputhandle], eax ;store returned file handle
;read a character on file
mov edx, 0x01 ;length of signle-character memory location = 0x01
mov ecx, charbuff ;pointer to single-character memory location
mov ebx, [inputhandle] ;file handle
mov eax, 3 ;system read
int 0x80
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;at this point, debugger shows
;the returned value in eax is -9
;I'm not sure why
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
end:
;close file
mov ebx, [inputhandle] ;file handle
mov eax, 6 ;system close
int 0x80
; exit
mov ebx, 0
mov eax, 1
int 0x80