【发布时间】:2017-12-29 14:15:02
【问题描述】:
我从这个简单的启动程序中遇到了分段错误。
我正在使用 Ubuntu 16.10 和 kdbg 进行调试。到达起点__int 80h__ 后,停止移动到下一行。
section .bss ; section containing uninitialized data
BUFFLEN equ 1024 ; length of buffer
Buff: resb BUFFLEN ; text buffer itself
section .data ; section containing initialzed data
section .text ; secttion containing code
global _start ; linker needs to find the entry point!
_start:
nop ; this no-op keeps gdb happy
; read buffer full of text form stdin:
read:
mov eax, 3 ; specify sys_read call
mov ebx, 0 ; specify file descriptor 0 : standard input
mov ecx, Buff ; pass offset of the buffer to read to
mov edx, BUFFLEN ; pass number of bytes to be read at one pass
int 80h ; call sys_read to fill the buffer
mov esi,eax ; copy sys_read return value for safekeeping
cmp eax, 0 ; if eax = 0 , sys_read reached EOF on stdin
je Done ; jump if Equal ( to o, form compare)
; set up the register for the process buffer step:
mov ecx, esi ; place the number of bytes read into ecx
mov ebp, Buff ; pace address of buffer into ebp
dec ebp ; adjust the count to offset
; go through the buffer and cnvert lowercase to uppercase characters:
Scan:
cmp byte [ebp+ecx], 61h ; test input char agaisnst lowercase 'a'
jb Next ; if Below 'a' in ASCII, not lowercase
cmp byte [ebp+ecx], 7Ah ; test against lowercase 'z'
ja Next
sub byte [ebx+ecx], 20h ; subtract 20h to give uppercase..
Next:
dec ecx ; Decrement counter
jnz Scan ; if characters reamin, loop back
; Write the buffer full of processed text to stdout:
Write:
mov eax,4 ; Specify sys_write call
mov ebx, 1 ; Specify file descriptor 1 : stdout
mov ecx, Buff ; pass the offset of the buffer
mov edx, esi ; pass the # of bytes of data in the buffer
int 80h ; make sys_write kernel call
jmp read ; loop back and load another buffer full
Done:
mov eax, 1 ; Code for Exit sys_call
mov ebx, 0 ; return code of Zero
int 80h
我使用了这些命令:
nasm -f elf -g -F stabs uppercaser1.asm
ld -m elf_i386 -o uppercaser1 uppercaser1.o
./uppercaser < inputflie
【问题讨论】:
-
您真的遇到了分段错误还是它没有继续(即它看起来好像挂起)?这是一个巨大的差异。
-
我建议您通过调试器运行您的代码。它应该告诉您在代码中的哪个位置崩溃。使用
gdb ./uppercaser1例如你使用sub byte [ebx+ecx], 20h。我认为你的意思是使用sub byte [ebp+ecx], 20h? -
您是否尝试在不定向外部文件的情况下运行(即 ./uppercaser
-
在读取系统调用之后输入小写字符时会发生 seg 错误。我可以输入大写、数字和其他字符;这确实会导致段错误。我的计算机上有几乎相同代码的工作副本。这是作业还是来自《Assembly Language Step-by-Step: Programming with Linux 3rd Edition》一书?
-
谢谢,它应该是 ebp ,但是为什么它在执行第一个 int 80h @MichaelPetch 时会在 kdbg 中停止