Why doesn’t my code sort numbers greater than 128?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>_start:
mov rdx, len ; Store the number of elements in rdx (len - 1)
dec rdx ; Subtract 1 to get the index of the last element
mov rsi, 0 ; Store the starting index (0) in rsi
lea rdi, [array] ; Load the address of the array into rdi
call mergesort ; Call the mergesort function
call print_arr ; Call the function to print the sorted array
mov rax, 60 ; System call number for exit
xor rdi, rdi ; Exit status 0
syscall
mergesort:
cmp rsi, rdx ; Compare the starting and ending indices
jge _done ; If starting index is greater than or equal to the ending index, exit
push rbx
push rdx ; Push the ending index onto the stack
push rsi ; Push the starting index onto the stack
xor rbx, rbx ; Initialize rbx to 0
add rbx, rsi ; Add the starting index to rbx
add rbx, rdx ; Add the ending index to rbx
shr rbx, 1 ; Divide rbx by 2 to find the middle index
mov rsi, rbx ; Store the middle index in rsi
inc rsi ; Move to the starting index of the right sub-array (mid + 1)
call mergesort ; Call mergesort for the right sub-array
mov rdx, rbx ; Store the middle index in rdx
pop rsi ; Pop the starting index from the stack
call mergesort ; Call mergesort for the left sub-array
pop rdx ; Pop the ending index from the stack
mov rcx, rdx ; Store the ending index in rcx
mov rdx, rbx ; Store the middle index in rdx
call merge ; Call the merge function
pop rbx
_done:
ret
merge:
push r15
push rcx ; Push the ending index onto the stack
push rdx ; Push the middle index onto the stack
push rsi ; Push the starting index onto the stack
mov r15, rsi ; Store the starting index of the sorted array in r15
mov r8, rsi ; Store the starting index of the left sub-array in r8
mov r9, rdx ; Store the middle index in r9
inc r9 ; Move to the starting index of the right sub-array
lea r10, [rdi + r8] ; Load the address of the starting index of the left sub-array into r10
lea r11, [rdi + r9] ; Load the address of the starting index of the right sub-array into r11
lea r12, [sorted + r15] ; Load the address of the starting index of the sorted array into r12
L1 :
cmp r8, rdx ; Compare the left sub-array index with the middle index
jg L4 ; If the left sub-array index is greater than the middle index, jump to L4
cmp r9, rcx ; Compare the right sub-array index with the ending index
jg L4 ; If the right sub-array index is greater than the ending index, jump to L4
L5 :
mov al, byte [r11] ; Move the byte at the right sub-array index into al
cmp al, byte [r10] ; Compare al with the byte at the left sub-array index
jg L3 ; If al is greater than the byte at the left sub-array index, jump to L3
mov [r12], al ; Move al into the sorted array
inc r11 ; Increment the right sub-array index
inc r9 ; Increment the right sub-array counter
inc r15 ; Increment the sorted array index
inc r12 ; Move to the next position in the sorted array
jmp L1
L3 :
mov al, byte [r10] ; Move the byte at the left sub-array index into al
mov [r12], al ; Move al into the sorted array
inc r10 ; Increment the left sub-array index
inc r8 ; Increment the left sub-array counter
inc r15 ; Increment the sorted array index
inc r12 ; Move to the next position in the sorted array
jmp L1
L4 :
cmp r8, rdx ; Compare the left sub-array index with the middle index
jle copyleft_1 ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_1
jmp copyright_1 ; Otherwise, jump to copyright_1
copyright_1 :
lea r13, [rdi+r9] ; Load the address of the right sub-array index into r13
copyright_2 :
cmp r9, rcx ; Compare the right sub-array index with the ending index
jle copyright_3 ; If the right sub-array index is less than or equal to the ending index, jump to copyright_3
jg fin
copyright_3 :
mov al, byte[r13] ; Move the byte at the right sub-array index into al
mov [r12], al ; Move al into the sorted array
inc r9 ; Increment the right sub-array index
inc r12 ; Move to the next position in the sorted array
inc r13 ; Move to the next byte in the right sub-array
jmp copyright_2
copyleft_1 :
lea r14, [rdi+r8] ; Load the address of the left sub-array index into r14
lea r12 ,[sorted +r15] ; Load the address of the sorted array index into r12
copyleft_2 :
cmp r8, rdx ; Compare the left sub-array index with the middle index
jle copyleft_3 ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_3
jg fin
copyleft_3 :
mov al, byte[r14] ; Move the byte at the left sub-array index into al
mov [r12], al ; Move al into the sorted array
inc r12 ; Move to the next position in the sorted array
inc r8 ; Increment the left sub-array index
inc r15 ; Increment the sorted array index
inc r13 ; Move to the next byte in the left sub-array
jmp copyleft_2
fin:
lea r8, [rdi+rsi] ; Load the address of the starting index of the left sub-array into r8
lea r9, [sorted+rsi] ; Load the address of the starting index of the sorted array into r9
mov r10, rsi ; Move the starting index of the sorted array into r10
movearray:
mov al, byte [r9] ; Move the byte from the sorted array into al
mov [r8], al ; Move al into the array
inc r8 ; Move to the next position in the array
inc r9 ; Move to the next position in the sorted array
inc r10 ; Increment the index counter
cmp r10, rcx ; Compare the index counter with the ending index
jle movearray ; If the index counter is less than or equal to the ending index, jump to movearray
pop rsi ; Restore the values of the registers
pop rdx
pop rcx
pop r15
ret ; Return from the function
print_arr:
lea rsi, [array] ; Load the address of the array into rsi
mov rcx, len ; Move the length of the array into rcx
jmp print_loop ; Jump to the print_loop label
print_loop:
movzx r8d, byte[rsi] ; Move the current element of the array into r8d
printNumber r8d ; Print the current element
inc rsi ; Move to the next element in the array
loop print_loop ; Repeat until all elements are printed
ret ; Return from the function
</code>
<code>_start: mov rdx, len ; Store the number of elements in rdx (len - 1) dec rdx ; Subtract 1 to get the index of the last element mov rsi, 0 ; Store the starting index (0) in rsi lea rdi, [array] ; Load the address of the array into rdi call mergesort ; Call the mergesort function call print_arr ; Call the function to print the sorted array mov rax, 60 ; System call number for exit xor rdi, rdi ; Exit status 0 syscall mergesort: cmp rsi, rdx ; Compare the starting and ending indices jge _done ; If starting index is greater than or equal to the ending index, exit push rbx push rdx ; Push the ending index onto the stack push rsi ; Push the starting index onto the stack xor rbx, rbx ; Initialize rbx to 0 add rbx, rsi ; Add the starting index to rbx add rbx, rdx ; Add the ending index to rbx shr rbx, 1 ; Divide rbx by 2 to find the middle index mov rsi, rbx ; Store the middle index in rsi inc rsi ; Move to the starting index of the right sub-array (mid + 1) call mergesort ; Call mergesort for the right sub-array mov rdx, rbx ; Store the middle index in rdx pop rsi ; Pop the starting index from the stack call mergesort ; Call mergesort for the left sub-array pop rdx ; Pop the ending index from the stack mov rcx, rdx ; Store the ending index in rcx mov rdx, rbx ; Store the middle index in rdx call merge ; Call the merge function pop rbx _done: ret merge: push r15 push rcx ; Push the ending index onto the stack push rdx ; Push the middle index onto the stack push rsi ; Push the starting index onto the stack mov r15, rsi ; Store the starting index of the sorted array in r15 mov r8, rsi ; Store the starting index of the left sub-array in r8 mov r9, rdx ; Store the middle index in r9 inc r9 ; Move to the starting index of the right sub-array lea r10, [rdi + r8] ; Load the address of the starting index of the left sub-array into r10 lea r11, [rdi + r9] ; Load the address of the starting index of the right sub-array into r11 lea r12, [sorted + r15] ; Load the address of the starting index of the sorted array into r12 L1 : cmp r8, rdx ; Compare the left sub-array index with the middle index jg L4 ; If the left sub-array index is greater than the middle index, jump to L4 cmp r9, rcx ; Compare the right sub-array index with the ending index jg L4 ; If the right sub-array index is greater than the ending index, jump to L4 L5 : mov al, byte [r11] ; Move the byte at the right sub-array index into al cmp al, byte [r10] ; Compare al with the byte at the left sub-array index jg L3 ; If al is greater than the byte at the left sub-array index, jump to L3 mov [r12], al ; Move al into the sorted array inc r11 ; Increment the right sub-array index inc r9 ; Increment the right sub-array counter inc r15 ; Increment the sorted array index inc r12 ; Move to the next position in the sorted array jmp L1 L3 : mov al, byte [r10] ; Move the byte at the left sub-array index into al mov [r12], al ; Move al into the sorted array inc r10 ; Increment the left sub-array index inc r8 ; Increment the left sub-array counter inc r15 ; Increment the sorted array index inc r12 ; Move to the next position in the sorted array jmp L1 L4 : cmp r8, rdx ; Compare the left sub-array index with the middle index jle copyleft_1 ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_1 jmp copyright_1 ; Otherwise, jump to copyright_1 copyright_1 : lea r13, [rdi+r9] ; Load the address of the right sub-array index into r13 copyright_2 : cmp r9, rcx ; Compare the right sub-array index with the ending index jle copyright_3 ; If the right sub-array index is less than or equal to the ending index, jump to copyright_3 jg fin copyright_3 : mov al, byte[r13] ; Move the byte at the right sub-array index into al mov [r12], al ; Move al into the sorted array inc r9 ; Increment the right sub-array index inc r12 ; Move to the next position in the sorted array inc r13 ; Move to the next byte in the right sub-array jmp copyright_2 copyleft_1 : lea r14, [rdi+r8] ; Load the address of the left sub-array index into r14 lea r12 ,[sorted +r15] ; Load the address of the sorted array index into r12 copyleft_2 : cmp r8, rdx ; Compare the left sub-array index with the middle index jle copyleft_3 ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_3 jg fin copyleft_3 : mov al, byte[r14] ; Move the byte at the left sub-array index into al mov [r12], al ; Move al into the sorted array inc r12 ; Move to the next position in the sorted array inc r8 ; Increment the left sub-array index inc r15 ; Increment the sorted array index inc r13 ; Move to the next byte in the left sub-array jmp copyleft_2 fin: lea r8, [rdi+rsi] ; Load the address of the starting index of the left sub-array into r8 lea r9, [sorted+rsi] ; Load the address of the starting index of the sorted array into r9 mov r10, rsi ; Move the starting index of the sorted array into r10 movearray: mov al, byte [r9] ; Move the byte from the sorted array into al mov [r8], al ; Move al into the array inc r8 ; Move to the next position in the array inc r9 ; Move to the next position in the sorted array inc r10 ; Increment the index counter cmp r10, rcx ; Compare the index counter with the ending index jle movearray ; If the index counter is less than or equal to the ending index, jump to movearray pop rsi ; Restore the values of the registers pop rdx pop rcx pop r15 ret ; Return from the function print_arr: lea rsi, [array] ; Load the address of the array into rsi mov rcx, len ; Move the length of the array into rcx jmp print_loop ; Jump to the print_loop label print_loop: movzx r8d, byte[rsi] ; Move the current element of the array into r8d printNumber r8d ; Print the current element inc rsi ; Move to the next element in the array loop print_loop ; Repeat until all elements are printed ret ; Return from the function </code>
_start:
    mov rdx, len            ; Store the number of elements in rdx (len - 1)
    dec rdx                 ; Subtract 1 to get the index of the last element
    mov rsi, 0              ; Store the starting index (0) in rsi
    lea rdi, [array]        ; Load the address of the array into rdi
    call mergesort          ; Call the mergesort function

    call print_arr          ; Call the function to print the sorted array
    
    mov rax, 60             ; System call number for exit
    xor rdi, rdi            ; Exit status 0
    syscall

mergesort:
    cmp rsi, rdx            ; Compare the starting and ending indices
    jge _done               ; If starting index is greater than or equal to the ending index, exit
    push rbx                
    push rdx                ; Push the ending index onto the stack
    push rsi                ; Push the starting index onto the stack

    xor rbx, rbx            ; Initialize rbx to 0
    add rbx, rsi            ; Add the starting index to rbx
    add rbx, rdx            ; Add the ending index to rbx
    shr rbx, 1              ; Divide rbx by 2 to find the middle index

    mov rsi, rbx            ; Store the middle index in rsi
    inc rsi                 ; Move to the starting index of the right sub-array (mid + 1)
    call mergesort          ; Call mergesort for the right sub-array

    mov rdx, rbx            ; Store the middle index in rdx
    pop rsi                 ; Pop the starting index from the stack
    call mergesort          ; Call mergesort for the left sub-array

    pop rdx                 ; Pop the ending index from the stack
    mov rcx, rdx            ; Store the ending index in rcx
    mov rdx, rbx            ; Store the middle index in rdx
    call merge              ; Call the merge function

    pop rbx
_done:
    ret

merge:
    push r15
    push rcx                ; Push the ending index onto the stack
    push rdx                ; Push the middle index onto the stack
    push rsi                ; Push the starting index onto the stack

    mov r15, rsi            ; Store the starting index of the sorted array in r15
    mov r8, rsi             ; Store the starting index of the left sub-array in r8
    mov r9, rdx             ; Store the middle index in r9
    inc r9                  ; Move to the starting index of the right sub-array
    lea r10, [rdi + r8]     ; Load the address of the starting index of the left sub-array into r10
    lea r11, [rdi + r9]     ; Load the address of the starting index of the right sub-array into r11
    lea r12, [sorted + r15] ; Load the address of the starting index of the sorted array into r12
   
L1 : 
    cmp r8, rdx             ; Compare the left sub-array index with the middle index
    jg L4                   ; If the left sub-array index is greater than the middle index, jump to L4
    cmp r9, rcx             ; Compare the right sub-array index with the ending index
    jg L4                   ; If the right sub-array index is greater than the ending index, jump to L4
    
L5 : 
    mov al, byte [r11]     ; Move the byte at the right sub-array index into al
    cmp al, byte [r10]     ; Compare al with the byte at the left sub-array index
    jg L3                   ; If al is greater than the byte at the left sub-array index, jump to L3
    mov [r12], al           ; Move al into the sorted array
    inc r11                 ; Increment the right sub-array index
    inc r9                  ; Increment the right sub-array counter
    inc r15                 ; Increment the sorted array index
    inc r12                 ; Move to the next position in the sorted array
    jmp L1
    
L3 : 
    mov al, byte [r10]     ; Move the byte at the left sub-array index into al
    mov [r12], al           ; Move al into the sorted array
    inc r10                 ; Increment the left sub-array index
    inc r8                  ; Increment the left sub-array counter
    inc r15                 ; Increment the sorted array index
    inc r12                 ; Move to the next position in the sorted array
    jmp L1

L4 :
    cmp r8, rdx             ; Compare the left sub-array index with the middle index
    jle copyleft_1          ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_1
    jmp copyright_1         ; Otherwise, jump to copyright_1

copyright_1 : 
    lea r13, [rdi+r9]      ; Load the address of the right sub-array index into r13
    
copyright_2 : 
    cmp r9, rcx             ; Compare the right sub-array index with the ending index
    jle copyright_3         ; If the right sub-array index is less than or equal to the ending index, jump to copyright_3
    jg fin

copyright_3 : 
    mov al, byte[r13]       ; Move the byte at the right sub-array index into al
    mov [r12], al           ; Move al into the sorted array
    inc r9                  ; Increment the right sub-array index
    inc r12                 ; Move to the next position in the sorted array
    inc r13                 ; Move to the next byte in the right sub-array
    jmp copyright_2

copyleft_1 : 
    lea r14, [rdi+r8]      ; Load the address of the left sub-array index into r14
    lea r12 ,[sorted +r15] ; Load the address of the sorted array index into r12
    
copyleft_2 :    
    cmp r8, rdx             ; Compare the left sub-array index with the middle index
    jle copyleft_3          ; If the left sub-array index is less than or equal to the middle index, jump to copyleft_3
    jg fin
    
copyleft_3 : 
    mov al, byte[r14]       ; Move the byte at the left sub-array index into al
    mov [r12], al           ; Move al into the sorted array
    inc r12                 ; Move to the next position in the sorted array
    inc r8                  ; Increment the left sub-array index
    inc r15                 ; Increment the sorted array index
    inc r13                 ; Move to the next byte in the left sub-array
    jmp copyleft_2

fin:
    lea r8, [rdi+rsi]       ; Load the address of the starting index of the left sub-array into r8
    lea r9, [sorted+rsi]    ; Load the address of the starting index of the sorted array into r9
    mov r10, rsi            ; Move the starting index of the sorted array into r10
    
movearray:
    mov al, byte [r9]       ; Move the byte from the sorted array into al
    mov [r8], al            ; Move al into the array
    inc r8                  ; Move to the next position in the array
    inc r9                  ; Move to the next position in the sorted array
    inc r10                 ; Increment the index counter
    cmp r10, rcx            ; Compare the index counter with the ending index
    jle movearray           ; If the index counter is less than or equal to the ending index, jump to movearray

    pop rsi                 ; Restore the values of the registers
    pop rdx
    pop rcx
    pop r15
    
    ret                     ; Return from the function

print_arr: 
    lea rsi, [array]        ; Load the address of the array into rsi
    mov rcx, len            ; Move the length of the array into rcx
    jmp print_loop          ; Jump to the print_loop label

print_loop:
    movzx r8d, byte[rsi]    ; Move the current element of the array into r8d
    printNumber r8d         ; Print the current element
    inc rsi                 ; Move to the next element in the array
    loop print_loop         ; Repeat until all elements are printed

    ret                     ; Return from the function

This code is for sorting numbers declared in an array using db. In my understanding, since db represents a 1-byte number, it should be able to sort any number between 0 and 255. However, when I actually insert numbers greater than 128 into the array, sorting doesn’t occur, and the code breaks. I want to know why this problem occurs.

I want to know why this code can’t sort numbers over 128. 8 bits number can express 0~255, but this code can’t handle number over 128. Plz help me.

New contributor

user25004318 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật