0% found this document useful (0 votes)
11 views20 pages

Combinepdf

The document outlines a series of practical labs for a Computer Architecture and Assembly Language Programming course (CS401P), focusing on assembly language programming using NASM and AFD debugger. Each lab includes problem statements that require students to write assembly programs for various tasks such as loops, summation, sorting, and subroutines, along with detailed solutions. Students are encouraged to seek help through email or weekly interactive sessions for any queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Combinepdf

The document outlines a series of practical labs for a Computer Architecture and Assembly Language Programming course (CS401P), focusing on assembly language programming using NASM and AFD debugger. Each lab includes problem statements that require students to write assembly programs for various tasks such as loops, summation, sorting, and subroutines, along with detailed solutions. Students are encouraged to seek help through email or weekly interactive sessions for any queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Computer Architecture and Assembly

Language Programming Practical (CS401P) Lab No. 1

Problem Statement

In this practical course (CS401P) Assembly language programming is taught by practicing code
over “NASM” assembler and ‘AFD’ debugger. So, it is required for every student to learn to install
these tools and set their programming environment.

Today’s computers are of 32-bit and 64-bit processors, these system does not support ‘NASM’
assembler directly, so DOSBOX is used. DISBOX is an emulator of the DOS operating system that
will provide you compatibility of NASM on all windows platforms.

You are required to install NASM and DosBox to set up your programming environment, assemble
an assembly language program, and debug it using the AFD debugger.

Solution

1) Download the DosBox and AssmSoft from the below links

AssmSoft : https://vulms.vu.edu.pk/Courses/CS401P/Downloads/AssmSoft.zip
DosBox : https://vulms.vu.edu.pk/Courses/CS401P/Downloads/DOSBox.zip

2) After downloading the above tools follow the tutorial, linked below, to know how to install
and mount the NASM in the DOSBox

https://vulms.vu.edu.pk/Courses/CS401P/Downloads/How%20to%20run%20NASM.mp4

Further, For any queries feel free to send an email at [email protected] or join the weekly interactive
session
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 2

Problem Statement

There are several types of jumps in assembly language programming which you can use with ‘labels’ to
implement the loops in your program(s). You are required to write the assembly language program as
described below:
1. Declare a variable as ‘total’.
2. Initialize the following registers:
a. Set ‘BX’ to 5
b. Set ‘AX’ to 0
c. Set ‘CX’ to 10
3. Implement the loop which should run 10 times and during each iteration:
a. It subtracts 1 from value in the ‘CX’ register
b. Add value in the ‘BX’ register to value in the ‘AX’ register
4. At the end of the program,
5. transfer the value in the ‘AX’ register to the variable ‘total’.
Debug the program in AFD debugger to verify that your program is running properly.

Solution

[org 0x0100]
mov bx,5
mov cx,10
mov ax,0
l1:
add ax, bx
sub cx, 1
jnz l1
mov [total], ax
mov ax,0x4c00
int 0x21
total: dw 0

For any queries feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 3

Problem Statement

In the Virtual University of Pakistan, each student is assigned a unique VUID. A VUID consists of
a total of 11 characters, with the first two characters being alphabets and the remaining nine
characters being numbers. These nine numbers are unique for every student. You are required to
write an assembly language program that finds the biggest digit from 9 numbers of your VUID.

Instructions

• Store your VUID (only numbers) in the array of digits in the memory
• Declare a variable in memory named as ‘max’ to store the biggest digit of
your VUID in that variable.
• Implement labels and the jumps to implement the loop
• Use your own VUID
▪ Example: BC191234567 is your VUID then, biggest digit is 9.

Solution

[org 0x0100]
id: db 1,2,3,4,5,6,7,8,9
max: db 0
mov si,1
mov cx,0
mov al, [id]
larger:
mov byte[max], al
find:
mov al,[id+si]
inc si
cmp al,[max]
jg larger
cmp si,8
jle find

mov ax, 0x4c00 ; terminate program


int 0x21

For any queries feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 4

Problem Statement

A VUID of Virtual University’s student consist of 9 numeric digits. You are required to store
these numeric digits as an array of digits in the memory. The goal of this lab is to design an
assembly language program that traverses this array, extracts individual digits and
accumulates their sum.

Instructions

• Store your VUID (only numbers) in the array of digits in the memory
• Declare a variable in memory named as ‘sum’ to store the total sum of your VUID in that
variable.
• Set the counter to numbers of digits of your VUID
• Implement labels and the jumps to implement the loop
• Implement the summation logic to sum all the numbers (use labels and jumps to implement
the loop)
• Use your own VUID
▪ Example: BC191234567 is your VUID then, biggest digit is 38.

Solution

[org 0x100] [org 0x100]


mov al,0 id: db 1,2,3,4,5,6,7,8,9
mov di,0 sum: db 0
mov cx,9 mov al,[id]
mov bl,0 mov [sum],al
summation: add al,[vuid+di] mov si,1
inc di
dec cx azhar: mov al,[id+si]
jnz summation add [sum],al
mov [sum],al inc si==9
mov ax, 0x4c00 cmp si,8
int 0x21 jle azhar
vuid: db 1,2,3,4,5,6,7,8,9
sum: db 0 mov ax, 0x4c00
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 5

Problem Statement

You are required to write an assembly language program which will sort array using bubble
sort, the sorting must be signed.
• Following numbers must be sorted:
-15, -30, -12, -50, 10, 20, 55, 40, 7, 0
• After sorting the numbers, the numbers must be in the following order:
-50, -30, -15, -12, 0, 7, 10, -20, 40,55
• You will store the sorted numbers in the same variable(array) declared earlier for unsorted
numbers.
• Hint: You can use different type of jumps to sort the array.
JE, JNE JZ JNZ

[org 0x0100]
jmp start
data: dw -15, -30, -12, -50, 10, 20, 55, 40, 7, 0
swap: db 0
start: mov bx, 0 ; initialize array index to zero
mov byte [swap], 0 ; rest swap flag to no swaps
loop1: mov ax, [data+bx] ; load number in ax
cmp ax, [data+bx+2] ; compare with next number
jle noswap ; no swap if already in order
mov dx, [data+bx+2] ; load second element in dx
mov [data+bx+2], ax ; store first number in second
mov [data+bx], dx ; store second number in first

mov byte [swap], 1 ; flag that a swap has been done


noswap:
add bx, 2 ; advance bx to next index
cmp bx, 18 ; are we at last index
jne loop1 ; if not compare next two
cmp byte [swap], 1 ; check if a swap has been done
je start ; if yes make another pass
mov ax, 0x4c00 ; terminate program
int 0x21
For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 6

Problem Statement

You are required to write an assembly language program which multiplies first 3 numeric
digits of your VUID with last three numeric digits of your VUID.

; 16bit multiplication
[org 0x0100]
multiplicand: dd 1300 ;16bit multiplicand 32bit space
multiplier: dw 500; 16bit multiplier
result: dd 0 ; 32bit result

jmp start

start: mov cl, 16 ; initialize bit count to 16


mov dx, [multiplier] ; load multiplier in dx

checkbit: shr dx, 1 ; move right most bit in carry


jnc skip ; skip addition if bit is zero
mov ax, [multiplicand]
add [result], ax ; add less significant word
mov ax, [multiplicand+2]
adc [result+2], ax ; add more significant word

skip: shl word [multiplicand], 1


rcl word [multiplicand+2], 1 ; shift multiplicand left
dec cl ; decrement bit count
jnz checkbit ; repeat if bits left

mov ax, 0x4c00 ; terminate program


int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 7

Problem Statement

Subroutines are also known as functions or procedures. They promote modularity by


breaking down complex tasks into smaller, manageable segments, enhancing code
readability and maintainability. Their reusable nature allows developers to efficiently
employ the same code logic across different sections of a program, significantly reducing
redundancy.
You are required to write an assembly language program that has subroutine(s) to find
biggest digit your VUID and stores the result in memory.

Solution
[org 0x0100]
jmp start

id: db 2,1,0,4,1,3,4,5,3
max: db 0

findBiggest:
mov al, [id]
larger:
mov byte[max], al
find:
mov al,[id+si]
inc si
cmp al,[max]
jg larger
cmp si,8
jle find
ret

start:
mov si,1
mov cx,0
call findBiggest

mov ax, 0x4c00 ; terminate program


int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 8

Problem Statement

Write an assembly language program that prints the hexadecimal representation of the
given number.

Instruction:

• Write one subroutine to clear the screen.


• Write another subroutine to print the number.
• Store given number(decimal) in any register and convert it to hexadecimal.

Solution_1:
[org 0x0100]
jmp start
; subroutine to clear the screen
clrscr: push es
push ax
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen
add di, 2 ; move to next screen location
cmp di, 4000 ; has the whole screen cleared
jne nextloc ; if no clear next position
pop di
pop ax
pop es
ret
; subroutine to print a number at top left of screen
; takes the number to be printed as its parameter
printnum: push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov ax, [bp+4] ; load number in ax
mov bx, 16 ; use base 16 for division
mov cx, 0 ; initialize count of digits
nextdigit:
mov dx, 0 ; zero upper half of dividend
div bx ; divide by 16
cmp dl,10
je numIs10
cmp dl,11
je numIs11
cmp dl,12
je numIs12
cmp dl,13
je numIs13
cmp dl,14
je numIs14
cmp dl,15
je numIs15
cmp dl,10
jl numIsLesser10

numIs10:
mov dl,'A'
jmp proceed
numIs11:
mov dl,'B'
jmp proceed
numIs12:
mov dl,'C'
jmp proceed
numIs13:
mov dl,'D'
jmp proceed
numIs14:
mov dl,'E'
jmp proceed
numIs15:
mov dl,'F'
jmp proceed
numIsLesser10:
add dl, 0x30 ; convert digit into ascii value

proceed:
push dx ; save ascii value on stack
inc cx ; increment count of values
cmp ax, 0 ; is the quotient zero
jnz nextdigit ; if no divide it again
mov di, 160; point di to top left column
nextpos: pop dx ; remove a digit from the stack
mov dh, 0x07 ; use normal attribute
mov [es:di], dx ; print char on screen
add di, 2 ; move to next screen location
loop nextpos ; repeat for all digits on stack
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret 2
start:
call clrscr ; call the clrscr subroutine
mov ax, 2275
push ax ; place number on stack
call printnum ; call the printnum subroutine
mov ax, 0x4c00 ; terminate program
int 0x21
Solution_2:
[org 0x0100]
jmp start
; subroutine to clear the screen
clrscr: push es
push ax
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov di, 0 ; point di to top left column
nextloc: mov word [es:di], 0x0720 ; clear next char on screen
add di, 2 ; move to next screen location
cmp di, 4000 ; has the whole screen cleared
jne nextloc ; if no clear next position
pop di
pop ax
pop es
ret
; subroutine to print a number at top left of screen
; takes the number to be printed as its parameter
printnum: push bp
mov bp, sp
push es
push ax
push bx
push cx
push dx
push di
mov ax, 0xb800
mov es, ax ; point es to video base
mov ax, [bp+4] ; load number in ax
mov bx, 16 ; use base 16 for division
mov cx, 0 ; initialize count of digits

nextdigit:
mov dx, 0 ; zero upper half of dividend
div bx ; divide by 16
cmp dl, 9
ja hexdigit ; jump if digit is above 9
add dl, 0x30 ; convert digit (up to 9) into ascii value
jmp proceed
hexdigit: add dl, 0x37 ; convert digit (above 9) into ascii value

proceed: push dx ; save ascii value on stack


inc cx ; increment count of values
cmp ax, 0 ; is the quotient zero
jnz nextdigit ; if no divide it again
mov di, 0 ; point di to top left column
nextpos: pop dx ; remove a digit from the stack
mov dh, 0x07 ; use normal attribute
mov [es:di], dx ; print char on screen
add di, 2 ; move to next screen location
loop nextpos ; repeat for all digits on stack
pop di
pop dx
pop cx
pop bx
pop ax
pop es
pop bp
ret 2
start:
call clrscr ; call the clrscr subroutine
mov ax, 5678
push ax ; place number on stack
call printnum ; call the printnum subroutine
mov ax, 0x4c00 ; terminate program
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 9

Problem Statement

Write an assembly language program that gets terminated when you press T from your
keyboard.

Solution
[org 0x0100]
jmp start
; keyboard interrupt service routine
kbisr: push ax
push es
mov ax, 0xb800
mov es, ax ; point es to video memory
in al, 0x60 ; read a char from keyboard port
cmp al, 0x2a ; is the key left shift
jne nextcmp ; no, try next comparison
mov byte [es:0], 'L' ; yes, print L at top left
jmp nomatch ; leave interrupt routine
nextcmp: cmp al, 0x36 ; is the key right shift
jne nomatch ; no, leave interrupt routine
mov byte [es:0], 'R' ; yes, print R at top left
nomatch: mov al, 0x20 out 0x20, al ; send EOI to PIC
start: xor ax, ax
mov es, ax ; point es to IVT base
cli ; disable interrupts
mov word [es:9*4], kbisr ; store offset at n*4
mov [es:9*4+2], cs ; store segment at n*4+2
sti ; enable interrupts
l1: mov ah, 0 ; service 0 – get keystroke
int 0x16 ; call BIOS keyboard service
cmp al, 27 ; is the Esc key pressed
jne l1 ; if no, check for next key
mov ax, 0x4c00 ; terminate program
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 10

Problem Statement

Write an assembly language program to make asterisks travel the border of the screen, from
upper left to upper right to lower right to lower left and back to upper left indefinitely, making
each movement after one second.

Solution
; program to move astericks
[org 0x0100]
jmp start
seconds: dw 0
timerflag: dw 0
oldkb: dd 0
; keyboard interrupt service routine
kbisr: push ax
in al, 0x60 ; read char from keyboard port
cmp al, 0x2a ; has the left shift pressed
jne nextcmp ; no, try next comparison
cmp word [cs:timerflag], 1; is the flag already set
je exit ; yes, leave the ISR
mov word [cs:timerflag], 1; set flag to start printing
jmp exit ; leave the ISR
nextcmp: cmp al, 0xaa ; has the left shift released
jne nomatch ; no, chain to old ISR
mov word [cs:timerflag], 0; reset flag to stop printing
jmp exit ; leave the interrupt routine
nomatch: pop ax
jmp far [cs:oldkb] ; call original ISR
exit: mov al, 0x20
out 0x20, al ; send EOI to PIC
pop ax
iret ; return from interrupt
; timer interrupt service routine
timer: push ax
cmp word [cs:timerflag], 1 ; is the printing flag setjne skipall ; no, leave the ISR
inc word [cs:seconds] ; increment tick count
push word [cs:seconds]
call printnum ; print tick count
skipall: mov al, 0x20
out 0x20, al ; send EOI to PIC
pop ax
iret ; return from interrupt
start: xor ax, ax
mov es, ax ; point es to IVT base
mov ax, [es:9*4]
mov [oldkb], ax ; save offset of old routine
mov ax, [es:9*4+2]
mov [oldkb+2], ax ; save segment of old routine
cli ; disable interrupts
mov word [es:9*4], kbisr ; store offset at n*4
mov [es:9*4+2], cs ; store segment at n*4+2
mov word [es:8*4], timer ; store offset at n*4
mov [es:8*4+2], cs ; store segment at n*4+
sti ; enable interrupts
mov dx, start ; end of resident portion
add dx, 15 ; round up to next para
mov cl, 4
shr dx, cl ; number of paras
mov ax, 0x3100 ; terminate and stay resident
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 11

Problem Statement

Write an assembly language program to underline each character of the screen font using
BIOS video services.

Solution
; put underlines on screen font
[org 0x0100]
jmp start
font: times 256*16 db 0 ; space for font
start: mov ax, 0x1130 ; service 11/30 – get font info
mov bx, 0x0600 ; ROM 8x16 font
int 0x10 ; bios video services
mov si, bp ; point si to rom font data
mov di, font ; point di to space for font
mov cx, 256*16 ; font size
push ds
push es
pop ds ; ds:si to rom font data
pop es ; es:di to space for font
cld ; auto increment mode
rep movsb ; copy font
push cs
pop ds ; restore ds to data segment
mov si, font-1 ; point si before first char
mov cx, 0x100 ; total 256 characters
change: add si, 16 ; one character has 16 bytes
mov byte [si], 0xFF ; change last line to all ones
loop change ; repeat for each character
mov bp, font ; es:bp points to new font
mov bx, 0x1000 ; bytes per char & block number
mov cx, 0x100 ; number of characters to change
xor dx, dx ; first character to change
mov ax, 0x1110 ; service 11/10 – load user font
int 0x10 ; bios video services
mov ax, 0x4c00 ; terminate program
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 12

Problem Statement

You are required to write a TSR program that will print total number of characters in your
name (including spaces within complete name) on top left corner of screen once you will
press first letter of your name from keyboard.

Solution
[org 0x0100]
jmp start
oldisr: dd 0 ; space for saving old isr
; keyboard interrupt service routine
kbisr: push ax
push es
mov ax, 0xb800
mov es, ax ; point es to video memory
in al, 0x60 ; read a char from keyboard port
cmp al, 0x32 ; check if m has been pressed
jne nomatch
mov byte [es:0], '1' ; yes, print 1 at first column
mov byte [es:2], '8' ; yes, print 8 at second column
;jmp exit ; leave interrupt routine
nomatch: pop es
pop ax
jmp far [cs:oldisr] ; call the original ISR
exit: mov al, 0x20
out 0x20, al ; send EOI to PIC
pop es
pop ax
iret ; return from interrupt
start: xor ax, ax
mov es, ax ; point es to IVT base
mov ax, [es:9*4]
mov [oldisr], ax ; save offset of old routine
mov ax, [es:9*4+2]
mov [oldisr+2], ax ; save segment of old routine
cli ; disable interrupts
mov word [es:9*4], kbisr ; store offset at n*4
mov [es:9*4+2], cs ; store segment at n*4+2sti ; enable interrupts
mov dx, start ; end of resident portion
add dx, 15 ; round up to next para
mov cl, 4
shr dx, cl ; number of paras
mov ax, 0x3100 ; terminate and stay resident
int 0x21

For any queries, feel free to send an email at [email protected] or join the weekly interactive sessions
Computer Architecture and Assembly
Language Programming Practical (CS401P) Lab No. 13

Problem Statement

You are required to write a TSR program that will print characters of your name on new line once
you press ‘p’ from keyboard.

Solution

name db 'YourName', 0
newline db 13, 10, '$'
prevInt9 dw 0
org 0x0100
mov ax, 0
mov ds, ax
; Save the original interrupt vector at INT 9
mov ax, 3509h
int 0x21
mov word [prevInt9], bx
mov word [prevInt9+2], es
; Install our interrupt handler at INT 9
mov ax, SEG keyHandler
mov es, ax
mov bx, OFFSET keyHandler
mov ah, 25h
mov al, 9
int 0x21
; Terminate and Stay Resident (TSR)
mov ah, 31h
int 0x21

keyHandler:
pushf ; Save flags
push ax ; Save AX register
; Check if the 'p' key is pressed (ASCII code 112)
mov ah, 1
int 0x16
cmp al, 112
jne skipPrintName
; Print the name on a new line
mov dx, name
mov ah, 9
int 0x21
; Print newline characters
mov dx, newline
mov ah, 9
int 0x21
skipPrintName:
pop ax ; Restore AX register
popf ; Restore flags
; Call the original interrupt handler
pushf
call dword [prevInt9]
retf
; Define the TSR signature and padding

times 0x100-($-$$) db 0

Further, For any queries feel free to send an email at [email protected] or join the weekly interactive
session

You might also like