BUBBLE SORT in assembly 8086! help..

The program will ask the user to input 5 ASCII characters (ACDBE) via keyboard. the program will then Bubble sort the input to ABCDE. however, an error

[code] mov dx, offset buff
mov cx, size buff[/code]

was obtained from the compiler. help?

; You may customize this and other start-up templates;
; The location of this template is c:emu8086inc_com_template.txt
.model tiny

org 100h

buff db 256 dup(?)

mov ah, 3fh ; reading a string from standard input
xor bx, bx
mov dx, offset buff
mov cx, size buff
int 21h
jnc okay

mov bx, offset buff
sub al, 2 ; ignore the CRLF
push ax
cmp al, 2 ; two or more chars ?
jb sorted

add ax, bx
dec ax
mov cx, ax ; cx - ptr. to the last element
inc bx ; starting with the second element

mov si, cx ; the inner loop always starts from the last element
mov dl, 1 ; dl is used as the "sorted" flag

mov ax, [si - 1] ; get two chars
cmp al, ah ; are they in order ?
jbe cool
xor dl, dl ; no, then reset the "sorted" flag
mov [si - 1], ah ; ... and exchange the values
mov [si], al

dec si ; ok, the inner loop continues until si < bx
cmp si, bx
jae for2

test dl, dl ; some exchanges occured ?
jne sorted ; nope - then the array is sorted

inc bx ; the outer loop continues till
cmp bx, cx ; bx goes past the last element in array ...
jbe for1

pop cx ; write the result at the standard output
xor bx, bx
mov ah, 40h
inc bx
mov dx, offset buff
int 21h


end start



  • Try this:
    buff db 256 dup(?)
    buff_size equ $ - buff


    mov cx, buff_size
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion