我有以下代码打开文件,将其读入缓冲区然后关闭文件。
关闭文件系统调用要求文件描述符号在ebx寄存器中。 ebx寄存器在读取系统调用之前获取文件描述符编号。我的问题是,在进行读取系统调用之前,我应该将ebx寄存器保存在堆栈中还是某个地方(可以将80h垃圾邮件放入ebx寄存器吗?)。然后恢复ebx寄存器以进行紧密系统调用?或者我的代码是否优良且安全?
我运行了下面的代码并且它可以工作,我只是不确定它是否通常被认为是良好的汇编实践,因为我没有在int 80h读取调用之前保存ebx寄存器。
;; open up the input file
mov eax,5 ; open file system call number
mov ebx,[esp+8] ; null terminated string file name, first command line parameter
mov ecx,0o ; access type: O_RDONLY
int 80h ; file handle or negative error number put in eax
test eax,eax
js Error ; test sign flag (SF) for negative number which signals error
;; read in the full input file
mov ebx,eax ; assign input file descripter
mov eax,3 ; read system call number
mov ecx,InputBuff ; buffer to read into
mov edx,INPUT_BUFF_LEN ; total bytes to read
int 80h
test eax,eax
js Error ; if eax is negative then error
jz Error ; if no bytes were read then error
add eax,InputBuff ; add size of input to the begining of InputBuff location
mov [InputEnd],eax ; assign address of end of input
;; close the input file
;; file descripter is already in ebx
mov eax,6 ; close file system call number
int 80h