Hi i m using dosbox and masm compilor. I want to prompts the user to enter a character, and prints the ASCII code of the character in hex and in binary on the next line. Rep
Ok, Sarah, I made the changes to your code, now it displays binary, I added two variables, all the changes are pointed by little ◄■
arrows :
.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