我已经使用 nasm 编译器和 ld 链接器用汇编语言编写了一个 hexdump 实用程序。该程序应该转储任何输入文件的十六进制值。但是它在一个特定的过程“LoadBuff”处出现段错误。loadbuff 的功能是读取输入一个 16 字节的缓冲区。代码是
LoadBuff:
push ebx
push edx
push eax
mov eax,3 ;sys_read call
mov ebx,0 ;read from standard input
mov ecx,Buff ;pass the buffer adress
mov edx,BuffLen ;pass the number of bytes to be read at a time
int 80h ;call the linux kernel
mov ebp,eax
;cmp eax,0 ;number of characters read is returned in eax
;jz exit ;if zero character is returned i.e end of iinput file
;jump to exit
xor ecx,ecx
pop eax
pop edx
pop ebx
ret
如果线路
;cmp eax,0
;jz exit
未注释代码运行良好,没有任何段错误。但是,当我评论它并将这些行包含在调用者中以便在调用者中而不是在这里进行相同的比较时,此过程出现段错误。
gdb 回溯给出
#0 0x00000000 in ?? ()
知道为什么会这样吗?