ascending order using bubble sort.
.model small
.stack 100h
.data
; Array of 10 numbers to sort
numbers db 10, 2, 8, 4, 5, 9, 1, 6, 7, 3
n db 10 ; number of elements in array
.code
main proc
mov ax, @data ; initialize data segment
mov ds, ax
; Outer loop: passes
mov cx, n ; CX = 10
dec cx ; CX = 9 passes needed
outer_loop:
mov si, 0 ; SI = index of first element
; Inner loop: compare adjacent elements
mov dx, cx ; DX = number of comparisons in this pass
inner_loop:
mov al, numbers[si] ; AL = current element
mov bl, numbers[si+1] ; BL = next element
cmp al, bl
jbe no_swap ; if AL <= BL, no swap
; Swap AL and BL
mov numbers[si], bl
mov numbers[si+1], al
no_swap:
inc si ; move to next element
dec dx
jnz inner_loop ; repeat inner loop
dec cx ; next pass
jnz outer_loop ; repeat outer loop
; End program (DOS interrupt)
mov ah, 4ch
int 21h
, main endp
end main
1. Concept: Bubble Sort
Bubble sort algorithm:
Compare each pair of adjacent elements.
Swap them if they are in the wrong order.
Repeat steps 1–2 for the entire list until no swaps are needed.
For a list of 10 numbers, we perform 9 passes, because after each pass the
largest number “bubbles” to its correct position.
arr → numbers ki list
outer loop → total passes
inner loop → adjacent comparison
cmp → compare numbers
jbe → agar already correct ho to swap nahi
End pe array ascending order mein sort ho jata hai
Q2: How would you print capital ‘A’ with blue foreground
and red background with no blinking property? write its
assembly code?
Step 1: Understanding the color attributes
In text mode (like 80x25), colors are specified in one byte:
Lower 4 bits → Foreground color
Upper 3 bits → Background color
1 bit → Blink (if set, character blinks)
Color codes:
Foreground:
0 = Black
1 = Blue