您好我使用的是dosbox和masm compilor。我想提示用户输入一个字符,并在下一行打印十六进制和二进制字符的ASCII码。重复此过程,直到用户键入回车符。下面的代码可以很好地显示十六进制的字符,直到回车,我如何修改此代码以显示charcter的二进制文件。?
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 0DH,0AH,'Enter the character : $'
PROMPT_2 DB 0DH,0AH,'The ASCII code of the given number in HEX form is : $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
@START: ; jump label
LEA DX, PROMPT_1 ; load and display the string PROMPT_1
MOV AH, 9
INT 21H
MOV AH, 1 ; read a character
INT 21H
MOV BL, AL ; move AL to BL
CMP BL, 0DH ; compare BL with CR
JE @END ; jump to label @END if BL=CR
LEA DX, PROMPT_2 ; load and display the string PROMPT_2
MOV AH, 9
INT 21H
XOR DX, DX ; clear DX
MOV CX, 4 ; move 4 to CX
@LOOP_1: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
RCL DL, 1 ; rotate DL towards left by 1 position
; through carry
LOOP @LOOP_1 ; jump to label @LOOP_1 if CX!=0
MOV CX, 4 ; move 4 to CX
@LOOP_2: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
RCL DH, 1 ; rotate DH towards left by 1 position
; through carry
LOOP @LOOP_2 ; jump to label @LOOP_2 if CX!=0
MOV BX, DX ; move DX to BX
MOV CX, 2 ; initialize loop counter
@LOOP_3: ; loop label
CMP CX, 1 ; compare CX wiht 1
JE @SECOND_DIGIT ; jump to label @SECOND_DIGIT if CX=1
MOV DL, BL ; move BL to DL
JMP @NEXT ; jump to label @NEXT
@SECOND_DIGIT: ; jump label
MOV DL, BH ; move BH to DL
@NEXT: ; jump label
MOV AH, 2 ; set output function
CMP DL, 9 ; compare DL with 9
JBE @NUMERIC_DIGIT ; jump to label @NUMERIC_DIGIT if DL<=9
SUB DL, 9 ; convert it to number i.e. 1,2,3,4,5,6
OR DL, 40H ; convert number to letter i.e. A,B...F
JMP @DISPLAY ; jump to label @DISPLAY
@NUMERIC_DIGIT: ; jump label
OR DL, 30H ; convert decimal to ascii code
@DISPLAY: ; jump label
INT 21H ; print the character
LOOP @LOOP_3 ; jump to label @LOOP_3 if CX!=0
JMP @START ; jump to label @START
@END: ; jump label
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN
答案 0 :(得分:2)
当你在寄存器中有值时,它以位(0/1编码为低/高电流电压)存储在CPU中,因此它实际上是#34;格式化的&#34;二进制!
您需要从最重要的位置开始,每位输出8个字符'0'
/ '1'
。
在AL
中包含字符的那一刻,输出二进制表单的代码可能如下所示:
cx = 8 ; 8 bits to output
bin_loop:
rcl al,1 ; move most significant bit into CF
setc bl ; bl = 0 or 1 by CF (80386 instruction)
add bl,'0' ; turn that 0/1 into '0'/'1' ASCII char
call display_bl ; must preserve al and cx
loop bin_loop
根据cx
对loop
的使用情况判断,您处于16b实模式。因此,如果您也不能使用80386指令(setc
)(当定位8086/80186/80286 CPU时,如emu8086
仿真器),则可以通过其他方式实现(如两条指令)例如,而不是一个)。
根据你在使用shl/rcl
的循环中使用CF,我确定你会想出一些东西,它非常相似。
答案 1 :(得分:0)
好的,Sarah,我对您的代码进行了更改,现在它显示了二进制文件,我添加了两个变量,所有更改都用小◄■
箭头指示:
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 0DH,0AH,'Enter the character : $'
PROMPT_2 DB 0DH,0AH,'The ASCII code of the given number in HEX form is : $'
PROMPT_3 DB 0DH,0AH,'The ASCII code of the given number in BIN form is : $'
MY_CHAR DB ? ; ◄■ char entered by user.
BINARY DB 9 DUP('$') ; ◄■ zeroes and ones.
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
@START: ; jump label
LEA DX, PROMPT_1 ; load and display the string PROMPT_1
MOV AH, 9
INT 21H
MOV AH, 1 ; read a character
INT 21H
MOV MY_CHAR, AL ; ◄■ save char to use in binary conversion.
MOV BL, AL ; move AL to BL
CMP BL, 0DH ; compare BL with CR
JE @END ; jump to label @END if BL=CR
LEA DX, PROMPT_2 ; load and display the string PROMPT_2
MOV AH, 9
INT 21H
XOR DX, DX ; clear DX
MOV CX, 4 ; move 4 to CX
@LOOP_1: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
RCL DL, 1 ; rotate DL towards left by 1 position
; through carry
LOOP @LOOP_1 ; jump to label @LOOP_1 if CX!=0
MOV CX, 4 ; move 4 to CX
@LOOP_2: ; loop label
SHL BL, 1 ; shift BL towards left by 1 position
RCL DH, 1 ; rotate DH towards left by 1 position
; through carry
LOOP @LOOP_2 ; jump to label @LOOP_2 if CX!=0
MOV BX, DX ; move DX to BX
MOV CX, 2 ; initialize loop counter
@LOOP_3: ; loop label
CMP CX, 1 ; compare CX wiht 1
JE @SECOND_DIGIT ; jump to label @SECOND_DIGIT if CX=1
MOV DL, BL ; move BL to DL
JMP @NEXT ; jump to label @NEXT
@SECOND_DIGIT: ; jump label
MOV DL, BH ; move BH to DL
@NEXT: ; jump label
MOV AH, 2 ; set output function
CMP DL, 9 ; compare DL with 9
JBE @NUMERIC_DIGIT ; jump to label @NUMERIC_DIGIT if DL<=9
SUB DL, 9 ; convert it to number i.e. 1,2,3,4,5,6
OR DL, 40H ; convert number to letter i.e. A,B...F
JMP @DISPLAY ; jump to label @DISPLAY
@NUMERIC_DIGIT: ; jump label
OR DL, 30H ; convert decimal to ascii code
@DISPLAY: ; jump label
INT 21H ; print the character
LOOP @LOOP_3 ; jump to label @LOOP_3 if CX!=0
;▼ FROM CHAR TO BINARY ▼
LEA SI, BINARY+7 ; ◄■ point to string in data segment.
MOV CX, 8 ; ◄■ maximum number of binary digits.
@BIN_CONVERSION:
SHR MY_CHAR,1 ; ◄■ get rightmost bit.
JC @BIT1
MOV [BYTE PTR SI], '0'
JMP @BIN_SKIP
@BIT1:
MOV [BYTE PTR SI], '1'
@BIN_SKIP:
DEC SI
LOOP @BIN_CONVERSION
LEA DX, PROMPT_3 ; ◄■ display message.
MOV AH, 9
INT 21H
LEA DX, BINARY ; ◄■ display binary.
MOV AH, 9
INT 21H
JMP @START ; jump to label @START
@END: ; jump label
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN