UPDATE I made a mistake at the end of the code. The syscall obviously doesn't work on Windows because it's a Linux syscall. I found this call somewhere and copied and pasted it into my code without thinking. Sorry, my bad. There should be a call to ExitProcess funciton from Kernel32.dll
No need for that in bubble sort. Each subsequent pass of the outer loop will revisit and recheck earlier elements. The algorithm relies on multiple passes to "bubble" the largest unsorted element to its final position.
@@screeck No, you're not. I also made the same mistake once upon a time. On Windows, you don't make syscalls directly in usermode. Instead, you call the user-mode APIs such as ExitProcess (IIRC), which calls a Native API function (functions located in ntdll.dll), which actually performs a syscall. (I hate Windows' abstraction hell.)
UPDATE
I made a mistake at the end of the code.
The syscall obviously doesn't work on Windows because it's a Linux syscall.
I found this call somewhere and copied and pasted it into my code without thinking.
Sorry, my bad.
There should be a call to ExitProcess funciton from Kernel32.dll
It was amazing, I enjoyed every second of it. Thx Mate!!
why don't you finish the series about os dev ?
well i think for x86 as the thumbnail suggest the system call look like "int 80" not syscall for x64 we write syscall
didn't you forget something in the swap section at the end where you have to go back to the innerloop when the array + esi*4 < array + len*4?
No need for that in bubble sort. Each subsequent pass of the outer loop will revisit and recheck earlier elements.
The algorithm relies on multiple passes to "bubble" the largest unsorted element to its final position.
Is this x86 assembly?
yes
;For Assembly x64 Linux
section .data
numbers db 2, 12, 0, 3, 8, 98, 69
len equ $ - numbers
newline db 0xa
comma db ','
section .bss
buffer resb 4
section .text
global _start
_start:
mov rcx, len
dec rcx
outer_loop:
push rcx
mov rsi, numbers
inner_loop:
mov al, [rsi]
cmp al, [rsi+1]
jle next
; Swap
xchg al, [rsi+1]
mov [rsi], al
next:
inc rsi
loop inner_loop
pop rcx
loop outer_loop
; Print sorted array
mov r12, 0 ; counter
print_loop:
movzx rax, byte [numbers + r12]
; Convert number to ASCII
mov rdi, buffer
call itoa
; Print number
mov rax, 1
mov rdi, 1
mov rsi, buffer
mov rdx, 4 ; maximum 3 digits + null terminator
syscall
; Print comma or newline
inc r12
cmp r12, len
je print_newline
mov rax, 1
mov rdi, 1
mov rsi, comma
mov rdx, 1
syscall
jmp continue_print
print_newline:
mov rax, 1
mov rdi, 1
mov rsi, newline
mov rdx, 1
syscall
jmp exit
continue_print:
cmp r12, len
jl print_loop
exit:
; Exit
mov rax, 60
xor rdi, rdi
syscall
; Function to convert integer to ASCII
; Input: RAX = integer to convert
; RDI = pointer to output buffer
itoa:
add rdi, 3
mov byte [rdi], 0
mov rcx, 10
.loop:
xor rdx, rdx
div rcx
add dl, '0'
dec rdi
mov [rdi], dl
test rax, rax
jnz .loop
ret
Why do you put esi*4?
cuz, integer are 4 bytes in size
@majorek4909 so esi*4 tells that esi is 4 bytes long and it is actually not a multiplication?
@@stavros222 it means that every value in array is 4 bytes in size, so in order to get to next one you have to multiply the index by 4
@@majorek4909 it sounds a little complicated to me. Imma search it to be more familiar with data. Thx
You cannot call a linux syscall on Windows.
Yes, you're right, I'm dumb
@@screeck No, you're not. I also made the same mistake once upon a time.
On Windows, you don't make syscalls directly in usermode. Instead, you call the user-mode APIs such as ExitProcess (IIRC), which calls a Native API function (functions located in ntdll.dll), which actually performs a syscall. (I hate Windows' abstraction hell.)