CO Lab Manual-15-33

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Lab 5

Finding the Average of three numbers

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

Increment and Decrement

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

Decision-Making and Branching

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].

8.1. Comparing numbers


The cmp instruction has two operands:

cmp reg/mem, reg/mem/constant

8.2. Flag-based jumps


The jumps based on the flags being set/reset are:
Instruction Description
JE jump if equal
JNE Jump if not equal
JZ shortlabel jump if zero
JNZ shortlabel jump if not zero
.MODEL SMALL
.STACK 100h
.DATA
msg DB 10, 13, 'This program displays Congratulations!!! if you enter 5 $', 10, 13
msg1 DB 10, 13, 'Congratulations!!! $', 10, 13
NUM1 DB ?
.CODE
MAIN PROC
MOV AX, @data ; Required at the start of every program (inside your main procedure, from what I've seen)
MOV DS, AX
LEA DX, msg ; load and display the msg
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 NUM1,AL
MOV AH, 2 ; carriage return
MOV DL, 0DH
INT 21H
CMP NUM1, 5 ; Compare NUM1 to 5...
JE DispMsg1 ; Jump-if-Equal...CMP NUM1
JMP SkipMsg ; Jump to the SkipMsg label .
DispMsg1:
MOV AH, 09H ; Displays the message stored in the defined byte "msg1"
MOV DX, OFFSET msg1
INT 21H
SkipMsg:
MOV AL, 0h ; Op code to exit to DOS from the assembler.
MOV AH, 4CH
INT 21H
MAIN ENDP
END MAIN
23
Output

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

Loops in Assembly Language

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].

❖ The syntax of the Loop instruction is:


o LOOP label
o The Loop instruction decrements CX without changing any flags
o If CX is not zero after the decrement, control is transferred to the destination label
- The following program prints the numbers from 1 to 9 using loops.
Code Output
.model small
.STACK 100h
.data
num db
.code
main proc
mov cx, 9
mov num,1
top:
mov dl, num
add dl, 48
mov ah, 2h Class activity:
int 21h
Write a simple program using GUI Turbo Assembler that
inc num
prints the numbers from 2 to 7 using loop instruction.
loop top
MOV AL, 0h
MOV AH, 4CH
INT 21H
endp
end main

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

AND – OR Logical Operators

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

You might also like