Logo 
Search:

Assembly Language Articles

Submit Article
Home » Articles » Assembly Language » GeneralRSS Feeds

Program that read two numbers in binary form, computes and display their product by ADD and SHIFT instructions using procedures

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

An AL Program that read two numbers in binary form, computes and display their product by ADD and SHIFT instructions using procedures.

Code for Program that read two numbers in binary form, computes and display their product by ADD and SHIFT instructions using procedures in Assembly Language

 .MODEL SMALL
 .STACK 100H

 .DATA
   PROMPT_1  DB  'Enter the first binary number  (max 8-bit) - A : $'
   PROMPT_2  DB  'Enter the second binary number (max 8-bit) - B : $'
   PROMPT_3  DB  'Result - ( A * B ) : $'

   VALUE_1   DB  ?
   VALUE_2   DB  ?

 .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

     MOV VALUE_1, DL              ; save the first valuein VALUE_1

     CALL NEXT_LINE               ; call the procedure NEXT_LINE

     LEA DX, PROMPT_2             ; load and display PROMPT_2  
     MOV AH, 9
     INT 21H

     CALL READ_BINARY_NUMBER      ; call the procedure READ_BINARY_NUMBER

     MOV VALUE_2, DL              ; save the second valuein VALUE_2

     CALL NEXT_LINE               ; call the procedure NEXT_LINE

     LEA DX, PROMPT_3             ; load and display PROMPT_3
     MOV AH, 9
     INT 21H

     CALL MULTIPLY                ; call the procedure MULTIPLY

     MOV BX, DX                   ; move the result from DX to BX

     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 STACK to DX
   POP AX                    ; pop a value from 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 DL
   ; uses : MAIN

   MOV CX, 8                      ; initialize loop counter
   XOR DL, DL                     ; clear DL
   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 DL, 1                    ; shift BX by 1 position towards left
     OR  DL, 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

   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

   RET                            ; return control to the calling procedure
 DISPLAY_BINARY_NUMBER ENDP 

 ;**************************************************************************;
 ;-------------------------  REVERSE_BIT_PATTERN  --------------------------;
 ;**************************************************************************;

 MULTIPLY PROC
   ; this procedure will multiply two binary numbers     
   ; input : BL, BH
   ; output : store the result in the register DX    
   ; uses : MAIN

   XOR DX, DX                     ; clear DX
   XOR BX, BX
   XOR AX, AX

   MOV AL, VALUE_1
   MOV BL, VALUE_2

   @LOOP_3:                       ; loop label
     TEST BX, 1                   ; test the LSB of BX i.e. 0 or 1     
     JZ @NEXT                     ; jump to label @END_LOOP if ZF=0     

     ADD DX, AX                   ; add the contents of AX into DX

     @NEXT:                       ; loop label

     SHL AX, 1                    ; shift AX towards left by 1 position
     SHR BX, 1                    ; shift BX towards right by 1 position

     JNZ @LOOP_3                  ; jump to label @LOOP_3 if CX!=0

   RET                            ; return control to the calling procedure
 MULTIPLY ENDP

 ;**************************************************************************;
 ;--------------------------------------------------------------------------;
 ;**************************************************************************;

 END MAIN

 ;**************************************************************************;
 ;**************************************************************************;
 ;------------------------------  THE END  ---------------------------------;
 ;**************************************************************************;
 ;**************************************************************************;
  
Share: 



Easy Tutor
Easy Tutor author of Program that read two numbers in binary form, computes and display their product by ADD and SHIFT instructions using procedures 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:


 

Other Interesting Articles in Assembly Language:


 
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!