assembly 程序中的“分段故障(核心转储)”

oewdyzsn  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(223)

我使用了gdb-peda,它说错误是在inner_loop标签中产生的,这个过程应该执行两个方阵的乘法
'

matrix_mult:
pushl %ebp        # save the value of ebp on the stack
movl %esp, %ebp   # set up the stack frame 
    
xorl %eax, %eax   # initialize the loop counter
movl 8(%ebp), %ebx # get the pointer to matrix A
movl 12(%ebp), %ecx # get the pointer to matrix B
movl 16(%ebp), %edx # get the pointer to matrix C
movl 20(%ebp), %esi # get the value of N

matrix_loop:
pushl %esi        # save the value of esi on the stack
movl $0, %esi     # initialize the inner loop counter
movl $0, %ebp     # initialize the result for this element

inner_loop:
movl (%ebx,%esi,4), %eax # get the element from matrix A
mull (%ecx,%esi,4) # multiply it by the element from matrix B
addl %eax, %ebp   # add the result to ebp
addl $1, %esi       # increment the loop counter
73: movl 20(%ebp),%edi 
74: cmpl %edi, %esi # compare to N
75: jl inner_loop     # if the loop counter is less than N, jump back to the beginning of the loop

#At this point, ebp contains the result for this element of the matrix
movl %ebp, (%edx) # store the result in the output matrix
addl $4, %edx     # move to the next element in the output matrix
popl %esi         # restore the value of esi
incl %eax           # increment the loop counter
movl 20(%ebp),%edi         
cmpl %edi, %eax # compare to N
jl matrix_loop    # if the loop counter is less than N, jump back to the beginning of the outer loop

popl %ebp
ret

'
当使用gdb-peda时,它在第73-75行之间断开,显示如下内容:

=> 0x565561e4 <inner_loop+11>:  mov    edi,DWORD PTR [ebp+0x14]
   0x565561e7 <inner_loop+14>:  cmp    esi,edi
   0x565561e9 <inner_loop+16>:  jle    0x565561d9 <inner_loop>`
ldfqzlk8

ldfqzlk81#

你问的是如何调试它。
一种方法如下:
1.读取或写入未分配给进程的内存导致分段错误
1.弄清楚故障的有效地址(不是指令,而是内存引用地址),当然会是非法的。
1.找出是谁计算了那个地址,然后解决问题。
一方面,您要查找代码中的错误(或缺失)指令,这可能是一个迭代过程:找到处理坏数据的好代码。因此,你必须找到产生坏数据的代码(另一次迭代),并重复直到你找到出错的地方。

相关问题