Professional Documents
Culture Documents
CO Lab Manual-15-33
CO Lab Manual-15-33
CO Lab Manual-15-33
15
6. Calculating the Average of three numbers using TASM
Code:
dosseg
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter the First number : $'
PROMPT_2 DB 'Enter the Second number : $'
PROMPT_3 DB 'Enter the Third number : $'
PROMPT_4 DB 'The average of three number = $'
NUM1 DB ?
NUM2 DB ?
NUM3 DB ?
AVG DB ?
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX ; display massage and enter the first number and save it in num1 variable
LEA DX, PROMPT_1 ; load and display the PROMPT_1
MOV AH, 9
INT 21H
MOV AH, 1 ; read a character
INT 21H
SUB AL, 30H ; save First digit in VALUE_1 in ASCII code
MOV NUM1,AL
MOV AH, 2 ; carriage return
MOV DL, 0DH
INT 21H
MOV DL, 0AH ; line feed
16
INT 21H
; display massage and enter the Second number and save it in num2 variable
LEA DX, PROMPT_2 ; load and display the PROMPT_2
MOV AH, 9
INT 21H
MOV AH, 1 ; read a character
INT 21H
SUB AL, 30H ; save First digit in NUM1 in ASCII code
MOV NUM2,AL
MOV AH, 2 ; carriage return
MOV DL, 0DH
INT 21H
MOV DL, 0AH ; line feed
INT 21H
; display massage and enter the third number and save it in num3 variable
LEA DX, PROMPT_3 ; load and display the PROMPT_3
MOV AH, 9
INT 21H
MOV AH, 1 ; read a character
INT 21H
SUB AL, 30H ; save First digit in NUM3 in ASCII code
MOV NUM3,AL
MOV AH, 2 ; carriage return
MOV DL, 0DH
INT 21H
MOV DL, 0AH ; line feed
INT 21H
; Print massage in the prompt_4 on the screen
LEA DX, PROMPT_4 ; load and display the PROMPT_4
17
MOV AH, 9
INT 21H
; Calculate the sum of NUM1+NUM2+NUM3
MOV AL, NUM1
ADD AL, NUM2
ADD AL, NUM3
; Calculate the average by Divide the Sum in AL reg. by 3 (three numbers)
MOV AH,0
MOV DL,3
DIV DL
MOV AVG,AL
ADD AL, 30H ; convert ASCII to DECIMAL code
MOV AH, 2 ; display the character
MOV DL, AL
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
Output
Class activity:
Write a simple program using GUI Turbo Assembler to find the average of three numbers 8, 2, 5.
18
Lab 6
19
7. Increment and Decrement
.model small
.STACK 100H
.data
PROMPT_1 DB 'Enter the number and I will increment it: $'
PROMPT_2 DB ' the number after increment is: $'
.code
main proc
MOV AX, @DATA ; initialize DS
MOV DS, AX ; display massage and enter the first number and save it in num1 variable
LEA DX, PROMPT_1 ; load and display the PROMPT_1
MOV AH, 9
INT 21H
MOV AH,08 ; Function to read a char from keyboard
INT 21h ; the char saved in AL
MOV AH,02 ; Function to display a char
MOV DL,AL ; Copy a saved char in AL to DL to output it
INT 21h
LEA DX, PROMPT_2 ; load and display the PROMPT_2
MOV AH, 9
INT 21H
INC AL
MOV AH,02 ; Function to display a char
MOV DL,AL ; Copy a saved char in AL to DL to output it
INT 21h
0
MOV AH,4Ch ; Function to exit
MOV AL,00 ; Return 00
INT 21h
endp
end main 20
Output
Class activity:
Write a simple program using GUI Turbo Assembler
that decrements a number entered by the user.
21
Lab 7
22
8. Decision-Making and Branching
Decision-making is a two step process. First, two numbers are compared and the flags set accordingly. Then a
conditional jump is made based on those flags [5].
Class activity:
Write a simple program using GUI Turbo Assembler
that prints “HELLO” when the user enters a number
that doesn’t equals 1.
24
Lab 8
25
9. Loops
The LOOP instruction is mainly used to simulate the different loops in HLL. The Loop instructions use
the CX register to indicate the loop count [6].
26
Lab 9
Prime numbers
27
10. Prime numbers
The following code checks whether the number entered is prime or not.
dosseg
.MODEL SMALL
.stack 100H
.DATA
VAL1 DB ?
NL1 DB ' ENTER NUMBER: $'
NL2 DB ' IT IS NOT PRIME :$'
NL3 DB ' IT IS PRIME :$'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
LEA DX,NL1
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
SUB AL,30H
MOV VAL1,AL
MOV AH,00
MOV CL,2
DIV CL
MOV CL,AL
LBL1:
MOV AH,00
MOV AL,VAL1
28
DIV CL
CMP AH,00
JZ LBL2
DEC CL
CMP CL,1
JNE LBL1
JMP LBL3
LBL2:
MOV AH,09H
LEA DX,NL2
INT 21H
JMP EXIT
LBL3:
MOV AH,09H
LEA DX,NL3
INT 21H
Output
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
29
Lab 10
30
11. AND – OR Logical Operators
11.1 Upper case to lower case conversion using OR
The following program converts an upper-case letter to lower case letter using OR
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter the Upper Case Letter : $'
PROMPT_2 DB 0DH,0AH,'The Lower Case Letter is : $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, PROMPT_1 ; load and print PROMPT_1
MOV AH, 9
INT 21H
MOV AH, 1 ; read a letter
INT 21H
MOV BL, AL ; save the letter in BL
LEA DX, PROMPT_2 ; load and print PROMPT_2
MOV AH, 9
INT 21H
OR BL, 20H ; convert a upper case letter to lower case letter
MOV AH, 2 ; print the Lower case letter
MOV DL, BL
INT 21H Output
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN
31
11.2 Lower case to Upper case conversion using AND
The following program converts a lower-case letter to upper case letter using AND
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter the Lower Case Letter : $'
PROMPT_2 DB 0DH,0AH,'The Upper Case Letter is : $'
.CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
LEA DX, PROMPT_1 ; load and print PROMPT_1
MOV AH, 9
INT 21H
MOV AH, 1 ; read a letter
INT 21H
MOV BL, AL ; save the letter in BL
LEA DX, PROMPT_2 ; load and print PROMPT_2
MOV AH, 9
INT 21H
AND BL, 0DFH ; convert a lower case letter to upper case letter
MOV AH, 2 ; print the Lower case letter
MOV DL, BL
INT 21H Output
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
END MAIN
32
12. References
[1] https://techterms.com/definition/assembly_language
[2] https://en.wikipedia.org/wiki/DOSBox
[3] https://en.wikipedia.org/wiki/Turbo_Assembler
[4] http://cssimplified.com/computer-organisation-and-assembly-language-programming/an-assembly-
program-to-read-in-two-decimal-inputs-and-print-out-their-sum-in-decimal
[5]https://www.csee.umbc.edu/courses/undergraduate/CMSC211/fall01/burt/lectures/Chap05/decisionMaking
.html
[6] https://faculty.kfupm.edu.sa/COE/aimane/assembly/pagegen-87.aspx.htm
33