assembly 分割故障[已关闭]

ou6hu8tu  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(145)

3天前关闭。
Improve this question

selectsort:
    push rbp           ; save old base pointer
    mov rbp, rsp       ; set up new stack frame
    sub rsp, 8         ; allocate space for local variables

    ; check if array size is zero or less than zero
    mov rax, [rbp + 16]  ; array size
    cmp rax, 0          ; compare with zero
    jle exit            ; if zero or less, exit
    jg  exit

    ; initialize number of swaps
    xor rax, rax        ; clear rax
    mov [rbp - 8], rax  ; store in local variable

    ; get array address and size
    mov rdi, [rbp + 8]  ; array address
    mov rcx, [rbp + 16] ; array size

    ; initialize loop counters
    xor rsi, rsi        ; clear rsi (current index)
    dec rcx             ; decrement array size (end index)
    mov [rbp - 4], rcx  ; store end index in local variable

outerloop:
    ; initialize minimum index
    mov r8, -1         ; set minimum index to -1

    mov rax, rsi        ; set current index to minimum index

    ; initialize inner loop counter
    mov rbx, rsi        ; set inner loop counter to current index

innerloop:
    ; check if current index is less than end index
    mov r9, [rbp - 4]
    cmp rbx, [rbp - 4]  ; compare with end index
    jge innerloopend    ; if greater or equal, end inner loop

    ; check if current element is less than minimum element
    mov r8, [rdi + rbx * 8]  ; get current element
    cmp r8, [rdi + rax * 8]  ; compare with minimum element
    jge innerloopinc    ; if greater or equal, increment inner loop counter

    ; update minimum index
    mov rdx, rbx        ; set minimum index to current index

innerloopinc:
    inc rbx             ; increment inner loop counter
    jmp innerloop       ; repeat inner loop

innerloopend:
    ; check if minimum index is less than current index
    cmp rdx, rsi        ; compare with current index  j

exit:
    mov rax, [rbp - 8]  ; return number of swaps
    pop rbp               ; restore stack frame
    ret
SECTION .data
    openPrompt  db  "Welcome to my Program", 0h
    closePrompt db  "Program ending, have a nice day", 0h
    msg1        db "The array was already sorted or the size was incorrect", 0h
    msg2        db "The total swaps done during the sort is: ", 0h
    arr1        dq  100h, 56h, 72h, 99h, 56h, 5h, 39h, 2h, 456h, 324h, 11h, 100h
                    .LENGTHOF equ ($- arr1)/8
SECTION .bss

SECTION     .text
    global      _start

 
_start:
    nop
    
    push    openPrompt
    call    PrintString
    call    Printendl
    
    mov rsi, arr1            ; address of array
    mov rdx, arr1.LENGTHOF              ; size of array
    call selectsort ; sort array
    
    cmp rax, 0h
    je zero
    push msg2
    call PrintString
    call Printendl
    mov rsi, rax
    
    zero:
    push msg1
    call PrintString
    call Printendl
   
    push    closePrompt         ;The prompt address - argument #1
    call    PrintString
    call    Printendl
    
    nop
;
;Setup the registers for exit and poke the kernel
;Exit: 
Exit:
    mov     rax, 60                 ;60 = system exit
    mov     rdi, 0                  ;0 = return code
    syscall                         ;Poke the kernel

我试过改变不同的寄存器,但没有效果。我应该怎么做才能消除我的分段错误?

xbp102n0

xbp102n01#

“无segfaults,但运行不正常”

一个一个
您正在使用RDX和RSI中的寄存器参数调用 SelectSort 代码,但您尝试从堆栈中检索数组大小(RDX)!您在RAX中加载的内容将是垃圾。
由于jle exitjg exit之间涵盖了所有可能性,排序将始终中止,并返回带有“交换次数”的RAX,这也是垃圾,因为[rbp - 8]从未被写入。

ToDo:在打印 * msg 2 * 和 * msg 1 * 之间,您仍然需要打印RAX中的交换次数,然后跳过打印 * msg 1 * 的代码。

相关问题