我的汇编程序是 YASM,我在 64 位 Linux 上编码。
yasm -f elf -m amd64 -g dwarf2 filename.asm
我使用 ld组装和链接
我正在尝试实现选择排序。rdi
并rsi
指向strbuf2 resb 10
数组的各个部分。这种分段错误的原因可能是什么?第 105 行和第 106 行执行完全相同类型的操作,那么为什么它在第 106 行而不是第 105 行崩溃呢?
我已经包含了代码的相关部分,以及崩溃时的 gdbtui 屏幕截图。
更新:计数器已修复
; ====== Sorting begins here ======
; Register uses:
; bpl holds the current minimum value
; r8 holds the memory address of the current minimum value
; rdi points to the boundary of the "outer loop"
; rsi points to the boundary of the "inner loop"
sorting:
mov rdi, strbuf2 ; outer loop pointer
mov rsi, strbuf2+1 ; inner loop pointer
mov rax, 1 ; inner loop counter
mov rbx, 0 ; outer loop counter
innerloop:
mov bpl, [rdi] ; assume beginning element of unsorted array is minimum
; store the value of first element of unsorted array
mov dl, [rdi]
; compare the current small value with the value in rsi
mov cl, [rsi]
cmp bpl, cl
jg new_small
inc rsi
inc rax
cmp rax, 9
jle innerloop
jg innerloop_done
new_small:
inc rax
mov bpl, cl; save the new small value
mov r8, rsi ; save its index
inc rsi
cmp rax, 9
jle innerloop
innerloop_done:
; When the inner loop is completed...
; First, do the swap
; to swap r8 (target memory address) with [rdi] (outer array boundary)
mov dl, 0 ; initialise
mov dl, [rdi]
mov [rdi], bpl
mov [r8], dl
inc rdi ; move the outer loop pointer forward
inc rsi ; move the inner loop pointer forward
inc rbx ; increment the outer loop counter (the unsorted array becomes smaller)
; set the inner loop counter to the appropriate position
mov rax, 1
add rax, rbx ; now rax (inner loop counter)
; will always be rbx+1 (outer loop counter + 1)
cmp rbx, 9
jle innerloop
; ====== Sorting ends here ======
分段错误 gdb 输出