Professional Documents
Culture Documents
Lab Manual-Section B
Lab Manual-Section B
LAB MANUAL
CS203-COMPUTER ORGANIZATION AND ASSEBMLY LANGUAGE
BSCS, 3rd Semester, Section B, Fall 2023
Course Instructor: Dr. Tariq Sadad
##########################################################
LAB 01 LECTURE
MASM INSTALLATION, AL PROGRAM STRUCTURE, BASICS CONCEPTS
mount c c:\mp
c:
c:\>edit first.asm
dosseg
.model small
.stack 100h
.data
.code
main proc
Interrupt
Stop the current program and allow the microprocessor to access
hardware to take input or give output.
ASCII Code
When output is not exact, and it is a smiley face or something else
that is actually ascii code.
----------------------------------------------------------------
LAB 01 TASKS
LAB TASK 1: Download and install DOSBOX on lab computers and execute
your first AL program? Write the program execution steps?
LAB TASK 2: Write an AL program to print your university registration
number?
################################################
################################################
LAB 02 LECTURE
Install VSCDOE from website
https://code.visualstudio.com/
dosseg
.model small
.stack 100h
.data
.code
main proc
mov dl,'h'
mov ah,2
int 21h
mov dl,'a'
mov ah,2
int 21h
mov dl,'n'
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
OUTPUT: khan
LAB 03 LECTURE
REGISTERS, USER INPUT, ADDITION, SUBTRACTION
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ah,1
int 21h
mov dl,al
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
-------------------------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
;addressing mode
mov bl,3
mov cl,1
sub bl,cl
add bl,48
mov dl,bl
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
--------------------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
mov bl, 1
mov cl,2
add bl,cl
add bl,48
mov dl,bl
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
LAB 3 TASKS
mov dx,ax
moves data address to ds so that the data segment gets initialized as
heap memory to access variables fast.
Type mismatch
mov dx,var1 ; is incorrect if var1 size is 8bits.
Or
lea dx,var3
Load Effective Address: it is an indirect addressing instruction used
as a pointer in which first variable points the address of second
variable.
dosseg
.model small
.stack 100h
.data
var1 db '1'
var2 db ?
var3 db '12345$'
.code
main proc
mov ax,@data
mov ds,ax
mov dl,var1
mov ah,2
int 21h
;mov dl,var2
;mov ah,2
;int 21h
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
------------------------------------------------
dosseg
.model large
.stack 100h
.data
msg1 db 'hello$'
msg2 db 'world$'
.code
main proc
mov ax,@data
mov dx,ax
main endp
end main
LAB 4 TASKS
LAB TASK 1: Write an AL program by using different data types: DB, DW,
DD and print the different type of values.
LAB TASK 2:
################################################
################################################
LAB 5 LECTURE
JUMP AND LOOP INSTRUCTIONS
dosseg
.model small
.stack 100h
.data
.code
main proc
mov cx,10
mov dx,48 ;ascii code for 0
test1:
mov ah,2 ;print
int 21h ;interrupt to go out
add dx,1
;inc dx
loop test1
mov ah,4ch
int 21h
main endp
end main
ed
---------------------------------------------
dosseg
.model small
.stack 100h
.data
msg1 db 'numbers are equal$'
msg2 db 'numbers are not equal$'
.code
main proc
mov ax,@data
mov ds,ax
;comparing
cmp al,dl
je l1
l1:
mov dx,offset msg1
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
LAB 5 TASKS
################################################
################################################
LAB 6 LECTURE
ARRAY, DUP, SOURCE INDEX
dosseg
.data
arr1 db 1h,2h,3h,4h
.code
main proc
mov ax,@data
mov ds,ax
;mov dx,[si+1]
inc si
mov dx,[si]
mov ah,2
int 21h
inc si
mov dx,[si]
mov ah,2
int 21h
inc si
mov dx,[si]
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
---------------------------------------------
dosseg
.model small
.stack 100h
.data
arr db 'a','b','c'
.code
main proc
mov ax,@data
mov ds,ax
mov cx,3
l1:
mov dx,[si]
mov ah,2
int 21h
inc si
loop l1
mov ah,4ch
int 21h
main endp
end main
LAB 6 TASKS
################################################
################################################
LAB 07 LECTURE
STRING, JUMP INSTRUCTIONS
.model small
.stack 100h
.data
var1 db 100 dup("$")
.code
main proc
mov ax,@data
mov ds,ax
cmp al,13
je printString
mov [si],al
inc si
inc bl
jmp l1
printString:
mov dl, bl
mov ah,2
add dl, 48
int 21h
mov ah,4ch
int 21h
main endp
end main
----------------------------------
Program: Get an integer from user and display whether the number is
even or odd.
.model small
.stack 100h
.data
ev db 'Even$'
od db 'Odd$'
.code
main proc
mov ax,@data
mov ds,ax
mov ah,1
int 21h
mov bl,2
div bl
cmp ah,0
je IsEven
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
mov dx,offset od
mov ah,9
int 21h
mov ah,4ch
int 21h
IsEven:
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
mov dx,offset ev
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
---------------------------------------
.model small
.stack 100h
.data
ev db 'divisible by 2$'
od db 'divisible by 3$'
.code
main proc
mov ax,@data
mov ds,ax
mov ah,1
int 21h
mov bl,2
div bl
cmp ah,0
je IsEven
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
mov dx,offset od
mov ah,9
int 21h
mov ah,4ch
int 21h
IsEven:
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
mov dx,offset ev
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
LAB 7 TASKS
################################################
################################################
LAB 08 LECTURE
STRINGS, LOOP
dosseg
.model small
.stack 100h
.data
var1 db 20 dup('$')
.code
main proc
mov ax,@data
mov ds,ax
l1:
;taking input from user
mov ah,1
int 21h
mov [si],al
inc si
jmp l1
progend:
mov dx,offset var1
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
-----------------------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
mov cx,26
mov ah,2
mov dl,65
l1:
int 21h
inc dl
loop l1
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
mov cx,26
mov dl,97
mov ah,2
l2:
int 21h
inc dl
loop l2
mov ah,4ch
int 21h
main endp
end main
LAB 8 TASKS
################################################
################################################
LAB 09 LECTURE
ADDRESSING MODES, MEMORY MODELS
PROGRAM: find the largest number in an array, lrgnum.asm
.model small
.stack 100h
.data
STRING1 DB 1,3,8,5
res db ?
.code
main proc
mov ax,@data
mov ds,ax
mov cx, 4
mov bl, 0
LEA SI, STRING1
up:
mov al, [SI]
cmp al, bl
jl nxt
mov bl, al
nxt:
inc si
dec cx
jnz up
mov res,bl
mov dl,res
add dl,48
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
-----------------------------------------
dosseg
.model small
.stack 200
.data
crlf db 0Dh,0Ah,'$'
prompt1 db 'enter the first number$'
prompt2 db 'enter the second number$'
prompt3 db 'the sum of two numbers is: $'
.code
main proc
lea dx,prompt1
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,30h
mov cl,al
lea dx,crlf
mov ah,09h
int 21h
lea dx,prompt2
mov ah,09h
int 21h
mov ah,01h
int 21h
sub al,30h
add al,cl
mov cl,al
add cl,30h
lea dx,crlf
mov ah,09h
int 21h
lea dx,prompt3
mov ah,09h
int 21h
mov dl,cl
mov ah,02h
int 21h
mov ah,04ch
int 21h
main endp
end main
LAB 9 TASKS
LAB 10 LECTURE
STACK AND ITS OPERATIONS
PROGRAM: PUSH and POP operations in stack. stack1.asm
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ax,'3'
push ax
mov bx,'5'
push bx
mov dx,ax
mov ah,2
int 21h
mov dx,bx
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
----------------------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ax,'4'
push ax ;copies 4 to stack
mov dx,ax
mov ah,2
int 21h
mov dx,bx
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
------------------------------------------
dosseg
.model small
.stack 100h
.data
string db 'ali'
.code
main proc
mov ax,@data
mov ds,ax
mov si,offset string
mov cx,3
mov ah,4ch
int 21h
main endp
end main
LAB 10 TASKS
LAB TASK 1: Write a program to reverse the input string from keyboard
LAB TASK 2: Write a program to reverse the input numerical values from
keyboard
LAB TASK 3: Program: Jump conditions -> to input two numbers and check
if they are equal, unequal, greater or lesser.
################################################
################################################
LAB 11 LECTURE
STACK, NESTED LOOP
*
**
***
****
*****
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ax,@data
mov ds,ax
mov cx,5
L1:
push cx ;to keep the outer 5 reserved
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
;loop L2
inc bx
pop cx ;receive the cx back
loop L1
mov ah,4ch
int 21h
main endp
end main
LAB 11 TASKS
1
22
333
4444
55555
################################################
################################################
LAB 12 LECTURE
Procedures and Macros
dosseg
.model small
.stack 100h
.data
str1 db 'hello$'
str2 db 'how are you$'
str3 db 'good to see you$'
.code
main proc
mov ax,@data
mov ds,ax
call enterkey
call enterkey
mov ah,4ch
int 21h
main endp
enterkey proc
mov dx,10
mov ah,2
int 21h
mov dx,13
mov ah,2
int 21h
ret
enterkey endp
end main
--------------------------------------
dosseg
print macro p1
mov dx,offset p1
mov ah,9
int 21h
endm
.model small
.stack 100h
.data
str1 db 'hello$'
str2 db 'test program for macro$'
.code
main proc
mov ax,@data
mov ds,ax
print str1
print str2
mov ah,4ch
int 21h
main endp
end main
LAB 12 TASKS
################################################
################################################
LAB 13 LECTURE
DIVISION AND MULTIPLICATION USING DIV AND MUL
dosseg
.model small
.stack 100h
.data
quo db ?
rem db ?
.code
main proc
mov ax,26
mov bl,5
div bl
mov quo,al
mov rem,ah
mov dl,quo
add dl,48
mov ah,2
int 21h
mov dl,rem
add dl,48
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
---------------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
mov al,5
mov bl,2
mul bl
AAM
mov ch,ah ;1 in ah moves to ch
mov cl,al ;0 in al moves to cl
mov dl,ch ;1 to dl
add dl,48 ;convert to ascii to ready for print
mov ah,2
int 21h
mov dl,cl ;0 to dl
add dl,48
mov ah,2
int 21h
mov ah,4ch
int 21h
main endp
end main
--------------------------
LAB 13 TASKS
LAB TASK 1: Write a program to take input from user and divide two
numbers using procedure. Required output template is as below.
Enter Numerator:
Enter Denominator:
Quotient:
Remainder:
LAB TASK 2: Program to take input two numbers and multiply them, using
procedure. Output should look like as below.
Product is:
################################################
################################################
LAB 14 LECTURE
Shift and Rotate Instructions
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ax, 2
shl ax, 1
add ax, 48
mov dx, ax
mov ah, 2
int 21h
main endp
end main
-----------------------------
dosseg
.model small
.stack 100h
.data
.code
main proc
mov ax, 6
ror ax, 1
add ax, 48
mov dx, ax
mov ah, 2
int 21h
main endp
end main
------------------------------
LAB 14 TASKS
Task 1: Write a program to take input from the user and perform two
bits shift left.
Task 2: Write a program to take input from the user and perform three
bits rotate right.
Task 3: Using procedure, write a program to take input from the user
and perform two bits rotate left.
################################################
################################################
LAB 15 LECTURE
Addressing Modes: Instructions only
------------------------
MOVZX
----------------------
MOVSX
-------------------
XCHG
.DATA
var1 DWORD 10000000h
var2 DWORD 20000000h
.CODE
xchg ah, al ; exchange 8-bit regs
xchg ax, bx ; exchange 16-bit regs
xchg eax, ebx ; exchange 32-bit regs
xchg var1,ebx ; exchange mem, reg
--------------------------
Direct Memory Operands
.DATA
var1 BYTE 10h
.CODE
mov al, var1 ; AL = var1 = 10h
mov al,[var1] ; AL = var1 = 10h
--------------------------
Direct-offset operand
.DATA
arrayW WORD 1020h, 3040h, 5060h
arrayD DWORD 1, 2, 3, 4
.CODE
mov ax, arrayW+2
mov ax, arrayW[4]
mov eax,[arrayD+4]
-----------------------------
.data
var1 DWORD 100
var2 DWORD 200
sum DWORD ?
.code
mov eax, var1
add eax, var2
mov sum, eax
-------------------------
Register Indirect Addressing
.data
array DWORD 10000h,20000h,30000h
.code
mov esi, OFFSET array ; esi = array address
mov eax,[esi] ; eax = [array] = 10000h
add esi,4 ; why 4?
add eax,[esi] ; eax = eax + [array+4]
-----------------------------
Index Addressing
.data
array DWORD 10000h,20000h,30000h
.code
mov esi, 0 ; esi = array index
mov eax,array[esi] ; eax = array[0] = 10000h
add esi,4
add eax,array[esi] ; eax = eax + array[4]
------------------------------
Index Scaling
.DATA
arrayB BYTE 10h,20h,30h,40h
arrayW WORD 100h,200h,300h,400h
arrayD DWORD 10000h,20000h,30000h,40000h
.CODE
mov esi, 2
mov al, arrayB[esi] ; AL = 30h
mov ax, arrayW[esi*2] ; AX = 300h
mov eax, arrayD[esi*4] ; EAX = 30000h
-------------------------
Base Addressing
.DATA
mystruct WORD 12
DWORD 1985
BYTE 'M'
.CODE
mov ebx, OFFSET mystruct
mov eax, [ebx+2] ; EAX = 1985
mov al, [ebx+6] ; AL = 'M'
-------------------------
.data
matrix DWORD 0, 1, 2, 3, 4 ; 4 rows, 5 cols
DWORD 10,11,12,13,14
DWORD 20,21,22,23,24
DWORD 30,31,32,33,34
ROWSIZE EQU SIZEOF matrix ; 20 bytes per row
.code
mov ebx, 2*ROWSIZE ; row index = 2
mov esi, 3 ; col index = 3
mov eax, matrix[ebx+esi*4] ; EAX = matrix[2][3]
---------------------------------------
LAB 15 TASKS
################################################
################################################
LAB 16 LECTURE
Conditional Jumps Review, While loop, Switches
Mnemonic Description
JCXZ Jump if CX = 0
JECXZ Jump if ECX = 0
The following table shows a list of signed jumps based on comparisons of signed operands:
Mnemonic Description Condition Tested
JG, JNLE Jump if Greater, Jump if Not Less or Equal ZF = 0 and SF = OF
JGE, JNL Jump if Greater or Equal, Jump if Not Less SF = OF
JL, JNGE Jump if Less, Jump if Not Greater or Equal SF ≠ OF
JLE, JNG Jump if Less or Equal, Jump if Not Greater ZF = 1 or SF ≠ OF
The following shows a list of unsigned jumps based on comparisons of unsigned operands:
mov eax,var1
if( var1 == var2 ) cmp eax,var2
X = 1; jne elsepart
else mov X,1
X = 2; jmp next
elsepart:
mov X,2
next:
mov eax,var1
if (var1 <= var2) { cmp eax,var2
var3 = 10; jle ifpart
} mov var3,6
else { mov var4,7
var3 = 6; jmp next
var4 = 7; ifpart:
} mov var3,10
next:
WHILE Loops
LAB 15 TASKS
################################################
################################################