0

演習として、yasm でバブルソートの始まりを書いています。しかし、私は以下の最後の命令で毎回セグメンテーション違反をしており、その理由がわかりません。

    segment .data

arr     db      5,6,2,3,8,1

    segment .text
    global  main 
main:   
    xor ecx, ecx                ; counter
    mov rdx, 6                  ; sizeof(arr)
    cld

_Do:    xor eax, eax            ; set swapped = false

for:
    movzx esi, byte [arr+ecx]
    movzx edi, byte [arr+ecx+1]
    cmpsb                       ; is a[i]>a[i+1]? <--- segfault here every time
    ;jump to swap next, if I could get there

私の理解では、cmpsb は si と di のバイトを比較します。なぜセグメンテーション違反になるのでしょうか? これは私の側では本当に単純なエラーに違いありませんが、私にはわかりません。通常、cmpsb は repe のコンテキストで使用されますが、ここでも機能すると思いました。助けてくれてありがとう!

4

1 に答える 1

2

CMPSB doesn't compare the contents of two registers -- that's what normal CMP is for. Instead, it treats the registers as addresses and compares the two values they point to. Try something like:

lea esi, byte [arr + ecx]
lea edi, byte [arr + ecx + 1]
cmpsb
于 2013-02-10T04:07:32.560 に答える