汇编代码程序发生分段错误
运行代码时,我不断收到分段错误错误。一切都编译得很好,但我似乎无法让它做我想做的事。该程序要求用户输入 3 个整数,然后询问用户他们认为这些数字的平均值是多少,考虑到这一点,然后返回用户是否猜对了
segment .data
;
; Output strings
;
prompt1 db "Enter a positive integer: ", 0
prompt2 db "Enter a second positive integer: ", 0
prompt3 db "Enter a third positive integer: ", 0
prompt4 db "Enter a guess of their average: ", 0
outmsg1 db "You entered ", 0
outmsg2 db " and ", 0
outmsg3 db " and ", 0
outmsg4 db "You guessed that the average is ", 0
outmsg5 db "You did you guess correctly? (0 = no, 1 = yes)", 0
avermsg db "The average of the numbers is ", 0
segment .bss
input1 resd 1
input2 resd 1
input3 resd 1
input4 resd 1
guess resd 1
segment .text
Global main
main:
enter 0,0 ; setup routine
pusha
mov eax, prompt1 ; print out prompt1
call print_string
call read_int ; read integer
mov [input1], eax ; store integer into input1
mov eax, prompt2 ; print out prompt2
call print_string
call read_int ; read integer
mov [input2], eax ; store integer into input2
mov eax, prompt3 ; print out prompt3
call print_string
call read_int ; read integer
mov [input3], eax ; store integer into input3
mov eax, prompt4 ; print out prompt4
call print_string
call read_int ; read integer
mov [guess], eax
mov eax, [input1] ; eax = dword at input1
add eax, [input2] ; eax += dword at input2
add eax, [input3] ; eax += dword at input3
mov ebx, 3
div ebx ; divides the sum by 3
mov ecx, eax ; freeing up eax, puts quotient into ecx
dump_regs 1 ; print out register values
; next print out results
mov eax, outmsg1
call print_string ; print out first message
mov eax, [input1]
call print_int
mov eax, outmsg2
call print_string ; print out second message
mov eax, [input2]
call print_int
mov eax, outmsg3
call print_string ; print out thrid message
mov eax, [input3]
call print_int
mov eax, outmsg4
call print_string ; print out fourth message
mov eax, [input4]
call print_int
xor ebx, ebx
cmp ecx, [guess]
sete bl
neg ebx
mov edx, ebx
and ecx, edx
not ebx
and ebx, [guess]
or edx, ebx
mov eax, outmsg5
call print_string
mov ecx, eax
call print_int
mov eax, [avermsg]
call print_string ; print out final message
mov ecx, edx
call print_int ; print out average of ebx
call print_nl ; print new line
popa
mov eax, 0 ; return back to C
leave
ret
I keep getting a segmentation fault error when running my code. Everything has compiled well, but I can't seem to get it to do what I want. The program is to ask the user to enter 3 integers, then ask the user what they think the average of the numbers would be, take that into account and then come back with whether or not the user guessed correctly
segment .data
;
; Output strings
;
prompt1 db "Enter a positive integer: ", 0
prompt2 db "Enter a second positive integer: ", 0
prompt3 db "Enter a third positive integer: ", 0
prompt4 db "Enter a guess of their average: ", 0
outmsg1 db "You entered ", 0
outmsg2 db " and ", 0
outmsg3 db " and ", 0
outmsg4 db "You guessed that the average is ", 0
outmsg5 db "You did you guess correctly? (0 = no, 1 = yes)", 0
avermsg db "The average of the numbers is ", 0
segment .bss
input1 resd 1
input2 resd 1
input3 resd 1
input4 resd 1
guess resd 1
segment .text
Global main
main:
enter 0,0 ; setup routine
pusha
mov eax, prompt1 ; print out prompt1
call print_string
call read_int ; read integer
mov [input1], eax ; store integer into input1
mov eax, prompt2 ; print out prompt2
call print_string
call read_int ; read integer
mov [input2], eax ; store integer into input2
mov eax, prompt3 ; print out prompt3
call print_string
call read_int ; read integer
mov [input3], eax ; store integer into input3
mov eax, prompt4 ; print out prompt4
call print_string
call read_int ; read integer
mov [guess], eax
mov eax, [input1] ; eax = dword at input1
add eax, [input2] ; eax += dword at input2
add eax, [input3] ; eax += dword at input3
mov ebx, 3
div ebx ; divides the sum by 3
mov ecx, eax ; freeing up eax, puts quotient into ecx
dump_regs 1 ; print out register values
; next print out results
mov eax, outmsg1
call print_string ; print out first message
mov eax, [input1]
call print_int
mov eax, outmsg2
call print_string ; print out second message
mov eax, [input2]
call print_int
mov eax, outmsg3
call print_string ; print out thrid message
mov eax, [input3]
call print_int
mov eax, outmsg4
call print_string ; print out fourth message
mov eax, [input4]
call print_int
xor ebx, ebx
cmp ecx, [guess]
sete bl
neg ebx
mov edx, ebx
and ecx, edx
not ebx
and ebx, [guess]
or edx, ebx
mov eax, outmsg5
call print_string
mov ecx, eax
call print_int
mov eax, [avermsg]
call print_string ; print out final message
mov ecx, edx
call print_int ; print out average of ebx
call print_nl ; print new line
popa
mov eax, 0 ; return back to C
leave
ret
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果不知道您使用的编译器,则很难查明问题所在。
分段错误发生在保护模式下,当您尝试访问您无权访问的分段时。
您在此处声明 3 个不同的段。在调用
print_string
之前,您必须确保您的ds
寄存器已初始化为.data
段。这似乎也是有问题的,在
read_int
之后,您将数据保存到input1
变量中,该变量似乎与您用于打印消息的段位于不同的段中,但您不这样做更改ds
。我不熟悉您的编译器如何准确处理这些段,因此请提供其文档的链接。
It is not easy to pinpoint the problem without knowing the compiler you use.
Segmentation fault hapens in protected mode, when you try to access a segment you have no permissions to access.
You declare 3 diferent segments here. You have to make sure your
ds
register is initialized to your.data
segment, before callingprint_string
.It also seems problematic that after
read_int
you save the data to theinput1
variable which seems to be in a different segment than that you used for printing the message, but you do not changeds
.I'm not familiar how exactly your compiler handles these segments, so please give a link to its documentation.
问题似乎就在这里。您必须将 edx 归零,因为 is 是除数的高位字,因此您可能会遇到类似除法溢出异常的情况。
如果情况并非如此,则问题可能出在您的某些 I/O 例程中
the problem seems to be here. You have to zero out edx since is is the high order word of the divisor, so probably you're getting something like a division overflow exception.
If this isn't the case probably the problem is in some of your I/O routines