Debugging x86程序在调试MASM32汇编时收到信号SIGSEGV,分段错误
我的任务是编写我的第一个汇编代码,它接受用户输入的1-12之间的数字,并输出阶乘。我需要写3个程序一个用于输入,一个用于计算阶乘,一个用于打印结果。我相信我写的是正确的(我没有写),但是在得到用户的输入后,程序终止了。我从未看到“打印”过程的结果。尝试调试我的代码时,我遇到错误:Debugging x86程序在调试MASM32汇编时收到信号SIGSEGV,分段错误,debugging,assembly,x86,procedure,masm32,Debugging,Assembly,X86,Procedure,Masm32,我的任务是编写我的第一个汇编代码,它接受用户输入的1-12之间的数字,并输出阶乘。我需要写3个程序一个用于输入,一个用于计算阶乘,一个用于打印结果。我相信我写的是正确的(我没有写),但是在得到用户的输入后,程序终止了。我从未看到“打印”过程的结果。尝试调试我的代码时,我遇到错误: Program received signal SIGSEGV, Segmentation fault. 此错误发生在“调用输入”的第一步之后 这是我的完整代码: include c:\asmio\asm32.inc
Program received signal SIGSEGV, Segmentation fault.
此错误发生在“调用输入”的第一步之后
这是我的完整代码:
include c:\asmio\asm32.inc
includelib c:\asmio\asm32.lib
includelib c:\asmio\User32.lib ; SASM files for I/O
includelib c:\asmio\Kernel32.lib ; SASM files for I/O
input proto ; 0 parameters
Factorial proto nvx: dword ; 1 parameter
Print proto nax: dword, nf: dword ; 2 parameters
; -------------------------------------------------------
.const ; Section to declare and initialize constants
NULL = 0
; -------------------------------------------------------
.data ; Section to declare and initialize variables
nvx dword ?
nfx dword ?
nf dword ?
ask byte "Enter a number between 1-12: ", NULL
fin byte "! is ", NULL
; -------------------------------------------------------
.code ; The actual code begins here: Main program
main proc ; Just like C++ this is the main program
start:
invoke input
mov nvx, eax
invoke Factorial, nvx
mov nfx, eax
invoke Print, nvx, nfx
ret 0 ; need this line to return to caller
main endp ; End of the procedure main
; -------------------------------------------------------
input proc
mov edx, OFFSET ask
call WriteString
call ReadInt
ret
input endp
; -------------------------------------------------------
Factorial proc USES ECX EBX nv: dword
mov ecx, nv ;start loop counter at value given by user because we need to multiply this many times to find the factorial.
mov ebx, nv ; hold value of nv as divisor
inc nv ; This is to account for 0! We increment by one and after the loop divide by nv
mov eax, nv ; stores the largest number for multiplication
L1:
dec nv ; becomes next largest number
mul nv ; multiplication
mov eax, edx ; stores product into eax for next multiplication
loop L1
inc ebx
cdq
idiv ebx ; divide final product by original number + 1 for the correct factorial
ret
Factorial endp
Print proc nax: dword, na: dword
mov eax, nvx
call WriteString
mov eax, OFFSET fin
call WriteString
mov eax, na
call WriteString
ret
Print endp
end main ; End of the entire program
; -------------------------------------------------------
哪条指令出现故障?@PeterCordes调试器箭头指向“调用输入”,我单击“继续”并出现错误:“程序接收信号SIGSEGV,分段错误”我特别困惑,因为在正常运行程序时,我确实到达了请求用户输入的部分(这是过程中调试器似乎失败的指令。)
invoke
不是一条实际的x86指令;它是一条MASM伪指令/宏。什么实际的指令出现故障?查看反汇编窗口和寄存器值。它是否可能是某种链接器问题,在那里它被组装成一个带有错误指针的间接调用?@PeterCordes当我使用“单步执行”程序时命令,它似乎运行良好,直到我的第三个过程被标记为“打印”。在该过程中调用WriteString时,错误似乎出现了。nvx
不是字符串,而是数字。不要尝试在其上使用WriteString
,因为这会尝试将其作为指针取消引用,从而导致错误。请使用WriteInt
。对于na
也一样。PS:您应该在打印中使用nax
因为这就是论点。