mud*_*dge 11 linux x86 assembly system-calls
我有以下代码打开文件,将其读入缓冲区然后关闭文件.
关闭文件系统调用要求文件描述符号在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
Run Code Online (Sandbox Code Playgroud)
Mat*_*ery 11
该int 80h调用本身不会破坏任何东西,除了把返回值eax.所以你拥有的代码片段很好.(但是如果你的代码片段是一个更大的例程的一部分,它应该被通常的Linux x86 ABI之后的其他代码调用,那么你需要ebx在进入例程时保留,可能还有其他寄存器,并在退出时恢复. )
可以在中找到内核中的相关代码arch/x86/kernel/entry_32.S.由于广泛使用宏,以及各种细节(支持系统调用跟踪,DWARF调试注释等),有点难以理解,但是:int 80h处理程序是system_call(我链接到的版本中的第493行); 寄存器通过SAVE_ALL宏保存(第497行); 然后他们RESTORE_REGS在返回之前通过(第534行)再次恢复.
| 归档时间: |
|
| 查看次数: |
6197 次 |
| 最近记录: |