我有下面的代码,打开一个文件,将其读入一个缓冲区,然后closures文件。
closures文件系统调用要求文件描述符编号位于ebx寄存器中。 读取系统调用之前,ebx寄存器获取文件描述符编号。 我的问题是我应该保存ebx寄存器堆栈或之前,我读系统调用,(可以int 80h垃圾ebx注册?)。 然后恢复closures系统调用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
除了将返回值放入eax
, int 80h
调用本身不会破坏任何东西。 所以你的代码片段是好的。 (但是如果你的代码片段是一个更大的例程的一部分,这个例程有望被其他代码在通常的Linux x86 ABI之后调用,那么在进入你的例程时需要保存ebx
和其他寄存器,并且在退出时恢复。)
内核中的相关代码可以在arch/x86/kernel/entry_32.S
。 由于广泛使用宏和各种细节(支持系统调用跟踪,DWARF调试注释等),但是: int 80h
处理程序是system_call
(我链接到的版本中的第493行) ; 寄存器通过SAVE_ALL
宏保存(第497行); 并在返回之前通过RESTORE_REGS
(行534)再次恢复。