41 Program to arrange the content of memory location 4000h to 4002h in descending order in 8086 Microprocessor

Code:

; Arrange the content of memory location
; 4000h to 4002h in descending order

.MODEL SMALL
.STACK 100H
.CODE
  MOV AX , 1000H  ; Initializing data segment
  MOV DS , AX

 ; PRINTING BEFORE SORTING
  MOV CX , 3
  MOV SI , 4000H
 NEXT:
    MOV AX , [SI]
AAM             ; INSTRUCTION DIVIDES BY 10
                    ; RESULT AH=HIGH DECIMAL , AL=LOW DECIMAL DIGIT
    ADD AX , 48     ; NOW AX CONTAINS THE 2 DECIMAL ASCII DIGITS

MOV BH , AL
MOV DL , AH   
ADD DL , 48
MOV AH , 02
INT 21H

MOV DH , 00
MOV DL , BH
ADD DL , 48
MOV AH , 02
INT 21H

    MOV DX , 0H     ; TO PRINT SPACE
    MOV AH , 02
    INT 21H

    INC SI
    LOOP NEXT

  MOV BX , 03     ; Initializing pass counter

 UP1:
    MOV  SI , 4000H   ; LOADING BASE ADDRESS
    MOV  CX , 2       ; Initializing loop counter     
  UP:
     MOV AL , [SI]
     CMP AL , [SI+1]  ; IF DESTINATION < SOURCE CF=1
                      ; IF DESTINATION > SOURCE CF=0
     JNC DN
      XCHG AL , [SI+1]
      XCHG AL , [SI]
  DN:
     INC SI          ; INCREMENT SOURCE INDEX
     LOOP UP         ; DECREMENT LOOP COUNTER IF  0 THEN EXIT
     DEC BX          ; DECREMENT PASS COUNTER
     JNZ UP1         ; JUMP IF NOT ZERO

 ; PRINTING NEW LINE
    MOV AH , 2
    MOV DL , 0DH              ;CARGE RETURN
    INT 21H
    MOV DL , 0AH              ;LINE FEED 
    INT 21H

 ; PRINTING AFTER SORTING
  MOV CX , 3
  MOV SI , 4000H
 NEXT2:
    MOV AX , [SI]
AAM             ; INSTRUCTION DIVIDES BY 10
                    ; RESULT AH=HIGH DECIMAL , AL=LOW DECIMAL DIGIT
    ADD AX , 48     ; NOW AX CONTAINS THE 2 DECIMAL ASCII DIGITS

MOV BH , AL
MOV DL , AH   
ADD DL , 48
MOV AH , 02
INT 21H

MOV DH , 00
MOV DL , BH
ADD DL , 48
MOV AH , 02
INT 21H

    MOV DX , 0H     ; TO PRINT SPACE
    MOV AH , 02
    INT 21H

    INC SI
    LOOP NEXT2


  MOV AH , 4CH     ; SERVICE ROUTINE FOR EXIT
  INT 21H
END





Ouput:





Previous
Next Post »