Bubble sort in Assembly | Tutorial for beginners

แชร์
ฝัง

ความคิดเห็น • 19

  • @screeck
    @screeck  2 หลายเดือนก่อน +3

    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

  • @gulyasgyorgy
    @gulyasgyorgy 2 หลายเดือนก่อน

    It was amazing, I enjoyed every second of it. Thx Mate!!

  • @lsmyrak
    @lsmyrak 2 หลายเดือนก่อน +2

    why don't you finish the series about os dev ?

  • @ONEGOALONEJOURNEY
    @ONEGOALONEJOURNEY 2 หลายเดือนก่อน

    well i think for x86 as the thumbnail suggest the system call look like "int 80" not syscall for x64 we write syscall

  • @SoundFriendly
    @SoundFriendly 2 หลายเดือนก่อน

    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?

    • @screeck
      @screeck  2 หลายเดือนก่อน +1

      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.

  • @ACium.
    @ACium. 2 หลายเดือนก่อน +1

    Is this x86 assembly?

  • @weberffs
    @weberffs 2 หลายเดือนก่อน +1

    ;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

  • @stavros222
    @stavros222 2 หลายเดือนก่อน

    Why do you put esi*4?

    • @majorek4909
      @majorek4909 2 หลายเดือนก่อน +1

      cuz, integer are 4 bytes in size

    • @stavros222
      @stavros222 2 หลายเดือนก่อน

      @majorek4909 so esi*4 tells that esi is 4 bytes long and it is actually not a multiplication?

    • @majorek4909
      @majorek4909 2 หลายเดือนก่อน +1

      @@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

    • @stavros222
      @stavros222 2 หลายเดือนก่อน

      @@majorek4909 it sounds a little complicated to me. Imma search it to be more familiar with data. Thx

  • @semihartan
    @semihartan 2 หลายเดือนก่อน

    You cannot call a linux syscall on Windows.

    • @screeck
      @screeck  2 หลายเดือนก่อน

      Yes, you're right, I'm dumb

    • @semihartan
      @semihartan 2 หลายเดือนก่อน

      @@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.)

  • @ROBOTRIX_eu
    @ROBOTRIX_eu 2 หลายเดือนก่อน +1