Lab 10-1 sol
Lab 10-1 sol
Laboratory Manual
for
Computer Organization and Assembly Language Programming
Page 1
OBJECTIVES:
Understand the use of IN and OUT instructions for direct hardware communication.
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.
1. Write an assembly program to read the status from the PIC control port.
2. Display the result of this read operation on the screen.
Instructions:
[org 0x0100]
jmp start
start:
mov dx, 0x20 ; DX points to PIC control port
in al, dx ; Read byte from port into AL
add al, '0' ; Convert to ASCII
mov ah, 0x0E ; BIOS teletype output
int 0x10 ; Display AL (status byte)
1. Write another program that configures the PIT to generate interrupts at a specific frequency.
2. Configure the PIT by writing to its control register and setting up the interval.
Instructions:
Page 2 of 6
Choose a control word that sets a repeating interval, which can be displayed to observe the timer
configuration.
[org 0x0100]
jmp start
start:
mov dx, 0x43 ; PIT control register
mov al, 0x36 ; Control word to set mode 3 (square wave generator)
out dx, al ; Write to control port
Page 3 of 6
Task 2: Interrupt Chaining and Unhooking an Interrupt
Part 1: Chain a Custom Interrupt
1. Hook INT 08h (the timer interrupt) with a custom handler that increments a counter on each
timer tick.
2. Chain this interrupt to retain the original INT 08h handler so that it executes after your custom
code.
Instructions:
[org 0x0100]
jmp start
start:
cli ; Disable interrupts while hooking
mov ax, 0x3508 ; Get vector for INT 08h
int 0x21
mov [orig_int08], bx ; Store original vector address
mov [orig_int08+2], es
int08_handler:
inc byte [counter] ; Increment counter
push ax
push es
mov ax, [orig_int08+2]
mov es, ax
mov dx, [orig_int08]
jmp [es:dx] ; Chain to original INT 08h handler
Page 2
Part 2: Unhook the Custom Interrupt
1. Write code to unhook your custom handler and restore the original INT 08h vector.
2. Ensure that the program terminates safely and restores the interrupt environment to its original
state.
Instructions:
Use the DOS interrupt to replace the interrupt vector with the original handler.
Verify that the original handler is properly restored by observing that the counter no longer
increments after unhooking.
restore_interrupt:
mov ax, 0x2508 ; Set INT 08h vector
mov dx, [orig_int08]
mov es, [orig_int08+2]
int 0x21
Task 3: Creating and Debugging a Terminate and Stay Resident (TSR) Program with INT
09h.
Instructions:
[org 0x0100]
jmp start
start:
cli ; Disable interrupts while setting handler
Page 5
mov ax, 0x3509 ; Get current vector for INT 09h
int 0x21
mov [orig_int09], bx ; Save original offset
mov [orig_int09+2], es ; Save original segment
int09_handler:
in al, 0x60 ; Read from keyboard port (scan code)
cmp al, 0x1E ; Check if 'A' key (scan code 1E)
jne skip_action
skip_action:
mov ax, [orig_int09+2] ; Restore segment for original handler
mov es, ax
mov dx, [orig_int09] ; Restore offset for original handler
jmp [es:dx] ; Chain to original handler
Page 6