本文介绍了如何在汇编语言中添加两个二进制数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序,提示用户输入两个最多8位的二进制数,并在二进制的下一行打印它们的总和。如果用户输入非法字符,则应提示他或她再次开始。每个输入都以回车结束。我想知道如何添加检查,以便如果用户输入0和1之外,他应该被提示有关错误的数字。
这是我的程序:
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:
.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
推荐答案
这篇关于如何在汇编语言中添加两个二进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!