0% found this document useful (0 votes)
6 views8 pages

Lab 11 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 such as manipulating PIC ports, experimenting with interrupts, and creating programs for asterisk movement on the screen using timer interrupts and keyboard controls. Additionally, it includes tasks for enabling user control over the animation and ensuring background functionality without disrupting other applications.

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)
6 views8 pages

Lab 11 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 such as manipulating PIC ports, experimenting with interrupts, and creating programs for asterisk movement on the screen using timer interrupts and keyboard controls. Additionally, it includes tasks for enabling user control over the animation and ensuring background functionality without disrupting other applications.

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/ 8

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:

 Learn to manipulate and handle Programmable Interrupt Controller (PIC) ports.


 Experiment with interrupt chaining and unhooking interrupts for custom handling.
 Explore the basics of the Programmable Interval Timer (PIT) and its integration with
interrupts.
 Gain insight into terminating and staying resident (TSR) programs and their applications.

Task 1: Timer-Controlled Asterisk Movement

Write a program to make an asterisk 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. You are not allowed to write any loop in your program. The Timer will move the
star by one cell only. Do not lose the previous content of the cell.

Instructions:

1. Hook the timer interrupt (INT 08h) to control the asterisk’s movement.
2. Set up a delay of approximately one second using the timer’s count.
3. Use a buffer to save the content of the screen cell before placing the asterisk, restoring it
after the asterisk moves to the next cell.
4. Avoid any explicit loop in your code to manage the movement.

[org 0x100]
jmp start

old_timer_isr: dd 0 ; Space for saving the old timer ISR address


asterisk_pos: dw 0 ; Position of the asterisk on the screen
tick_count: dw 0 ; Counter to control movement speed

; Timer interrupt service routine (ISR) to move the asterisk


timer_isr:
push ax
push bx
push es
push di

inc word [tick_count] ; Increment tick count


cmp word [tick_count], 20 ; Move every 20 ticks (adjust for speed)
jl skip_movement ; Skip movement if not yet time

mov word [tick_count], 0 ; Reset tick count

Page 2
; Calculate the next position for the asterisk along the screen border
call move_asterisk

skip_movement:
; Send End of Interrupt (EOI) to the PIC
mov al, 0x20
out 0x20, al

pop di
pop es
pop bx
pop ax

jmp far [cs:old_timer_isr] ; Chain to the original ISR

; Subroutine to move the asterisk around the screen border


move_asterisk:
; Set up screen memory segment
mov ax, 0xb800
mov es, ax ; Point ES to video memory

; Restore character at the previous position


mov di, [asterisk_pos]
mov byte [es:di], ' ' ; Clear previous position
mov byte [es:di + 1], 0x07 ; Restore white text on black background

; Update position to move along the screen border


call update_position

; Place asterisk at the new position


mov di, [asterisk_pos]
mov byte [es:di], '*' ; Set new position
mov byte [es:di + 1], 0x0E ; Yellow on black

ret

; Update the position of the asterisk to move along the border


update_position:
mov di, [asterisk_pos]

; Top row: Left to right


cmp di, 0
jl bottom_row ; If < 0, wrap around to bottom
cmp di, 158 ; Screen width is 80 chars (160 bytes for attribute and char)
jl move_right

Page 3
; Right column: Top to bottom
cmp di, 3840 ; Bottom right corner of the screen
jl move_down

; Bottom row: Right to left


cmp di, 4000
jl move_left

; Left column: Bottom to top


move_up:
sub di, 160 ; Move up one row
cmp di, 160
jge save_position ; Stop at top-left again

mov di, 0 ; Wrap back to starting point


jmp save_position

move_right:
add di, 2 ; Move right one position
jmp save_position

move_down:
add di, 160 ; Move down one row
jmp save_position

move_left:
sub di, 2 ; Move left one position
jmp save_position

bottom_row:
mov di, 4000 ; Move to bottom row left
jmp move_left

save_position:
mov [asterisk_pos], di
ret

start:
; Save the old timer ISR address
mov ax, 0
mov es, ax ; Point ES to IVT base
mov ax, [es:8*4] ; Get old ISR offset
mov word [old_timer_isr], ax
mov ax, [es:8*4 + 2] ; Get old ISR segment
mov word [old_timer_isr + 2], ax

Page 4
; Install our custom timer ISR
cli ; Disable interrupts
mov word [es:8*4], timer_isr ; Set new ISR offset
mov word [es:8*4 + 2], cs ; Set new ISR segment
sti ; Enable interrupts

; Initialize asterisk position to top-left corner


mov word [asterisk_pos], 0

; Wait for the Esc key to terminate the program


wait_for_esc:
mov ah, 0 ; BIOS service: Wait for keystroke
int 0x16
cmp al, 27 ; Check if the key is Esc (ASCII 27)
jne wait_for_esc ; If not, wait again

; Restore the original timer ISR


cli ; Disable interrupts
mov ax, word [old_timer_isr] ; Restore old ISR offset
mov [es:8*4], ax
mov ax, word [old_timer_isr + 2]; Restore old ISR segment
mov [es:8*4 + 2], ax
sti ; Enable interrupts

; Terminate program
mov ax, 0x4C00
int 0x21

Task 2: Adding User Control to Start/Stop the Animation and Keypad Filtering

Update previous program, star should start moving (from its previous position, initial position
will be top left of screen) if user enters Left Shift key and it should stop on Right Shift Key.
Other applications should work fine along with this functionality. Both shifts should work
properly on command prompt. Number keys at the right side of keypad should not work on
command prompt but number keys on top of the keypad should work properly.

Instructions:

1. Hook the keyboard interrupt (INT 09h) to monitor Shift key presses and enable or disable
the animation accordingly.
2. Track the asterisk’s current position so it resumes from the last stopped position.

Page 5
3. Filter inputs to disable the numeric keypad keys in the command prompt but keep the
top-row number keys functional.
4. Test to verify the functionality of both Shift keys, ensuring the asterisk movement
responds accurately and does not affect other command prompt operations.

[org 0x100]
jmp start

old_kb_isr dd 0 ; Space to store original keyboard ISR


animation_on db 0 ; Flag for animation status (on/off)
border_index db 0 ; Position index along the border
saved_char db ' ' ; Saved character at the current position

positions dw 0x0000, 0x4F00, 0x4F1E, 0x000E ; Top-left, top-right, bottom-right, bottom-left

; Timer interrupt handler


timer_isr:
push ax
push bx
push es
cmp byte [animation_on], 0
je skip_animation ; If animation is off, skip movement

mov ax, 0xB800


mov es, ax
mov di, [positions + border_index*2]
mov al, saved_char
stosb

inc byte [border_index]


and byte [border_index], 3
mov di, [positions + border_index*2]
mov al, [es:di]
mov saved_char, al

mov al, '*'


stosb

skip_animation:
mov al, 0x20
out 0x20, al

pop es
pop bx
pop ax
jmp far [cs:old_timer_isr]

Page 6
; Keyboard interrupt handler
keyboard_isr:
push ax
push es
in al, 0x60 ; Read scancode from keyboard port
cmp al, 0x2A ; Left Shift press
je enable_animation ; Start animation
cmp al, 0x36 ; Right Shift press
je disable_animation ; Stop animation

cmp al, 0x4B ; Check for numeric keypad keys (example)


jge filter_key ; Skip if numeric keypad key

jmp original_handler

enable_animation:
mov byte [animation_on], 1
jmp end_kb_isr

disable_animation:
mov byte [animation_on], 0
jmp end_kb_isr

filter_key:
jmp end_kb_isr ; Ignore the key by jumping to end

original_handler:
pop es
pop ax
jmp far [cs:old_kb_isr] ; Chain to original keyboard ISR

end_kb_isr:
mov al, 0x20
out 0x20, al
pop es
pop ax
iret ; Return from interrupt

start:
; Hook timer and keyboard interrupts and stay resident
xor ax, ax
mov es, ax

; Store original ISRs


mov ax, [es:8*4]
mov [old_timer_isr], ax
mov ax, [es:8*4+2]
mov [old_timer_isr+2], ax

Page 7
mov ax, [es:9*4]
mov [old_kb_isr], ax
mov ax, [es:9*4+2]
mov [old_kb_isr+2], ax

; Set new ISRs


cli
mov word [es:8*4], timer_isr
mov word [es:8*4+2], cs
mov word [es:9*4], keyboard_isr
mov word [es:9*4+2], cs
sti

; Stay resident
mov dx, start
add dx, 15
mov cl, 4
shr dx, cl
mov ax, 0x3100
int 0x21

Practice
Task 2: Enable Background Functionality

Update the previous program: Star should keep moving, other applications (e.g. AFD etc) should
work properly.

Instructions:

1. Use a TSR approach to retain the program in memory.


2. Implement a handler that allows the timer interrupt to continue updating the asterisk’s
position without interfering with other running applications.
3. Test the program by opening other applications to ensure the asterisk animation runs
concurrently without disrupting other processes.

Page 8

You might also like