编写汇编程序合并逆序数组时触发SIGSEGV段错误求助
逆序数组合并汇编程序段错误排查
需要编写x86汇编程序,将两个逆序排序的数组合并为一个新的逆序排序数组。现有代码运行时触发SIGSEGV(段错误),错误发生在elementArray1Greater标签处。曾怀疑leal 4(%ecx), %ecx指令导致问题,但移除该指令后仍报错。已获取objdump指令信息及寄存器状态,请求协助排查故障。
汇编代码
.global _start .section .data array1: .int 6,4,0 array2: .int 5,3,0 mergedArray: .zero 20 .section .text _start: # set up the input array pointers leal array1, %eax # pointer to the first element of array1 leal array2, %ebx # pointer to the first element of array2 # set up the output array pointer leal mergedArray, %ecx # pointer to the first element of output_array # set up the loop counters movl $0, %edx # counter for array1 movl $0, %esi # counter for array2 loop: # compare the current elements of the two arrays movl (%eax,%edx,4), %edi # load the current element of array1 into %edi movl (%ebx,%esi,4), %ebp # load the current element of array2 into %ebp cmpl %edi, %ebp # compare the two elements jg elementArray1Greater # if the element from array1 is greater than the element from array1, add it to the output array jl elementArray2Greater # if the element from array2 is greater than the element from array2, add it to the output array jmp equal elementArray1Greater: movl %edi, (%ecx,%edx,4) # add the current element of array1 to the output array leal 4(%ecx), %ecx addl $4, %edx # advance the counter for array1 jmp loop elementArray2Greater: movl %ebp, (%ecx,%esi,4) # add the current element of array2 to the output array leal 4(%ecx), %ecx addl $4, %esi # advance the counter for array2 jmp loop equal: cmpl $0, %edi je finish movl %edi, (%ecx,%edx,4) # add the current element of array1 to the output array leal 4(%ecx), %ecx addl $4, %edx addl $4, %esi jmp loop finish: movl %ebp, (%ecx,%esi,4) # add the current element of array2 to the output array # exit movl $0, %eax movl $1, %ebx int $0x80
错误信息
Program received signal SIGSEGV, Segmentation fault. 0x00000000004000df in elementArray1Greater ()
内容的提问来源于stack exchange,提问作者Algo
相关产品推荐
相关产品推荐

