Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bubble sort on array on Assembly Language

I need to Bubblesort an unorganized array with 7 integers from biggest to smallest so it would look like 9,6,5,4,3,2,1.
I ran my code through the compiler and it says

Compiler Error

I can't understand what is the problem with this code:

code segment
assume ds:code,cs:code
start:
 mov ax,code
 mov ds,ax    ;code start
ARR:   dw 1,2,4,3,6,5,9
 mov ch,0h
 mov cl,1h
 mov bh 7h
 jmp assign_nums
restart:
 mov ch,0h
 mov cl,1h
 dec bh
 jmp assign_nums
swap:
 mov ch,dl
 mov cl,dh
 jmp next
next:
 cmp bh,cl
 je restart
 add ch,1h
 add cl,1h
 jmp assign_nums
assign_nums:
 cmp bh,0h
 je done
 mov dh,[ARR+ch]
 mov dl,[ARR+cl]
 cmp dh,dl
 jl swap
 jnl next
done:
 nop
code ends
end start
like image 470
Puloko Avatar asked May 16 '26 10:05

Puloko


2 Answers

For the 1st error you forgot to type a comma between the register and the immediate.

For the 2nd and 3rd errors the CH and CL registers cannot be used for addressing memory. Use SI, DI, or BX instead.

Since your array is defined as words you must treat it as such!
Change

mov dh,[ARR+ch]
mov dl,[ARR+cl]

into something like (depends on other choices you make)

mov ax,[ARR+si]
mov dx,[ARR+di]

Please note that you placed the array amidst the instructions. This will crash your program as soon as you manage to compile it. Either place the array in a separate data segment of your program or jump over this line.

start:
 mov ax,code
 mov ds,ax
 jmp start2
ARR:   dw 1,2,4,3,6,5,9
start2:
 mov ch,0h
like image 190
Sep Roland Avatar answered May 18 '26 16:05

Sep Roland


This is simple code to bubble sort

iclude'emu8086.inc'

org 100h 
.data

array  db 9,6,5,4,3,2,1
count  dw 7

.code

    mov cx,count      
    dec cx               ; outer loop iteration count

nextscan:                ; do {    // outer loop
    mov bx,cx
    mov si,0 

nextcomp:

    mov al,array[si]
    mov dl,array[si+1]
    cmp al,dl

    jnc noswap 

    mov array[si],dl
    mov array[si+1],al

noswap: 
    inc si
    dec bx
    jnz nextcomp

    loop nextscan       ; } while(--cx);



;;; this  loop to display  elements on the screen

    mov cx,7
    mov si,0

print:

    Mov al,array[si]  
    Add al,30h
    Mov ah,0eh
    Int  10h 
    MOV AH,2
    Mov DL , ' '
    INT 21H
    inc si
    Loop print

    ret 
like image 40
Ahmed Ramadan Avatar answered May 18 '26 18:05

Ahmed Ramadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!