MPLab3 Sorting
Jump to navigation
Jump to search
; Program to sort an array
1. Define the model and architecture for the assembly code
.model SMALL .stack 100H .386
2. Define your data in the data section ( here you can define various data items: variables, constants, strings, arrays)
.data ARRAY dw 20 DUP (?) ;The DUP directive is used to specify the number of times the question mark (?) is duplicated (20 times) DATA1 dw 0000H NUMB dw 0000H msg db 10,13,"Enter the size of the array: $" msg2 db 10,13,"Enter the elements of array: $" msg3 db 10,13,"The sorted array is: $"
3. Define your instruction in the code section (here you write the assembly instructions that perform computations, control program flow, and interact with data variables and memory)
.code
.startup
; print Enter the size of the array message
MOV AH, 09H
MOV DX, OFFSET msg
INT 21H
;Read the size of the array
MOV AH, 01H
INT 21H
;The binary equivalent(8-bits) of the ASCII code for the entered character is stored in AL
SUB AL,30H ; convert the character digit ('0' to '9') into corresponding numeric value( 0 to 9)
MOV AH,0
MOV CX, AX ; This instruction loads the CX with the size of array
MOV DATA1, AX ; The size is also stored in DATA1
; print Enter the elements of Array message
MOV AH, 09H
MOV DX, OFFSET msg2
INT 21H
'''; Read the individual elements, convert them from ASCII characters to integers, and stores them in the ARRAY.'''
MOV AH, 0
MOV SI, 0 ; SI is initialized to 0, which means it's pointing to the beginning of array
MOV BX, OFFSET ARRAY ; Loads the offset address of the label ARRAY into the BX register.
L1: MOV DL, 0AH ; Move the newline character (line feed) into DL to jump onto next line
MOV AH, 02H
INT 21H
; input element of the array
MOV DX, SI
MOV AH, 01H
INT 21H
SUB AL,30H ; convert the character digit ('0' to '9') into corresponding numeric value( 0 to 9)
MOV SI, DX
MOV [BX + SI], AX ; stores the value in AX into a memory location specified by the contents of the BX and SI registers.
INC SI ; increment the value of SI register by 1
LOOP L1
'''; Sorting'''
MOV CX, DATA1 ; CX is loaded with the number of elements in the array.
MOV BX, OFFSET ARRAY ; BX is set to point to the beginning of the ARRAY
MOV DI,CX ; DI is loaded with the number of elements in the array
; outer loop
L2: ; MOV CX, DATA1
MOV NUMB, CX ; Change1, NUMB is loaded with the current number of elements (CX)
DEC NUMB ; Change2, NUMB is decremented by 1, effectively reducing it by one element for each pass of the outer loop.
MOV CX, NUMB ; change3, CX is updated to hold the current number of elements after decrementing.
MOV SI, 0 ; SI is initialized to zero, which serves as an offset within the array
; inner loop
L3: MOV AL, [BX + SI] ; AL is loaded with the current element in the array
CMP [BX + SI + 1],AL ; Compares the current element (AL) with the next element in the array
JL L4 ; If the next element is less than the current element, it swaps them. Otherwise, it continues to the next element.
XCHG AL,[BX + SI + 1] ; Swaps the values of the current element and the next element.
MOV [BX + SI],AL ; Moves the updated value back into the current element's position in the array.
L4: INC SI ; Increments SI to move to the next element in the array.
LOOP L3 ; Repeats the inner loop L3 for the remaining elements in the array (according to the updated CX value).
DEC DI ; After the inner loop is complete, the outer loop continues until all elements are compared and sorted. DI is decremented, and the process continues for the remaining elements.
JNZ L2 ; checks the value of the DI register. If DI is not equal to zero, the program jumps back to the label L2.
'''; Displays the sorted array'''
MOV CX, DATA1
MOV SI, 0
MOV BX, OFFSET ARRAY
MOV AH,09
MOV DX, OFFSET msg3
INT 21H
L5: MOV DL, 0AH ; jump onto next line
MOV AH, 02H
INT 21H
MOV DX, [BX + SI]
INC SI
ADD DL, 30H
MOV AH, 02
INT 21H
LOOP L5
; exit from the program
MOV AH, 4CH
INT 21H
END