Q) Read an alphanumeric character and display its equivalent ASCII code at the center of the screen.
This program will get an alphanumeric character from the keyboard and display its ASCII equivalent value on the screen at the center.
Algorithm:
Step 1: Read any alphanumeric character from keyboard using ‘01H’ DOS service.
Step 2: Set the cursor to center of the screen using ‘02H’ BIOS service with int 10H.
Step 3: Duplicate the value of AL in BL register for later reference and separate 2 digits(Example, 35 will be split as 03 and 05 for conversion of each bit to its respective ASCII) as follows.
i) 03 is got by right shifting the contents of the contents AL register 4 times (one digit takes 4 bits).
ii) Check whether the content of AL is now less than ‘0AH’ (that is 10) or not.
iii) if YES, add AL with 30H to get the equivalent ASCII value of the data present in AL register.
iv) if NO, then first add 07H and then 30H to get equivalent ASCII value of data present in AL.
CODE:
Explanation:
- .MODEL SMALL
- PLACE MACRO X,Y
- MOV AH,02H
- MOV DH,Y
- MOV DL,X
- INT 10H
- ENDM
PLACE is a macro to place the cursor to the desired axis on the screen. So its parameters are X and Y.
We can place the cursor to a defined location on the screen by moving the DOS service number, ‘02H’ to AH register. And Row number in ‘DH’ register and column number in ‘DL’ register. And then call Interrupt ‘INT 21H’ to place the cursor to the given <row(DX),col(DL)> on the screen.
ENDM is the end of the PLACE macro. Macros can also be written as a separate file outside and including it in the program using INCLUDE instruction(as we did in the 2A program).
- .DATA
- MSG1 DB 10,13,"ENTER THE ALPHANUMERIC CHARACTER:$"
- MSG2 DB 10,13,"ASCII CODE IS:$"
- .CODE
- MOV AX,@DATA
- MOV DS,AX
- PLACE 18H,0BH
- MOV DX,OFFSET MSG1
- MOV AH,09H
- INT 21H
Then PLACE macro is called with two parameters for row and column to place the cursor to the appropriate position on the screen. MSG1 should be printed now for the user, asking to enter a string. Same is done. Offset address of MSG1 is loaded to DX register. (LEA could have been used too). Then move the DOS code ‘09H’ to display a message to AH register and call the interrupt INT 21H. MSG will be displayed.
NOW,
If the user enters a character,Its ASCII value will be stored in the AL register. So in order to print that value, first we need to get the ASCII equivalent of ‘Each digits in the ASCII value’ of the character entered. So we will take each digit of ASCII(each 4 bit values in the AL) and convert it into their ASCII equivalent and then print them.
There are some rules to be followed during the conversion of 8 bit number to ASCII. Those are:
- If the 8 bit number represent a value less than 0AH (which is 10 in decimal), we need to just add 30H to get the ASCII value.
- If the 8 bit number represents a value greater than 0AH, then we need to first add 7H and then add 30H to get the ASCII.
0000 1011 (which is 0BH) we know that this is actually the number 012 according to Hexadecimal Number system. But to get it during computation, we will add 07H to 0BH and see what happens,
0000 1011 (which is BH)
+ 0000 0111 (which is 07H)
We get,
0001 0010 (which is 12).
Now that we got 12, add 30H to get the ASCII.
Once you are clear with this above part, No doubt that you will understand the code below.
- LOOP1:
- MOV AH,01H
- INT 21H
- CMP AL,00H
- JE LOOP1
Concept in brief, before the logic:
Suppose that our input is ‘2’ whose ASCII value is 32. The input will be stored as (0011 0010) in AL.
So what we will do is, Get the first digit (‘3’) of the ASCII(first 4 bits of AL) and convert into its ASCII equivalent and print it on screen. Then take second digit (‘2’) of the ASCII(Last 4 bits of AL) and convert into its ASCII equivalent and print.
In order to get the first 4 bits from AL. We will perform the AND operation of AL and ‘F0H’ (which is 1111 0000). The AND will result in ‘0011 0000’. This will be in AL now. Perform a right shift on AL by 4 bits to bring the value to LSB. This will result in ‘0000 0011’. Which is nothing but the decimal value ‘3’. So we got the first digit of the ASCII ‘32’.
Now we have to convert ‘3’ into its ASCII to print it on the screen. As per the conversion rules, 3 is less than 0AH. So simply add ‘30H’. We get ‘33H’ which is ASCII of ‘3’. Print it on the screen using DOS service ‘02H’.
[Important: We need to back up our AL into Some register before starting the process(since AL is going to change during the right shift and we will lose it permanently).]
Then get the second digit, Restore the original value of AL which is backed up in BL. This time, to eliminate the first 4 bits, perform AND operation of AL with ‘0FH’ (which is 0000 1111)
Consider the AL value, ‘0011 0010’. On ‘AND operation’ with ‘0000 1111’ , it will result in ‘0000 0010’. Which is nothing but the decimal value ‘2’. Compare it with 0AH and add ‘07H’ if greater than ‘0AH’. Add 30H to AL and you got the second digit ASCII now. Print it.
Now the code,
- MOV BL,AL
- MOV CL,04H
- AND AL,0F0H
- SHR AL,CL
- CMP AL,0AH
- JL NEXT1
- ADD AL,07H
- NEXT1:
- ADD AL,30H
- PUSH AX
- PUSH DX
- MOV DX,OFFSET MSG2
- MOV AH,09H
- INT 21H
- POP DX
- POP AX
- MOV DL,AL
- MOV AH,02H
- INT 21H
Then AL is ANDed with ‘F0H’ to eliminate the last four bits. ( while writing code, use 0F0H since first letter ’F’ is a character). Now we get the first character.
Then the value is compared to 0AH using ‘CMP’ as per the rules and if it is less than 0AH, simply move to label NEXT1 to add 30H and display on the screen. Otherwise add ‘07H’ and next add ‘30H’.
Print the message MSG2 using 09H service on INT 21H. Push the values of AX and DX into the stack since you are going to lose the present values of those, while displaying the message MSG2. After printing, Pop them back to the same registers.
Now print the first digit of ASCII to the screen using ‘02H’ service on INT 21H.
- MOV AL,BL
- AND AL,0FH
- CMP AL,0AH
- JL NEXT2
- ADD AL,07H
- NEXT2:
- ADD AL,30H
- MOV DL,AL
- MOV AH,02H
- INT 21H
- MOV AH,4CH
- INT 21H
- END
Program is terminated using 4CH service and END declares End of the program.
OUTPUT:
ENTER THE ALPHANUMERIC CHARACTER: 2
ASCII CODE IS: 32
2 comments
Write commentsWonderful efforts!
ReplyThanx bro :)
ReplyShare your views about this article!