Microprocessor Lab for BE Electronics @ Pulchowk Campus
Lab 10: Assembly Language Programming - Input/Output
In most cases we have to take input from the keyboard and display the result on the screen.
Since the input taken from the user is in ASCII format because the keyboard is an ASCII
encoder. Similarly to display a character it should be in ASCII format again. For the characters it
is fine, but what happens when we have to process numbers. In this case we have to convert the
ASCII character into numeric form when taking input and before displaying we have to convert
the number into ASCII character form.
To convert a character into the corresponding number, that is read by INT 21H function 01 we
process as follows.
MOV AH,01
INT 21H
SUB AL,30H ;Converts the ASCII character to binary number
While taking multiple digit numbers we have to make a loop to read characters (actually
numbers), convert the characters to numbers as above and store the final number in some
location in the data segment.
For multiple digit numbers we process as follows.
.DATA
NUM DB 0 ;Space for three digit number
COUNT DB 0 ;Counter to count the no of digits entered
NEWLN DB 0DH,0AH,'$'
.CODE
MAIN PROC FAR
MOV AX,@DATA
MOV DS,AX
MOV CX,03 ;For three digit decimal number
MOV SI,OFFSET NUM
;Read characters for a number and add with prev num
L1: MOV AH,01
INT 21H
CMP AL,0DH ;Stop if the user presses return
JE LP
SUB AL,30H ;Convert the digit to ASCII
PUSH AX ;Save the digit for further purpose
MOV AL,10 ;Multiply NUM by ten and store it back
MUL NUM ;As we are using byte operation the result
MOV NUM,AL ;will not be greater than byte
POP AX ;recover back the number
ADD NUM,AL ;Add it with the previous result
INC COUNT ;Count the number of digits entered
LP: LOOPNE L1
Without converting the digits entered at the same instant, the read digit is first stored in a
data segment then processed later. (You can refer to Abel's book page no 250).
Read a string stored in the memory and then convert to binary later.
For the displaying purpose also we proceed in the same way. In this case we do the reverse
process; convert the binary number to ASCII decimal format.
S. K. Ghimire, Jan 2025
1
Microprocessor Lab for BE Electronics @ Pulchowk Campus
To display a number in memory at NUM into decimal format we process as follows
;Back to the ASCII format for display
MOV CX,10
MOV AL,NUM
MOV AH,0
MOV BX,0
;Find each decimal digit of the number and store in stack
L4: MOV DX,0
DIV CX ;Divide by 10
ADD DX,30H ;Convert the digit to characters
PUSH DX ;Store the decimal digit in the stack
INC BX
CMP AX,0 ;Stop if the quotient is 0
JA L4
;Get characters from stack and display as decimal number
MOV AH,02
MOV CX,BX
DISP: POP DX
INT 21H ;Display the character
LOOP DISP
The processing can be done directly in the data segment. (See Abel's book). Convert the binary
number into ASCII decimal format and display the final ASCII string that represents the decimal
number.
Note: To convert the binary number into ASCII Hexadecimal format, divide the number by 16
instead of 10 and add 30H to the remainder from 0 to 9 and add 41H to the remainder from 10 to
15.
Assignments:
1. Write a program to find the sum of numbers from 1 to n. Read n from the user and display
the sum in decimal format. (You can assume n as a single digit at first and you can modify
it to read n as a 3-digit integer).
2. Write a program that reads a string from the keyboard and count the no of words in the
string and display it in decimal format in the next line.
3. Write a program that reads a string from the keyboard and count the no of vowels in the
string. Display the count in decimal format.
4. Write a program to add the sequence 1×2+2×3+3×4+... up to n terms entered by the user
and display the result in decimal format.
5. Write a program to find the sum of the following series up to the terms specified by the
user and display the result in decimal format. 2 × 4 + 3 × 6 + 4 × 8 +… to n terms.
6. Write a program to read a digit and display the factorial in decimal format (you can
process the result up to 16-bits only).
Optional Assignments:
7. Write an assembly language program that reads a number from the user and displays the
multiplication table of it on the screen.
8. Write a program to find the sum of the series 1×22+2×32+3×42+... up to the terms entered
by the user and display the result in decimal format. (also try to display the sum in Hex
format)
9. Write a program to read ten decimal numbers (up to 16-bits) and store them in memory
and display the sum in decimal format.
***
S. K. Ghimire, Jan 2025
2