Skip to main content

Program 9: Bubble Sort

 **Program 9: Bubble Sort**


```assembly

.model small

.stack 100h


.data

    array db 5, 2, 8, 3, 1

    n db 5


.code

    main proc

        mov bx, n             ; Load 'n' into BX

        dec bx                ; Decrement BX for loop

        

    outer_loop:

        mov si, 0             ; Initialize SI for inner loop

        

    inner_loop:

        mov al, [array + si]  ; Load array element at index SI

        cmp al, [array + si + 1] ; Compare with next element

        jg swap_elements      ; Jump if greater (needs swapping)

        

        inc si                ; Increment SI for next element

        loop inner_loop       ; Decrement BX and loop if not zero

        

        loop outer_loop       ; Decrement BX and loop if not zero

        

        ; Print sorted array

        mov si, 0             ; Initialize SI for printing

        mov cx, n             ; Load 'n' into CX

        

    print_loop:

        mov dl, [array + si]  ; Load array element at index SI

        add dl, 30h           ; Convert to ASCII

        mov ah, 02h           ; DOS function to print character

        int 21h               ; Print character

        

        inc si                ; Increment SI for next element

        loop print_loop       ; Decrement CX and loop if not zero

        

        mov ah, 4Ch           ; Exit program

        int 21h

    main endp

end main

```


Explanation:

1. `.model small` and `.stack 100h`: Memory model and stack size definitions.

2. `.data` section: Declares the data segment.

   - `array db 5, 2, 8, 3, 1`: Defines a byte-sized array with values.

   - `n db 5`: Defines the length of the array.

3. `.code` section: Contains the main code.

   - `mov bx, n`: Move the value of "n" into BX.

   - `dec bx`: Decrement BX for the loop (BX = length - 1).

   - `outer_loop:`: Label for the outer loop.

   - `mov si, 0`: Initialize SI for the inner loop.

   - `inner_loop:`: Label for the inner loop.

   - `mov al, [array + si]`: Load the array element at index SI into AL.

   - `cmp al, [array + si + 1]`: Compare AL with the next element.

   - `jg swap_elements`: Jump to "swap_elements" if greater (needs swapping).

   - `inc si`: Increment SI for the next element.

   - `loop inner_loop`: Decrement BX and loop if not zero.

   - `loop outer_loop`: Decrement BX and loop if not zero.

   - `print_loop:`: Label for the loop to print the sorted array.

   - `mov dl, [array + si]`: Load the array element at index SI into DL.

   - `add dl, 30h`: Convert to ASCII.

   - `mov ah, 02h`: DOS function to print character.

   - `int 21h`: Print character.

   - `inc si`: Increment SI for the next element.

   - `loop print_loop`: Decrement CX and


 loop if not zero.

   - `mov ah, 4Ch`: Set the exit code for DOS.

   - `int 21h`: Call DOS interrupt to exit the program.


Comments

Popular posts from this blog

12. Write a program to convert NFA to DFA.

Converting a Non-Deterministic Finite Automaton (NFA) to a Deterministic Finite Automaton (DFA) involves creating a new DFA where each state corresponds to a set of NFA states reachable under certain conditions. Below is a simple Python program to perform this conversion. I'll explain each line of code: ```python from collections import defaultdict def epsilon_closure(states, transitions, epsilon):     closure = set(states)     stack = list(states)          while stack:         state = stack.pop()         for next_state in transitions[state].get(epsilon, []):             if next_state not in closure:                 closure.add(next_state)                 stack.append(next_state)          return closure def nfa_to_dfa(nfa_states, nfa_transitions, alphabet, start_state, n...