0

I'm writing the beginning of a bubble sort in yasm, as an exercise. But I'm segfaulting every time at the last instruction below and I don't understand why.

    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

My understanding is cmpsb compares the byte in si and di. Why should it segfault? This must be really simple error on my part but I don't see it. Usually cmpsb is used in the context of a repe, but I thought it would work here too. Thanks for any help!

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328

1 Answers1

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