CSC 225 Lab 4

advertisement
CSC 225
Assignment #5
Notes:


NO PARTNERS allowed for this lab. You must write the code on your own.
o However, if your code does not work, you MAY get help debugging your code.
This lab does not work on Linux (Sorry!). You must use the Windows version of the LC3
Simulator.
Objectives:

To learn how to program in the LC-3 assembly language, including:
o Stack
o Interrupts
Resources:

User Program 1
Background:
The operating system of a real computer constantly switches between programs. While one
program is waiting for input from a user, another program can be running doing useful work. In
this lab we will create two user programs. Using interrupts, we will switch between the two
programs. When Program 1 is waiting for user input, Program 2 will run. An interrupt will occur
when the user types a character on the keyboard. This will stop Program 2 and start an interrupt
service routine. The service routine will get the character from the keyboard and return control to
Program 1. Each time Program 1 needs user input, Program 2 will get a chance to run. This is
not how a real operating system works, but is an introduction to the topic.
Description:
This lab uses four programs: user program 1 (UP1), user program 2 (UP2), a trap (trap x26), and an
interrupt service routine.
1. User Program 1 (Start at x3000)
This program is partially written for you. The program simply echoes five characters to the
screen. The program gets a character from the keyboard using trap x26. Trap x26 will put the
input character into R0. The character is echoed to the screen using trap x21. Modify the
program to
a. Load the location of Trap 26 (see below) into memory location x0026 of the trap vector
table.
b. Load the location of the interrupt service routine (see below) into location x0180 of the
interrupt vector table. x0180 is the location of the keyboard interrupt vector.
c. Initialize the stack pointer. The stack pointer is stored in R6. Set the value of R6 to
x3000. This is where we will start our stack. (Why won't pushing data onto the stack
overwrite the user code in UP1?)
2. User Program 2 (Start at x3400)
This program displays asterisks to the screen in an endless loop. Create a loop that adds one to a
register each time through the loop. When the register becomes negative, the program prints an
asterisk to the screen. Then the register is reset to zero and the loop is started again. You may use
trap x21 to output the character to the screen.
3. Trap x26 (Start at x3300)
The trap has three purposes:
a. Save the PC from UP1 into a hard-coded memory location (x32FF). (Where is the PC from
UP1?)
b. Turn on the interrupt enable bit in the KBSR
c. JMP to UP2.
4. Interrupt Service Routine (Start at x3500)
This routine services the interrupt for the keyboard device. It has three tasks:
a. Get the character from the KBDR. Do not use a trap to do this. Do you have to poll the
KBSR? Why/why not?
b. Clear the KBSR (ready bit and interrupt enable bit).
c. Use RTI to return to UP1. The PC of UP1 needs to be written over the PC of UP2 on the
stack. The PC of UP2 will be on the current top of the stack. You MUST use the stack
pointer for this operation.
Note: The hardware will automatically write the PC and PSR of UP2 onto the supervisor
stack. You must overwrite the PC of UP2 with the PC of UP1 on the stack so the RTI returns to
the correct place. So, we are manually modifying the supervisor stack for the purpose of this lab.
The simulator does not give us access to the PSR, so we must live with UP1 getting the PSR of
UP2. It won't make a difference in this lab.
Running the Program
1.
2.
3.
4.
Load all programs into the LC3 simulator.
Start the PC at x3000 (the location of UP1).
Type 5 letters into the console. The output of UP2 should be interleaved with the output of UP1.
After 5 letters, the program should print “Success! Running again…” and run again.
Debugging
Your program didn’t work, did it? Use break points and step through your code carefully. Try the
following:
1. Set a breakpoint after the modifications that you made to UP1. Check that:
a. The trap vector table entry at x0026 is correct
b. The ISR table entry at x0180 is correct
c. The stack pointer (R6) has been set to x3000
2. Set a breakpoint in the Trap 26 at the JSR to UP2. Check that:
a. Interrupts are enabled
b. The PC from UP1 has been stored at x32FF
3. Set a breakpoint at the RTI of the ISR. Check that:
a. The breakpoint is encountered when a character is pressed while in UP2.
b. R0 contains the ASCII value of the character that you pressed while in UP2.
c. The PC from UP1 that was saved to x32FF has been written to the top of the stack
(at location 2FFE).
Demo
You must demo your program to your instructor.
Handin
LC-3 assembly language source code for UP1, UP2, trap x26, and the interrupt service routine.
Handin by the end of lab on the day it is due. You may handin early and then simply re-handin if
you need to make changes to your code. You must handin to your section. Replace the XX with
your section number. Handin will CLOSE at the end of lab.
handin grader-ph 225-XX-asgn5 UP1.asm UP2.asm trap26.asm isr.asm
Deliverable
Cover page with Demo Signature
Download