用汇编语言编写:从键盘输入一个十进制数(100以内),将其转变为二进制数并在屏幕输出

2024-12-04 05:17:46
推荐回答(2个)
回答(1):

DSEG SEGMENT
INPUT DB 'INPUT A NUMBER:','$'
OUTPUT DB 32,'RESULT IS:','$'
NUMBER DB 3
DB ?
db 3 DUP(0)
DSEG ENDS

CSEG SEGMENT
assume cs:CSEG, ds:DSEG
MAIN PROC FAR
mov ax, dseg
mov ds, ax
lea dx,INPUT
mov ah,9
int 21h
lea dx,NUMBER
mov ah,0ah
int 21h
lea dx,OUTPUT
mov ah,9
int 21h
mov bl,NUMBER+2
sub bl,30h
cmp NUMBER+1,1
jz jp0
mov al,10
mul bl
add al,NUMBER+3
sub al,30h
mov bl,al
jp0:mov ah,2
mov cx,8
jp1:shl bl,1
mov dl,'1'
jc out1
dec dl
out1:int 21h
loop jp1

mov ah,1 ;按任意键退出
int 21h
mov ax, 4c00h
int 21h
MAIN ENDP
CSEG ENDS
END MAIN

0~99都有效 欢迎追问 求满意

回答(2):

        CODE  SEGMENT
              ASSUME    CS:CODE
        MSG1  DB        13,10,'please input a number(0~99): $'
        MSG2  DB        13,10,'the bin number is: $'
           X  DB        ?

      START:  PUSH      CS
              POP       DS
              PUSH      CS
              POP       ES
              ; 以下显示输入数据提示 
              LEA       DX,MSG1
              MOV       AH,9
              INT       21H
              ; 以下输入1~2位 10进制数 
              ; 输入的10进制数转为二进制数放置于 bl中 
              MOV       AH,1
              INT       21H
              AND       AL,0FH
              MOV       BL,AL
              INT       21H
              CMP       AL,13
              JE        @1
              AND       AL,0FH
              SHL       BL,1
              MOV       BH,BL
              SHL       BL,1
              SHL       BL,1
              ADD       BL,BH
              ADD       BL,AL
         @1:
              ; 以下显示输出提示 
              LEA       DX,MSG2
              MOV       AH,9
              INT       21H
              ; 以下循环逐位输出二进制数               
              MOV       CX,8
              MOV       AH,2
         @2:
              ROL       BL,1
              JC        @3
              MOV       DL,'0'
              JMP       @4
         @3:
              MOV       DL,'1'
         @4:
              INT       21H
              LOOP      @2
              ; 以下 返回dos 
              MOV       AH,4CH
              INT       21H
        CODE  ENDS
              END       START