Question: What is wrong with my code? Do not give me chatgpt generated answer bits 64 %include ./functions64.inc global selectsort ; Makes it available to access
What is wrong with my code? Do not give me chatgpt generated answer
bits 64 %include "./functions64.inc"
global selectsort ; Makes it available to access
section .text
selectsort: push rbp ; Save old base pointer mov rbp, rsp ; Set up new stack frame sub rsp, 8 ; Reserve 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
; 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 rax, rsi ; Set minimum index to -1
; Initialize inner loop counter mov rbx, rsi ; Set inner loop counter to current index inc rbx
innerloop: ; Check if current index is less than end index mov r9, [rbp - 4] cmp rsi, r9 ; Compare with end index jge innerloopend ; If greater or equal, end inner loop
; Check if current element is less than minimum element mov r8, [rbx + rdi * 8] ; Get current element cmp r8, [rax + rdi * 8] ; Compare with minimum element
; Update minimum index cmovl rax, rbx ; Set minimum index to current index
innerloopinc: inc rbx ; Increment inner loop counter cmp rbx, r9 jl innerloop ; Repeat inner loop
swapping: ; Swap minimum element with current element mov r10, [rax + rdi * 8] ; Get minimum element mov r11, [rsi + rdi * 8] ; Get current element mov [rsi + rdi * 8], r10 ; Store minimum element at current index mov [rax + rdi * 8], r11 ; Store current element at minimum index
; Increment number of swaps mov rax, [rbp - 8] ; Get number of swaps inc rax ; Increment number of swaps mov [rbp - 8], rax ; Store number of swaps
innerloopend: ; Check if minimum index is less than current index cmp rax, rsi ; Compare with current index jl swapping
inc rsi ; Increment current index cmp rsi, [rbp - 4] ; Compare with end index jle outerloop ; Repeat outer loop if less or equal
exit: mov rsp, rbp ; return number of swaps pop rbp ; restore stack frame ret 8
main file Winclude "./functions 64 .inc" extern selectsort SECTION data openPrompt db "Welcome to my Program", Oh closePrompt do "Program ending, have a nice day*", Oh msg1 db "The array was already sorted or the size was incorrect", Oh msg2 db "The total swaps done during the sort is: ", Oh 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, Oh 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 Exit: mov rax,60;60= system exit mov rdi, 0;0= return code syscall "Poke the kernel main file Winclude "./functions 64 .inc" extern selectsort SECTION data openPrompt db "Welcome to my Program", Oh closePrompt do "Program ending, have a nice day*", Oh msg1 db "The array was already sorted or the size was incorrect", Oh msg2 db "The total swaps done during the sort is: ", Oh 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, Oh 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 Exit: mov rax,60;60= system exit mov rdi, 0;0= return code syscall "Poke the kernel
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
