Difference between revisions of "MPLab3 Sorting"

From MKWiki
Jump to navigation Jump to search
Line 12: Line 12:
 
<pre>
 
<pre>
 
.data
 
.data
ARRAY dw 20 DUP (?)    ;The DUP directive is used to specify the number of times the question mark (?) is duplicated, which in this case is 20.
+
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
 
DATA1 dw 0000H
 
NUMB dw 0000H
 
NUMB dw 0000H
Line 34: Line 34:
 
INT 21H
 
INT 21H
  
;at this point whatever character(size) is entered by the user, its binary equivalent(8-bits) of the ASCII code is stored in AL
+
;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)   
 
SUB AL,30H    ; convert the character digit ('0' to '9') into corresponding numeric value( 0 to 9)   
Line 46: Line 46:
 
MOV DX, OFFSET msg2
 
MOV DX, OFFSET msg2
 
         INT 21H
 
         INT 21H
 +
       
 +
        MOV AH, 0     
 +
MOV SI, 0    ; SI is initialized to 0, which means it's pointing to the beginning of some data or array
 +
 +
        MOV BX, OFFSET ARRAY    ; Loads the offset address of the label ARRAY into the BX register.
  
; Now left shift the bits, to extract the MSB and print it (repeat the shifting and extracting until every bit is printed)
+
        L1: MOV DL, 0AH    ; Move the newline character (line feed) into DL to jump onto next line
 +
                MOV AH, 02H
 +
INT 21H
 +
 +
                MOV DX, SI      ; input element of the array
 +
MOV AH, 01H
 +
INT 21H
 +
             
 +
                SUB AL,30H
 +
 +
                MOV SI, DX
 +
MOV [BX + SI], AX
 +
 +
                INC SI
 +
LOOP L1
 +
       
  
MOV CX, 8
 
 
repeat8Times:
 
SHL BL, 1
 
JC printOne ; Jump if carry generated
 
 
MOV DL, 30H ; ASCCI code for '0' is 30H, which is 48 in decimal
 
JMP print
 
 
printOne:
 
MOV DL, 31H
 
 
 
print:
 
MOV AH, 02H
 
INT 21H
 
LOOP repeat8Times
 
 
 
 
; exit from the program
 
; exit from the program

Revision as of 00:24, 31 October 2023

; 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
        
        MOV AH, 0      
	MOV SI, 0     ; SI is initialized to 0, which means it's pointing to the beginning of some data or 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
		
                MOV DX, SI      ; input element of the array
		MOV AH, 01H
		INT 21H		
              
                SUB AL,30H
		
                MOV SI, DX
		MOV [BX + SI], AX
		
                INC SI
		LOOP L1
        

			
	
	; exit from the program	
	MOV AH, 4CH
        INT 21H

END