0% found this document useful (0 votes)
12 views

Lab 10-1 sol

The document is a laboratory manual for Computer Organization and Assembly Language Programming at the National University of Computer and Emerging Sciences. It outlines objectives and tasks related to hardware communication, interrupt handling, and creating Terminate and Stay Resident (TSR) programs. Each task includes detailed instructions and assembly code examples for practical implementation in a lab setting.

Uploaded by

sana ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lab 10-1 sol

The document is a laboratory manual for Computer Organization and Assembly Language Programming at the National University of Computer and Emerging Sciences. It outlines objectives and tasks related to hardware communication, interrupt handling, and creating Terminate and Stay Resident (TSR) programs. Each task includes detailed instructions and assembly code examples for practical implementation in a lab setting.

Uploaded by

sana ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 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.

Task 1: Basic Hardware Communication with IN and OUT Instructions

Part 1: Read from a Port

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:

 Use IN instruction with the correct PIC port address.


 Use INT 21h (DOS interrupt) to display the value read from the port.

[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)

mov ax, 0x4C00 ; Exit to DOS


int 0x21

Part 2: Write to a Port

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:

 Use OUT instruction to send data to the PIT control register.

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

mov dx, 0x40 ; Channel 0 data port of PIT


mov al, 0xFF ; Low byte of frequency divisor
out dx, al
mov al, 0xFF ; High byte of frequency divisor
out dx, al

mov ax, 0x4C00 ; Exit to DOS


int 0x21

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:

 Save the original interrupt vector (orig_int08) for INT 08h.


 Replace INT 08h with your custom handler address, which increments a counter and then calls
the original INT 08h handler.

[org 0x0100]
jmp start

counter db 0 ; Initialize counter

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

mov ax, 0x2508 ; Set new handler for INT 08h


mov dx, int08_handler
int 0x21
sti ; Re-enable interrupts

; Infinite loop to keep the program running


hang: jmp hang

orig_int08 dw 0, 0 ; Placeholder for original INT 08h vector

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

mov ax, 0x4C00 ; Exit to DOS


int 0x21

Task 3: Creating and Debugging a Terminate and Stay Resident (TSR) Program with INT
09h.

1. Set up a TSR that hooks the keyboard interrupt.


2. Add a breakpoint within the handler to facilitate debugging and analysis.
3. Track specific key presses and observe program behavior with the breakpoint interrupt (INT 3).

Instructions:

1. Write a custom handler for INT 09h to monitor key presses.


2. Set the TSR to stay in memory and keep your handler active.
3. Use INT 3 within the handler to halt execution on specific key presses (e.g., 'A' key) for
debugging.
4. Observe and document any changes in register values when execution halts.

[org 0x0100]
jmp start

orig_int09 dw 0, 0 ; Placeholder for original INT 09h vector


counter db 0 ; Counter for specific key press tracking

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

mov ax, 0x2509 ; Set custom handler for INT 09h


mov dx, int09_handler
int 0x21
sti ; Re-enable interrupts

; TSR to keep handler in memory


mov ah, 0x31 ; Function to stay resident
mov dx, 0 ; Keep only the interrupt handler
int 0x21

int09_handler:
in al, 0x60 ; Read from keyboard port (scan code)
cmp al, 0x1E ; Check if 'A' key (scan code 1E)
jne skip_action

int 3 ; Debug breakpoint interrupt

; Action when 'A' key is pressed


inc byte [counter] ; Increment counter for demonstration

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

You might also like