Logo 
Search:

Assembly Language Articles

Submit Article
Home » Articles » Assembly Language » GeneralRSS Feeds

Program to read a number in binary form, display a number in binary form and reverse the bit pattern of the number( use procedures for each part. )

Posted By: Easy Tutor     Category: Assembly Language     Views: 3037

An AL Program to
(a) read a number in binary form
(b) display a number in binary form
(c) reverse the bit pattern of the number( use procedures for each part. )

Code for Program to read a number in binary form, display a number in binary form and reverse the bit pattern of the number( use procedures for each part. ) in Assembly Language

 .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  ---------------------------------;
 ;**************************************************************************;
 ;**************************************************************************;
  
Share: 



Easy Tutor
Easy Tutor author of Program to read a number in binary form, display a number in binary form and reverse the bit pattern of the number( use procedures for each part. ) is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!