编写一汇编语言程序,要求从键盘接收一个四位的十六进制数,并在终端上显示与它等值的二进制数和十进制数

2025-03-29 09:07:14
推荐回答(2个)
回答1:

参考:
http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/a5fcfaf6a09cd93e730eeca8.html

;从键盘输入四位16进制数,以2进制、10进制输出
;=============================================
DATA SEGMENT

STR1 DB 'INPUT A HEX NUM: $'
HEX DB 5 ;计划输入的字符个数(含回车符)
NNN DB 0 ;存放实际输入的字符个数
DB 5 DUP(?)
BIN DW ? ;输入数字的二进制形式的存放单元
ERR DB 0AH, 0DH, ' ERROR !'
DB 0AH, 0DH, ' I HAVE NO TIME TO PLAY WITH YOU ! $'
STR2 DB 13, 10, 'BINARY IS: $'
DATA ENDS
;--------------------------------------------
STACK SEGMENT PARA STACK 'STACK'
DB 100 DUP(?)
STACK ENDS
;--------------------------------------------
CODE SEGMENT
ASSUME CS:CODE, SS:STACK, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AX, STACK
MOV SS, AX

LEA DX, STR1 ;'INPUT A HEX NUM'
MOV AH, 9 ;显示字符串
INT 21H

LEA DX, HEX
MOV AH, 10 ;等着输入四位HEX数
INT 21H
;---------------------------------------
;下面进行转换

MOV BIN, 0 ;先把二进制数字单元清零

LEA SI, HEX + 2;取输入字符串的首地址
MOV CH, NNN ;取输入字符的个数,用来控制循环处理的次数

CONV:
MOV BL, [SI] ;取来一个字符
CMP BL, 0DH ;和回车符进行比较
JZ END0 ;是, 即结束

CMP BL, '0'
JB ERROR ;小于'0'转移
CMP BL, 'F'
JA ERROR ;大于'F'转移

CMP BL, '9'
JNA A2B ;不大于'9', 就去变换
CMP BL, 'A'
JNB A2B ;不小于'A', 就去变换

JMP ERROR ;其它符号都是错误
;---------------------------------------
A2B:
SUB BL, 30H ;ASC-->BIN
CMP BL, 10
JB A2B_END
SUB BL, 07H ;ASC-->BIN
A2B_END:
;---------------------------------------
MOV BH, 0 ;弄成16位数

PUSH CX

MOV AX, BIN ;把原来的数字乘以16
MOV CX, 16
MUL CX
ADD AX, BX ;加上新输入的
MOV BIN, AX ;存结果,这就是二进制数

INC SI

POP CX
DEC CH ;处理次数减一
JNZ CONV ;非零转移
;---------------------------------------
DISP:
LEA DX, STR2 ;'BINARY IS: $'
MOV AH, 9 ;显示字符串
INT 21H
;---------------------------------------
MOV AX, BIN ;取来数字
MOV CX, 16 ;显示16位数
DDDD:
MOV DL, '0' ;先计划显示0
TEST AX, 8000H ;测试最高位
JZ DDD1
INC DL ;非0就显示1
DDD1:
PUSH AX
MOV AH, 2 ;显示DL中的ASCII码
INT 21H
POP AX

SHL AX, 1 ;左移
LOOP DDDD ;CX-1,非零转移

JMP END0 ;16位显示完毕,转到结束
;---------------------------------------
ERROR:
LEA DX, ERR
MOV AH, 09H ;错误信息提示
INT 21H
;---------------------------------------
END0:
MOV DL, 10
MOV AH, 2 ;显示DL中的ASCII码
INT 21H

MOV DL, 13
MOV AH, 2 ;显示DL中的ASCII码
INT 21H
;---------------------------------------
MOV AX, BIN ;取来数字
CALL DISP_AX

MOV AH, 4CH ;返回DOS
INT 21H
;------------------------------
DISP_AX:
MOV BX, 10 ;10进制.
MOV CX, 5 ;5位.
ABC:
MOV DX, 0
DIV BX
PUSH DX ;保存余数
LOOP ABC
MOV CX, 5 ;显示5位.
ASD:
POP DX
CMP DL, 10
JB A48
ADD DL, 7
A48:
ADD DL, 48
MOV AH, 2
INT 21H ;输出.
LOOP ASD
RET
;------------------------------
CODE ENDS
;---------------------------------------
END START
;=============================================

程序执行效果如下:

c:\masm510>H_BD
INPUT A HEX NUM: D4A2
BINARY IS: 1101010010100010
54434
c:\masm510>

回答2:

        CODE  SEGMENT
              ASSUME    CS:CODE

      NUMBER  DB        5,?,5 DUP(?)
        LFCR  DB        13,10,'$'

      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              LEA       DX,NUMBER
              MOV       AH,10
              INT       21H
              LEA       DX,LFCR
              MOV       AH,9
              INT       21H
              LEA       SI,NUMBER
              ADD       SI,2
              MOV       BP,0
              MOV       CX,4
              CLD
         @1:
              PUSH      CX
              MOV       CX,4
              ROL       BP,CL
              LODSB
              CALL      ASCII2HEX
              XOR       AH,AH
              ADD       BP,AX
              POP       CX
              LOOP      @1

              MOV       AX,BP
              MOV       BX,10
              CALL      DSPAXX
              MOV       AX,BP
              MOV       BX,2
              CALL      DSPAXX

              MOV       AH,4CH
              INT       21H
;===============================
   ASCII2HEX  PROC      NEAR
              CMP       AL,'0'
              JB        @BYTE9
              CMP       AL,'9'
              JA        @BYTE2
              AND       AL,0FH
              RET
     @BYTE2:
              CMP       AL,'A'
              JB        @BYTE9
              CMP       AL,'F'
              JA        @BYTE4
              SUB       AL,37H
              RET
     @BYTE4:
              CMP       AL,'a'
              JB        @BYTE9
              CMP       AL,'f'
              JA        @BYTE9
              SUB       AL,57H
              RET
     @BYTE9:
              MOV       AL,0        ; 错误的输入当0对待
              RET
   ASCII2HEX  ENDP
 ;================================  
     ; 输出ax的值,输出的进位制在bx中设定,bx=2~16  
      DSPAXX  PROC      NEAR
              PUSH      AX
              PUSH      BX
              PUSH      CX
              PUSH      DX
              PUSHF
              XOR       CX,CX
              ; MOV       BX,16
  @DSPAXX_1:
              XOR       DX,DX
              DIV       BX
              INC       CX
              OR        DX,30H
              PUSH      DX
              CMP       AX,0
              JNE       @DSPAXX_1
              MOV       AH,2
 @DISPAXX_2:
              POP       DX
              CMP       DL,3AH
              JB        @DISPAXX_3
              ADD       DL,7
 @DISPAXX_3:
              INT       21H
              LOOP      @DISPAXX_2
              MOV       DL,32
              INT       21H
              POPF
              POP       DX
              POP       CX
              POP       BX
              POP       AX
              RET
      DSPAXX  ENDP
;===============================
 
        CODE  ENDS
              END       START