为什么我的代码不能对大于 128 的数字进行排序?

问题描述 投票:0回答:1
_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

此代码用于对使用 db.array 中声明的数字进行排序。根据我的理解,由于 db 代表一个 1 字节的数字,所以它应该能够对 0 到 255 之间的任何数字进行排序。但是,当我实际将大于 128 的数字插入到数组中时,排序不会发生,并且代码会中断。我想知道为什么会出现这个问题。

我想知道为什么这个代码不能对超过128的数字进行排序。8位数字可以表示0~255,但是这个代码不能处理超过128的数字。请帮助我。

assembly x86-64
1个回答
0
投票

您需要使用无符号跳转

    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
    ja L3                  ; unsigned jump (jmp above)
© www.soinside.com 2019 - 2024. All rights reserved.