.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter the number in binary form (max digit=16) : $'
PROMPT_2 DB 'The given number in original bit pattern is : $'
PROMPT_3 DB 'The given number in reverse bit pattern is : $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, PROMPT_1 ; load and display PROMPT_1
MOV AH, 9
INT 21H
CALL READ_BINARY_NUMBER ; call the procedure READ_BINARY_NUMBER
CALL NEXT_LINE ; call the procedure NEXT_LINE
LEA DX, PROMPT_2 ; load and display PROMPT_2
MOV AH, 9
INT 21H
CALL DISPLAY_BINARY_NUMBER ; call the procedure DISPLAY_BINARY_NUMBER
CALL NEXT_LINE ; call the procedure NEXT_LINE
LEA DX, PROMPT_3 ; load and display PROMPT_3
MOV AH, 9
INT 21H
CALL REVERSE_BIT_PATTERN ; call the procedure REVERSE_BIT_PATTERN
CALL DISPLAY_BINARY_NUMBER ; call the procedure DISPLAY_BINARY_NUMBER
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
;**************************************************************************;
;**************************************************************************;
;------------------------ PROCEDURE DEFINITIONS -------------------------;
;**************************************************************************;
;**************************************************************************;
;**************************************************************************;
;----------------------------- NEXT_LINE --------------------------------;
;**************************************************************************;
NEXT_LINE PROC
; this procedure will move the cursor to the next line
; input : none
; output : none
; uses : MAIN
PUSH AX ; push AX onto the STACK
PUSH DX ; push DX onto the STACK
MOV AH, 2 ; set output function
MOV DL, 0DH ; carriage return
INT 21H
MOV DL, 0AH ; line feed
INT 21H
POP DX ; pop a value from the STACK to DX
POP AX ; pop a value from the STACK to AX
RET ; return control to the calling procedure
NEXT_LINE ENDP
;**************************************************************************;
;-------------------------- READ_BINARY_NUMBER --------------------------;
;**************************************************************************;
READ_BINARY_NUMBER PROC
; this procedure will read a number in binary form
; input : none
; output : store binary number in BX
; uses : MAIN
MOV CX, 16 ; initialize loop counter
XOR BX, BX ; clear BX
MOV AH, 1 ; set input function
@LOOP_1: ; loop label
INT 21H ; read a digit
CMP AL, 0DH ; compare input and CR
JE @END ; jump to label @END if input is CR
AND AL, 0FH ; convert ascii to decimal code
SHL BX, 1 ; shift BX by 1 position towards left
OR BL, AL ; place the input decimal digit in BL
LOOP @LOOP_1 ; jump to label @LOOP_1 if CX!=0
@END: ; jump label
RET ; return control to the calling procedure
READ_BINARY_NUMBER ENDP
;**************************************************************************;
;------------------------ DISPLAY_BINARY_NUMBER -------------------------;
;**************************************************************************;
DISPLAY_BINARY_NUMBER PROC
; this procedure will display a number in binary form
; input : BX
; output : none
; uses : MAIN
PUSH BX ; push BX onto the STACK
MOV CX, 16 ; initialize loop counter
MOV AH, 2 ; set output function
@LOOP_2: ; loop label
SHL BX, 1 ; shift BX by 1 position towards left
JC @ONE ; jump to label @ONE if CF=1
MOV DL, 30H ; move 0 to DL
JMP @DISPLAY ; jump tp label @DISPLAY
@ONE: ; jump label
MOV DL, 31H ; move 1 to DL
@DISPLAY: ; jump label
INT 21H ; display a digit
LOOP @LOOP_2 ; jump to label @LOOP_2 if CX!=0
POP BX ; pop a value from STACK to BX
RET ; return control to the calling procedure
DISPLAY_BINARY_NUMBER ENDP
;**************************************************************************;
;------------------------- REVERSE_BIT_PATTERN --------------------------;
;**************************************************************************;
REVERSE_BIT_PATTERN PROC
; this procedure will read a number in binary form
; input : BX
; output : store the binary number in BX with reverse bit pattern
; uses : MAIN
PUSH DX ; push DX onto the STACK
MOV CX, 16 ; initialize loop counter
MOV DX, BX ; copy BX into DX
XOR BX, BX ; clear BX
@LOOP_3: ; loop label
SHL BX, 1 ; shift BX towards left by 1 position
SHR DX, 1 ; shift DX towards right by 1 position
JNC @ZERO ; jump to label @ZERO if CF=0
OR BX, 1 ; set the LSb of BX
@ZERO: ; jump label
OR BX, 0 ; set the LSB
LOOP @LOOP_3 ; jump to label @LOOP_3 if CX!=0
POP DX ; pop a value from the STACK to DX
RET ; return control to the calling procedure
REVERSE_BIT_PATTERN ENDP
;**************************************************************************;
;--------------------------------------------------------------------------;
;**************************************************************************;
END MAIN
;**************************************************************************;
;**************************************************************************;
;------------------------------ THE END ---------------------------------;
;**************************************************************************;
;**************************************************************************;