0% found this document useful (0 votes)
9 views5 pages

Lab 14 Sol

The document is a laboratory manual for a Computer Organization and Assembly Language Programming course at the National University of Computer and Emerging Sciences. It outlines objectives related to creating an infinite loop-based animation of falling stars, implementing keyboard-controlled thread creation, and mastering multithreaded column management. The manual includes detailed tasks and code examples for students to develop their programming skills in assembly language during the Fall 2024 semester.

Uploaded by

sana ejaz
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)
9 views5 pages

Lab 14 Sol

The document is a laboratory manual for a Computer Organization and Assembly Language Programming course at the National University of Computer and Emerging Sciences. It outlines objectives related to creating an infinite loop-based animation of falling stars, implementing keyboard-controlled thread creation, and mastering multithreaded column management. The manual includes detailed tasks and code examples for students to develop their programming skills in assembly language during the Fall 2024 semester.

Uploaded by

sana ejaz
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/ 5

National University of Computer and Emerging Sciences

Laboratory Manual

for
Computer Organization and Assembly Language Programming

Lab Instructor Sana Ejaz


Semester Fall 2024

Department of Computer Science

FAST-NU, Lahore, Pakistan

Page 1
OBJECTIVES:

 Understand Infinite Loop-Based Animation. Learn to implement an animated effect where a symbol
(star) moves continuously within a specific column of the console, simulating a falling motion

 Implement Keyboard-Controlled Thread Creation: Develop the ability to dynamically start new threads
for falling stars using keyboard input, with each thread targeting a different column.

 Master Multithreaded Column Management: Understand how to schedule and manage multiple
threads, each working independently to display animations in different columns, using timer-based
multitasking.

Task 1: Write a function fallingStar that takes column number as parameter and prints a
star moving in that column.

For example, if colNo is 80, your function will print a star in column 80, falling from row 0 to
row 24 (with some delay). After reaching row 24, it will again appear on 1st row and start falling
again, in an infinite loop.

fallingStar:
push bp
mov bp, sp

sub sp, 2 ; Reserve space for row variable


mov word [bp-2], 0 ; Initialize row to 0
mov si, [bp+4] ; Column number is passed as a parameter

fallingLoop:
push ax
push bx
push cx
push dx

mov ax, 0xb800 ; Video memory segment


mov es, ax

; Clear the previous row


mov bx, [bp-2] ; Load current row
dec bx ; Previous row
cmp bx, -1 ; Skip clearing if no previous row
jl skipClear
mov dx, bx

Page 2
shl dx, 7 ; Multiply by 160 to get row offset
add dx, si ; Add column offset
mov byte [es:dx], 0x20 ; Write space to clear star

skipClear:
; Draw star in the current row
mov bx, [bp-2] ; Load current row
mov dx, bx
shl dx, 7 ; Multiply by 160 to get row offset
add dx, si ; Add column offset
mov byte [es:dx], 0x2A ; Write '*' in the current column
mov byte [es:dx+1], 0x07 ; Attribute byte (white text)

; Update row number


inc word [bp-2] ; Move to the next row
cmp word [bp-2], 25 ; Check if it has reached the bottom
jne continueFall
mov word [bp-2], 0 ; Reset to the first row if at bottom

continueFall:
; Delay loop
mov cx, 5000 ; Arbitrary delay count
delayLoop:
loop delayLoop

pop dx
pop cx
pop bx
pop ax
jmp fallingLoop ; Infinite loop

mov sp, bp
pop bp
ret

Task 2: Write a program that starts a new thread of falling star if the user presses key ‘8’.

Each thread will start with a difference of 5 columns i.e.

1st thread: star falling on column 80 (row 0 to 24 in infinite loop)


2nd thread: star falling on column 75 (row 0 to 24 in infinite loop)
3rd thread: start falling on column 70 and so on.

[org 0x0100]

Page 3
jmp start

; PCB layout: ax,bx,cx,dx,si,di,bp,sp,ip,cs,ds,ss,es,flags,next


; PCB: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28
pcb: times 32*16 dw 0 ; Space for 32 PCBs
stack: times 32*256 dw 0 ; Space for 32 512-byte stacks
nextpcb: dw 1 ; Index of next free PCB
current: dw 0 ; Index of current PCB
nextcol: dw 80 ; Next column number for new thread

; Subroutine to register a new thread


; Takes the segment, offset of the thread routine, and a parameter
initpcb: push bp
mov bp, sp
push ax
push bx
push cx
push si

mov bx, [nextpcb] ; Read next available PCB index


cmp bx, 32 ; Are all PCBs used?
je exit ; Yes, exit

mov ax, [bp+8] ; Segment parameter


mov [pcb+bx*16+18], ax ; Save in PCB space for CS
mov ax, [bp+6] ; Offset parameter
mov [pcb+bx*16+16], ax ; Save in PCB space for IP

mov [pcb+bx*16+22], ds ; Set stack segment


mov si, [nextpcb] ; Read this PCB index
shl si, 9 ; Multiply by 512
add si, stack+512 ; Point to the end of stack
mov ax, [bp+4] ; Parameter for subroutine
sub si, 2 ; Decrement stack pointer
mov [si], ax ; Push parameter on stack
sub si, 2 ; Space for return address
mov [pcb+bx*16+14], si ; Save stack pointer in PCB

mov word [pcb+bx*16+26], 0x0200 ; Initialize thread flags


mov ax, [pcb+28] ; Read next of 0th thread
mov [pcb+bx*16+28], ax ; Set as next thread
mov ax, [nextpcb] ; Read new thread index
mov [pcb+28], ax ; Set as next of 0th thread
inc word [nextpcb] ; Update free PCB index

exit: pop si

Page 4
pop cx
pop bx
pop ax
pop bp
ret 6

; Timer interrupt service routine


timer: ; (Similar to Task 1's timer handler for multitasking)
; Save registers, cycle to the next PCB, load the context

; Main program
start:
; Hook timer interrupt
xor ax, ax
mov es, ax
cli
mov word [es:8*4], timer
mov [es:8*4+2], cs
sti

; Set up keypress handler


nextkey:
xor ah, ah
int 0x16 ; BIOS keyboard service
cmp al, '8' ; Check for key '8'
jne nextkey

push cs ; Use current segment


mov ax, fallingStar ; Offset of fallingStar
push ax
push word [nextcol] ; Column number
call initpcb ; Register new thread

sub word [nextcol], 5 ; Decrease column for the next thread


jmp nextkey ; Wait for the next keypress

Page 5

You might also like