问题描述
我有一个汇编程序,该程序需要三个参数(指向int数组,大小和目标的指针),并且需要对线性搜索进行编程,但是在我尝试过的每个实例中,我始终会收到分段错误。
section .text
; function should return the index at which the target was found
; Parameter 1 is a pointer to the int array
; Parameter 2 is the size of the array
; Parameter 3 is the target element to find in the array
linearSearch:
push rbx ; int i[] (the array)
push r12 ; int j (array.size())
push r13 ; int k (the element to find)
xor r10,r10 ; pointer to an index of the array
xor r14,r14 ; count to keep track of size searched
xor rax,rax ; initialize rax as 0 to avoid random nonsense
jmp loop ; head into loop to find element
mov r14,0 ; initialize count as 0
mov r10,rbx ; r10 = first element of i[]
loop:
cmp r14,r12 ; count >= j ? jmp end : continue
jae endNotFound ; jump to int not found end
cmp r10,r13 ; i[r10] == k ? je end : continue
je end ; jump to int found end
inc r14 ; increase counter for amount searched by one
mov r10,[rbx+4*r14] ; set index pointer equal to r14+1 (4 being the size of int in 64-bit)
jmp loop ; rinse and repeat
endNotFound:
mov rax,-1 ; by convention,return -1 if the int cannot be found
pop r13
pop r12
pop rbx
ret
end:
mov rax,r10 ; move index pointer to rax to be returned
pop r13
pop r12
pop rbx
ret
最初,我认为问题是我遇到某种随机变量问题,因此我使用xor将非参数变量初始化为0,但仍然收到相同的错误。我想念的是什么?或者更好的是,我在概念上忘记了什么一直导致这些细分错误,因为这不是我第一次遇到此问题,而且可能是同一回事。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)