私のアセンブラーはYASMで、64ビットLinuxでコーディングしています。
ldを使用して組み立て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出力
セグメンテーション違反のgdb出力