-1
org 100h

.Model SMALL 
.Stack
.Data 

  Ar DB 06h,05h,04h,02h

  Len DW ($-Ar)

.CODE  

  Mov Ax,@Data
  Mov Ds,Ax
  Mov Bx,Len   ;length of the array 
  Dec Bx       ;total number of passes ( n-1) 
  
Nxtpass:

  Mov Cx,Bx    ;total number of comparison 
  Mov Si,0     ;index=0
  
Nxtcomp:Mov Al,Ar[Si] ;read 1st number    
       Inc Si        ;update pointer
       Cmp Al,Ar[Si] ;compare 1st number with 2nd number
       Jb  No_EXCG   ;if same , noexchange 
  
  
      XCHG Al,Ar[Si]
      Mov Ar[Si-1],Al ; do exchange
  
No_EXCG:loop Nxtcomp  ;repeat until comparison
        Dec  Bx
        JNZ  Nxtpass  ;repeat untill pass = 0; 
  
       
ret
Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Durjoy
  • 7
  • 2
  • 2
    "Is my code right" is too broad a question for this site. Have you tested it on a variety of data? Are you aware of any bugs? Is there a particular part of the code you are unsure about? – Nate Eldredge May 21 '21 at 05:20
  • 2
    Why do you want your code to originate at offset `100h`? This is typical for CP/M programs, which start with CS=DS=ES=SS pointing to PSP and do not expect any manipulation with segment registers. See also [this answer](https://stackoverflow.com/questions/67454479/getting-wrong-result-in-assembly-language/67454775#67454775). – vitsoft May 21 '21 at 06:11
  • 3
    If you've tested this (e.g. in a debugger) and you think it works, you could post it on https://codereview.stackexchange.com/ if you're looking for whether feedback on how well-written it is. (e.g. your loops are compact, with branch conditions at the bottom [where they should be](https://stackoverflow.com/questions/47783926/why-are-loops-always-compiled-into-do-while-style), without wasting instructions on push/pop to use `loop` for both loops or anything silly like that. So it's probably decently efficient for 8086, but xchg-with-mem will be very slow on modern x86: implicit `lock`) – Peter Cordes May 21 '21 at 08:43
  • Why are you implementing one of the worst sorting algorithms anyway? – tripleee May 22 '21 at 15:44

1 Answers1

2

Your BubbleSort is good, but you could make it more secure if it could deal with an array of just one element, or even zero elements!
The code that will test for this can be combined with the outer loop logic:

    jmp   Start
Nxtpass:
    ...
Start:
    sub   bx, 1
    jnbe  Nxtpass
    ret

If there are no elements, the sub bx, 1 instruction will make CF=1 aka "Below".
If there's but 1 element, the sub bx, 1 instruction will make ZF=1 aka "Equal".
The jnbe Nxtpass instruction will only jump on the condition "NeitherBelowNorEqual" also known as "Above".


Cmp Al,Ar[Si] ;compare 1st number with 2nd number
Jb  No_EXCG   ;if same , noexchange 

The comment is misleading! Exchanging elements can be skipped on "Below" but also on "Equal". Better use jbe No_EXCG.


Programming style is personal but writing the mnemonics and register names with a leading capital is very, very unusual.

.Model SMALL 
.Stack
.Data 

  Ar db 06h, 05h, 04h, 02h
  Len equ $-Ar

.Code

    mov   ax, @Data
    mov   ds, ax
    mov   bx, Len
    jmp   Start

Nxtpass:
    mov   cx, bx
    xor   si, si
Nxtcomp:
    mov   al, Ar[si]
    inc   si
    cmp   al, Ar[si]
    jbe   No_EXCG
    xchg  al, Ar[si]
    mov   Ar[si-1], al
No_EXCG:
    loop  Nxtcomp
Start:
    sub   bx, 1
    jnbe  Nxtpass
    ret
Sep Roland
  • 33,889
  • 7
  • 43
  • 76