Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
i'm writing a program that prompts the user to enter two binary numbers of up to 8 digits each, and print their sum on the next line in binary. If the user enters an illegal character, he or she should be prompted to begin again. Each input ends with a carriage return. i wanna know how to add check so that if user enter other than 0 and 1 he should be prompted about wrong digit.
here is my program:

ASM
.MODEL SMALL
.STACK 100H 
.DATA
   PROMPT_1  DB  0DH,0AH,'Enter the first binary number ( max 8-digits ) : $'
   PROMPT_2  DB  0DH,0AH,'Enter the second binary number ( max 8-digits ) : $'
   PROMPT_3  DB  0DH,0AH,'The SUM of given binary numbers in binary form is : $'
   ILLEGAL   DB  0DH,0AH,'Illegal character. Try again.$'

.CODE
   MAIN PROC
     MOV AX, @DATA                ; initialize DS
     MOV DS, AX

     @START_2:                    ; jump label
       XOR BX, BX                 ; clear BX

       LEA DX, PROMPT_1           ; load and display the string PROMPT_1
       MOV AH, 9
       INT 21H

       MOV CX, 8                  ; initialize loop counter
       MOV AH, 1                  ; set input function

       @LOOP_1:                   ; loop label

         INT 21H                  ; read a character

         CMP AL, 0DH              ; compare AL with CR
         JNE @SKIP_1              ; jump to label @SKIP_1 if AL!=0DH
         JMP @EXIT_LOOP_1         ; jump to label @EXIT_LOOP_1

         @SKIP_1:                 ; jump label
           AND AL, 0FH            ; convert ascii into decimal code
           SHL BL, 1              ; shift BL towards left by 1 position
           OR BL, AL              ; set the LSB of BL with LASB of AL
       LOOP @LOOP_1               ; jump to label @LOOP_1 if CX!=0

       @EXIT_LOOP_1:              ; jump label

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

       MOV CX, 8                  ; initialize loop counter
       MOV AH, 1                  ; set input function

       @LOOP_2:                   ; loop label
         INT 21H                  ; read a character

         CMP AL, 0DH              ; compare AL with CR
         JNE @SKIP_2              ; jump to label @SKIP_2 if AL!=0DH
         JMP @EXIT_LOOP_2         ; jump to label @EXIT_LOOP_2

         @SKIP_2:                 ; jump label
           AND AL, 0FH            ; convert ascii into decimal code
           SHL BH, 1              ; shift BH towards left by 1 position
           OR BH, AL              ; set the LSB of BH with LASB of AL
       LOOP @LOOP_2               ; jump to label @LOOP_2 if CX!=0

       @EXIT_LOOP_2:              ; jump label

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

       ADD BL, BH                 ; add BL and BH
       JNC @SKIP                  ; jump to label @SKIP if CF=1
         MOV AH, 2                ; print the digit 1 i.e. carry
         MOV DL, 31H
         INT 21H

       @SKIP:                     ; jump label

       MOV CX, 8                  ; initialize loop counter
       MOV AH, 2                  ; set output function

       @LOOP_3:                   ; loop label
         SHL BL, 1                ; shift BL towards left by 1 position
         JC @ONE                  ; jump to label @ONE if CF=1
         MOV DL, 30H              ; set DL=0
         JMP @DISPLAY             ; jump to label @DISPLAY

         @ONE:                    ; jump label
           MOV DL, 31H            ; set DL=1

         @DISPLAY:                ; jump label
           INT 21H                ; print the character
       LOOP @LOOP_3               ; jump to label @LOOP_3 if CX!=0

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN
Posted
Updated 20-Oct-21 6:22am

1 solution

Compare each input digit to the value 1 and if it is greater then put a message and ask for it again. You could simplify things by creating a subroutine to do the collecting of the numbers which you call by:
C++
       LEA DX, PROMPT_2           ; load and display the string PROMPT_2
       CALL @GETNUM               ; get the number

; ...
; ... repeat for second number
; ... do compares and diplay results etc ...


And the actual subroutine would be something like:
C++
; this subroutine displays the prompt pointed to by DX
; and gets a binary number in AX
@GETNUM:
       MOV AH, 9
       INT 21H
 
       MOV CX, 8                  ; initialize loop counter
       MOV AH, 1                  ; set input function
@NUMLOOP:                   ; loop label
         INT 21H                  ; read a character

         CMP AL, 0DH              ; compare AL with CR
         JE @EXIT_LOOP            ; jump to label @EXIT_LOOP_2

         AND AL, 0FH            ; convert ascii into decimal code
;
; add code here to test for valid input
;
         SHL BH, 1              ; shift BH towards left by 1 position
         OR BH, AL              ; set the LSB of BH with LASB of AL
         LOOP @NUMLOOP               ; jump to label @LOOP_2 if CX!=0
@EXIT_LOOP:
         XOR AX,AX
         OR AL,BH
         RET                    ; return result in AX



You could also improve your code slightly by writing:
C++
CMP AL, 0DH              ; compare AL with CR
JE @EXIT_LOOP_2          ; CR character, jump to label @EXIT_LOOP_2

rather than
C++
         CMP AL, 0DH              ; compare AL with CR
         JNE @SKIP_2              ; jump to label @SKIP_2 if AL!=0DH
         JMP @EXIT_LOOP_2         ; jump to label @EXIT_LOOP_2

@SKIP_2:                          ; jump label
 
Share this answer
 
Comments
Hamza Javed 5-Dec-14 9:47am    
Thanks for your time :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900