Sunday, January 5, 2014

Input in Assembly Language Programming


You must think of function 'scanf' first. How simple it is! This C function reads the data from keyboard. As it is high level language function, it is very simple to understand. But, behind the scenes a lot many forces are active to make this 'scanf' function very successful! This all procedures can be seen in an assembly language program. When you want to read something from keyboard using microprocessor's programming language, it gives real demonstration of input procedures of computing. Lets see how it does it. I am writing the procedure here from DOS point of view. 

 
Most of processor's operations are based on interrupt. To read the data from keyboard, we need to invoke an interrupt! The DOS interrupt whose number is 21 in hexadecimal is required to accomplish this operation. An interrupt of computer needs the function number also. Now, to read a character DOS 21H interrupt needs function number 01. This function number must get stored into AH register of the processor.
The function number 01 an interrupt 21H is used to read a character from keyboard. So reading a character is so simple.

MOV AH,01H
INT 21H

This code reads a character from keyboard and processor immediately switches to next statement. The character which was pressed by user on keyboard will be stored in AL register! It usually stores the ASCII value of character pressed. In short, this function acts as getche() function if C. For example, if user pressed 'a' on keyboard then the value of AL will be 41 in hexadecimal! Now, how to read a 2 or 4 digit hex number, becomes a critical problem.

In this case, we may follow the following procedure for two digit number.
  1. Read the first digit number as a character.
  2. As the ASCII values of numbers and actual number has difference of 30, subtract 30 from AL For example, ASCII of 5 is 35.
  3. Swap the digits of two digit hex value in AL register. For example, AL=05 will become 50.
  4. Store this value in some other register such as BL.
  5. For reading second digit of the number, follow step 1 and 2 and finally add contents of AL and BL. Then we will get actual number that we read.

The following code will do the task for achieving this two digit number input.

1. MOV AH,01H
2. INT 21H ;reads first digit
3.
4. MOV BL,AL
5. SUB BL,30H
6. MOV CL,04H
7. ROL BL,CL
8.
9. MOV AH,01H
10. INT 21H ;reads second digit
11.
12. SUB AL,30H
13. ADD AL,BL ;AL will have number

For example, user entered 75 from keyboard then,
4. BL=37H
5. BL=07H
6. CL=04H
7. BL=70H
10. AL=35H
12. AL=05H
13. AL=75H ;AL with final value.

The code will work for digit entered between 0 to 9 only.

The hex number entered will be greater than 09H also. When user enters any hex digit between A to F then we have to add one more step in algorithm. In such case subtract the input value in AL by 07H also. Because the ASCII value difference between numerical value and character value is 07! So compare the value is below or equal to 09. For this, the conditional jump JBE/JNA can be used. Following code will do the task.

MOV AH,01H
INT 21H
MOV BL,AL
CMP BL,39H
JBE NEXT1

SUB BL,07H
NEXT1:
SUB BL,30H
MOV CL,04H
ROL BL,CL
MOV AL,01H
INT 21H
CMP AL,39H
JBE NEXT2
SUB AL,07H
NEXT2:
SUB AL,30H
MOV CL,04H
ADD AL,DL

Note: All the values used in this article are in hexadecimal form. It will work for x86 programming using MASM/TASM.

No comments:

Post a Comment