【汇编】输入两位10进制数,屏幕显示其二进制

时间:2022-12-21 01:05:38
 1 DATA SEGMENT 
 2 STRING DB 'pls input your number(double-digit) to display(binary system)',0DH,0AH,'$'
 3 BUF DB 3 DUP('$');缓冲区
 4 DATA ENDS
 5 CODE SEGMENT
 6      ASSUME CS:CODE,DS:DATA
 7 MAIN PROC FAR
 8      MOV AX,DATA
 9      MOV DS,AX 
10 
11      LEA DX,STRING;输出字符串
12      MOV AH,09H
13      INT 21H
14 
15      MOV AH,0AH;键盘输入
16      LEA DX,BUF
17      INT 21H
18 
19      MOV DL,0AH
20      MOV AH,02H
21      INT 21H
22 
23      LEA SI,BUF+2;地址最高位
24      MOV BL,[SI]
25      SUB BL,30H
26      MOV AL,10
27      MUL BL
28      MOV BL,[SI+1]
29      ADD AL,BL
30      SUB AL,30H
31      MOV BL,AL;不能对AL左移?为啥
32      MOV CX,08H
33 PRINT:  
34      SHL BL,1
35      JC   ONE
36      JNC  ZERO
37 
38 ZERO:
39      MOV DL,30H
40      MOV AH,02H
41      INT 21H
42      LOOP PRINT
43      MOV AH,4CH
44      INT 21H
45 ONE:
46      MOV DL,31H
47      MOV AH,02H
48      INT 21H
49      LOOP PRINT 
50      MOV AH,4CH
51      INT 21H
52 MAIN ENDP
53 CODE ENDS
54      END MAIN

将字符ASCII转化为16进制的数:例如‘12’——(31-30h)*10+32-30h后存入寄存器00001100。

转换后对此数左移8次,每次检测进位C标志位,如果有进位,则显示1,否则0;

另一种方法是,不移动此目标数,另BL=10000000,让BL右移8次,TEST(与)指令去检测每一位,为1的话显示1,否则0。下面是程序

 1 DATA SEGMENT 
 2 STRING DB 'pls input your number(double-digit) to display(binary system)',0DH,0AH,'$'
 3 BUF DB 3 DUP('$');缓冲区
 4 DATA ENDS
 5 CODE SEGMENT
 6      ASSUME CS:CODE,DS:DATA
 7 MAIN PROC FAR
 8      MOV AX,DATA
 9      MOV DS,AX 
10 
11      LEA DX,STRING;输出字符串
12      MOV AH,09H
13      INT 21H
14 
15      MOV AH,0AH;键盘输入
16      LEA DX,BUF
17      INT 21H
18 
19      MOV DL,0AH
20      MOV AH,02H
21      INT 21H
22 
23      LEA SI,BUF+2;地址最高位
24      MOV BL,[SI]
25      SUB BL,30H
26      MOV AL,10
27      MUL BL
28      MOV BL,[SI+1]
29      ADD AL,BL
30      SUB AL,30H
31      MOV BL,AL;不能对AL左移?为啥
32      MOV CX,08H
33      MOV BH,80H
34 PRINT:  
35      TEST BL,BH
36      JNZ  ONE
37      JZ  ZERO
38 
39 ZERO:
40      MOV DL,30H
41      MOV AH,02H
42      INT 21H
43      SHR BH,1
44      LOOP PRINT
45      MOV AH,4CH
46      INT 21H
47 ONE: