🏁
T9
/
5️⃣
Sem 5
/
Practicals
Practicals
/
MI
MI
/
Lab 6

Lab 6

Lab 6 : Programming based on Sorting of An Array of Numbers
OBJECTIVE
To learn to sort the given data in to ascending & descending order.
PRELAB
Read instructions related to Branch and Subroutine Call.
INSTRUCTIONS
Instructions
Operands
Description
CALL
procedure name label
Transfers control to procedure, return address is (IP) pushed to stack.
RET
No operands Or even immediate date
Return from near procedure. Algorithm: • Pop from stack: ◦ IP if immediate operand is present: SP = SP + operand
EXERCISE
1) Write an ALP to find the largest number from an array.
; ALP to find the largest number from an array

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array
    LARGEST DB ?                    ; Variable to store largest number

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set counter to array length
    MOV SI, 0          ; Initialize index
    MOV AL, ARRAY[SI]  ; Assume first element is largest
    MOV LARGEST, AL    ; Store in LARGEST

LOOP_START:
    CMP ARRAY[SI], AL  ; Compare current element with AL
    JLE NEXT_ITER      ; If less or equal, move to next iteration
    MOV AL, ARRAY[SI]  ; If greater, update AL
    MOV LARGEST, AL    ; Update LARGEST

NEXT_ITER:
    INC SI             ; Move to next element
    LOOP LOOP_START    ; Decrement CX and continue if not zero

    ; At this point, LARGEST contains the largest number

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
2) Write an ALP to find the smallest number from an array.
; ALP to find the smallest number from an array

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array
    SMALLEST DB ?                   ; Variable to store smallest number

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set counter to array length
    MOV SI, 0          ; Initialize index
    MOV AL, ARRAY[SI]  ; Assume first element is smallest
    MOV SMALLEST, AL   ; Store in SMALLEST

LOOP_START:
    CMP ARRAY[SI], AL  ; Compare current element with AL
    JGE NEXT_ITER      ; If greater or equal, move to next iteration
    MOV AL, ARRAY[SI]  ; If smaller, update AL
    MOV SMALLEST, AL   ; Update SMALLEST

NEXT_ITER:
    INC SI             ; Move to next element
    LOOP LOOP_START    ; Decrement CX and continue if not zero

    ; At this point, SMALLEST contains the smallest number

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
3) Write an ALP to sort the given array of numbers in ascending order.
; ALP to sort the given array of numbers in ascending order

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set outer loop counter
    DEC CX             ; Decrement by 1 for N-1 passes

OUTER_LOOP:
    MOV BX, CX         ; Set inner loop counter
    MOV SI, 0          ; Reset array index

INNER_LOOP:
    MOV AL, ARRAY[SI]  ; Load current element
    CMP AL, ARRAY[SI+1]; Compare with next element
    JLE NEXT_ITER      ; If in order, move to next iteration
    
    ; Swap elements if out of order
    XCHG AL, ARRAY[SI+1]
    MOV ARRAY[SI], AL

NEXT_ITER:
    INC SI             ; Move to next element
    DEC BX             ; Decrement inner loop counter
    JNZ INNER_LOOP     ; Continue inner loop if not zero

    LOOP OUTER_LOOP    ; Continue outer loop

    ; At this point, the array is sorted in ascending order

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
4) Write an ALP to sort the given array of numbers in descending order.
; ALP to sort the given array of numbers in descending order

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set outer loop counter
    DEC CX             ; Decrement by 1 for N-1 passes

OUTER_LOOP:
    MOV BX, CX         ; Set inner loop counter
    MOV SI, 0          ; Reset array index

INNER_LOOP:
    MOV AL, ARRAY[SI]  ; Load current element
    CMP AL, ARRAY[SI+1]; Compare with next element
    JGE NEXT_ITER      ; If in order (descending), move to next iteration
    
    ; Swap elements if out of order
    XCHG AL, ARRAY[SI+1]
    MOV ARRAY[SI], AL

NEXT_ITER:
    INC SI             ; Move to next element
    DEC BX             ; Decrement inner loop counter
    JNZ INNER_LOOP     ; Continue inner loop if not zero

    LOOP OUTER_LOOP    ; Continue outer loop

    ; At this point, the array is sorted in descending order

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
🏁
T9
/
5️⃣
Sem 5
/
Practicals
Practicals
/
MI
MI
/
Lab 6

Lab 6

Lab 6 : Programming based on Sorting of An Array of Numbers
OBJECTIVE
To learn to sort the given data in to ascending & descending order.
PRELAB
Read instructions related to Branch and Subroutine Call.
INSTRUCTIONS
Instructions
Operands
Description
CALL
procedure name label
Transfers control to procedure, return address is (IP) pushed to stack.
RET
No operands Or even immediate date
Return from near procedure. Algorithm: • Pop from stack: ◦ IP if immediate operand is present: SP = SP + operand
EXERCISE
1) Write an ALP to find the largest number from an array.
; ALP to find the largest number from an array

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array
    LARGEST DB ?                    ; Variable to store largest number

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set counter to array length
    MOV SI, 0          ; Initialize index
    MOV AL, ARRAY[SI]  ; Assume first element is largest
    MOV LARGEST, AL    ; Store in LARGEST

LOOP_START:
    CMP ARRAY[SI], AL  ; Compare current element with AL
    JLE NEXT_ITER      ; If less or equal, move to next iteration
    MOV AL, ARRAY[SI]  ; If greater, update AL
    MOV LARGEST, AL    ; Update LARGEST

NEXT_ITER:
    INC SI             ; Move to next element
    LOOP LOOP_START    ; Decrement CX and continue if not zero

    ; At this point, LARGEST contains the largest number

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
2) Write an ALP to find the smallest number from an array.
; ALP to find the smallest number from an array

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array
    SMALLEST DB ?                   ; Variable to store smallest number

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set counter to array length
    MOV SI, 0          ; Initialize index
    MOV AL, ARRAY[SI]  ; Assume first element is smallest
    MOV SMALLEST, AL   ; Store in SMALLEST

LOOP_START:
    CMP ARRAY[SI], AL  ; Compare current element with AL
    JGE NEXT_ITER      ; If greater or equal, move to next iteration
    MOV AL, ARRAY[SI]  ; If smaller, update AL
    MOV SMALLEST, AL   ; Update SMALLEST

NEXT_ITER:
    INC SI             ; Move to next element
    LOOP LOOP_START    ; Decrement CX and continue if not zero

    ; At this point, SMALLEST contains the smallest number

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
3) Write an ALP to sort the given array of numbers in ascending order.
; ALP to sort the given array of numbers in ascending order

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set outer loop counter
    DEC CX             ; Decrement by 1 for N-1 passes

OUTER_LOOP:
    MOV BX, CX         ; Set inner loop counter
    MOV SI, 0          ; Reset array index

INNER_LOOP:
    MOV AL, ARRAY[SI]  ; Load current element
    CMP AL, ARRAY[SI+1]; Compare with next element
    JLE NEXT_ITER      ; If in order, move to next iteration
    
    ; Swap elements if out of order
    XCHG AL, ARRAY[SI+1]
    MOV ARRAY[SI], AL

NEXT_ITER:
    INC SI             ; Move to next element
    DEC BX             ; Decrement inner loop counter
    JNZ INNER_LOOP     ; Continue inner loop if not zero

    LOOP OUTER_LOOP    ; Continue outer loop

    ; At this point, the array is sorted in ascending order

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN
4) Write an ALP to sort the given array of numbers in descending order.
; ALP to sort the given array of numbers in descending order

.MODEL SMALL
.STACK 100H
.DATA
    ARRAY DB 5, 12, 3, 8, 15, 1, 9  ; Example array
    LEN EQU $-ARRAY                 ; Length of array

.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX

    MOV CX, LEN        ; Set outer loop counter
    DEC CX             ; Decrement by 1 for N-1 passes

OUTER_LOOP:
    MOV BX, CX         ; Set inner loop counter
    MOV SI, 0          ; Reset array index

INNER_LOOP:
    MOV AL, ARRAY[SI]  ; Load current element
    CMP AL, ARRAY[SI+1]; Compare with next element
    JGE NEXT_ITER      ; If in order (descending), move to next iteration
    
    ; Swap elements if out of order
    XCHG AL, ARRAY[SI+1]
    MOV ARRAY[SI], AL

NEXT_ITER:
    INC SI             ; Move to next element
    DEC BX             ; Decrement inner loop counter
    JNZ INNER_LOOP     ; Continue inner loop if not zero

    LOOP OUTER_LOOP    ; Continue outer loop

    ; At this point, the array is sorted in descending order

    MOV AH, 4CH        ; DOS function to exit program
    INT 21H
MAIN ENDP
END MAIN