3

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
Jongware
  • 22,200
  • 8
  • 54
  • 100
Puloko
  • 81
  • 1
  • 2
  • 7

4 Answers4

2

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
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • so its a bunch of spaghetti code? is there a better way to code this? I have no clue what I'm doing here with the assembly code – Puloko May 24 '15 at 23:18
  • 1
    The answer from user3144770 is great. The only change I would make is not to give the option of jumping over the ARRAY. Only place your ARR in .data segment `.DATA OR .FARDATA DSEG` `ARR: dw 1,2,4,3,6,5,9` – BKCOHEN Dec 02 '15 at 17:27
2

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 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Would be more efficient to `mov si, OFFSET array` so `si` is a pointer, not an index. Both otherwise this is a pretty decent straightforward BubbleSort that keeps it simple by not doing an early-out check to stop after an outer-loop iteration with no swaps. – Peter Cordes Apr 09 '18 at 21:36
1

; SORTING ARRAY BY USING BUBBLE SORT ALGORITHM

.MODEL SMALL  
.STACK 100H  
.DATA  
    N DB 44H,22H,11H,55H,33H     ; N is an array      
    LEN DW 5 ; LENGTH OF ARRAY N   
.CODE   
 MAIN PROC  
 MOV AX,@DATA  
    MOV DS,AX  

 MOV CX,LEN ;Cx is counter for OUTERLOOP CX=5    
 DEC CX     ; CX = 4   

 OUTERLOOP:  
    MOV SI,0         ;    SI is the index of array N   
    MOV DX,CX  ; Dx is counter for INNERLOOP   
 INNERLOOP:    
    MOV AH,N[SI]    ; assign the number N[SI] into reg.AH  
    MOV AL,N[SI+1]  ; assign the next number N[SI+1] into reg.AL   
    CMP AH,AL       ; Compare between N[SI] and N[SI+1] <BR> 
    JC CARRY        ; if AL > AH => Carry Flag =1 ,THEN jump to carry   
    MOV N[SI] , AL  ; else , Do Switching bteween  N[SI] and N[SI+1]   
    MOV N[SI+1] ,AH   
 CARRY:   
    INC SI   
    DEC DX   
    JNZ INNERLOOP   
    LOOP OUTERLOOP   
 ;exit   
 MOV AH,4CH   ;service number     
 INT 21H      ; interrupt   
 MAIN  ENDP   
END 
  • Please include some explanations in your answer and use the correct syntax highlighting. – bastelflp Nov 18 '17 at 13:04
  • The pair of stores can be done as `mov n[si], AX` to store AL and AH together with a 16-bit store. (But [on modern Intel CPUs, you'll get partial-register merging slowdowns](https://stackoverflow.com/questions/45660139/how-exactly-do-partial-registers-on-haswell-skylake-perform-writing-al-seems-to). Still, you're using [a slow `loop` instruction](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) so obviously you're not optimizing for performance on modern CPUs.) – Peter Cordes Nov 21 '17 at 10:46
-1
.model small
.data

arr1 db 6, 5, 8, 3, 9
len1 equ $-arr1

.code


mov ax, @data
mov ds, ax
mov ch, len1-1


a1:
mov cl, ch
lea si, arr1


rept1:
mov al, [si]
inc si
cmp al, [si]
jbe next1
xchg al, [si]
mov [si-1], al


next1:
dec cl
jnz rept1
dec ch
jnz a1


mov ah, 4ch
int 21h

end
Small_Kitten
  • 116
  • 9
  • Please format the code. You do this by adding 4 spaces in front of each line. Also remove all of the blank lines. – Sep Roland Apr 09 '18 at 16:28
  • 1
    Did you know that 'code-only' posts like yours, don't count as answer? You need to explain in detail what your program does and how it helps the person who asked the original question on top of this page. Failing to do so is an open invitation for people to start down-voting on your post! – Sep Roland Apr 09 '18 at 16:33
  • Thank you @SepRoland for the feedback. i am a newbie to this community and your feedback was without any doubt very helpful! – AbinVarghese May 15 '18 at 12:48