CS 2110 - Part 2
logical not
!
variants of #include
#include <filename>: tells the preprocessor that the header file can be found in a predefined system directory #include "filename" : tells the preprocessor that the header file can be found in the same directory as the C source file.
bitwise AND
&
logical and
&&
2 types of qualifiers
- const: tells the compiler to put the variable in an area of memory where it can't be modified - volatile
Software Stack
- stack has sequential memory locations with a mechanism called a stack pointer. - stack pointer keeps track of the top of the stack - values inserted into a stack are stored in mem. location with decreasing addresses. - values on the stack aren't deleted after they are popped. - data stored on a stack doesn't physically move
.EXTERNAL and JSRR
.external lets you identify the label that comes after it as the symbolic name of an address that isn't known during assembly. assembler creates a symbol table entry for that label -> it marks that symbol as belonging to another module. at link time, the linker uses the symbol table entry for that label to complete the translation. JSRR: can be used on a register containing the label's value you find through .external ex. .ORIG x3000 LD R3, SUBADDR JSRR R3 HALT .EXTERNAL SUBTRACT SUBADDR .FILL SUBTRACT
Part 2: Handling the interrupt request
1. Intiate the interrupt 2. Service the interrupt (PC contains the starting address of the interrupt service routine, the routine executes). 3. Return from the interrupt (last instruction from every interrupt is the RTI).
Elements of a TRAP Mechanism
1. a set of service routines executed on behalf of user programs by the operating system 2. a table of the starting addresses of these 256 service routines; this table is in mem location x0000 to x00FF (TRAP vector table) 3. the TRAP instruction - what a user uses when they want to execute a specific service routine. 4. linkage back to the program
What happens when an interrupt comes in
1. force the running program to stop 2. have the processor execute a program that carries out the needs of the IO device 3. have the stopped program resume like nothing happened
TRAP instruction
1. if TRAP was called in user mode, (PSR[15] = 1), then switch from user stack to supervisor stack a. save r6 into saved_usp b. load saved_ssp into r6 2. push PC onto supervisor stack (set up return linkage) 3. push PSR onto supervisor stack 4. set PSR[15] to 0 to ensure we're in supervisor mode; but keep PSR[10:8] (the privilege) the same 5. zero extend trap vector to 16 bits, then save the value at the resulting address into PC. (PC <-- Mem[ZEXT(trapvec8)])
Steps of an interrupt
1. if we're in user mode, then switch to supervisor mode and switch the stack pointers 2. push PC onto supervisor stack 3. push PSR onto supervisor stack 4. set PSR[10:8] to the priority level that you're servicing. 5. set PSR[2:0] to something random (changing the CC) 6. prepend the interrupt vector with x01. then set the PC to that memory location PC <-- mem[x0100 + zext(interruptvex8)]
.STRINGZ
1. initialize a sequence of n+1 memory locations 2. first n words are the characters of the string in ASCII 3. last word is 0 (x0000)
entry of symbol table
1. its identifier 2. its type 3. the place in memory the variable has been allocated storage 4. the scope of the variable
RTI
1. pop old value of PSR and load it back into PSR 2. pop old value of PC off of the supervisor stack and loack it back into the PC 3. if PSR[15] ==1, switch back to user stack a. save R6 into saved_ssp b. load saved_usp into R6
How to initiate an interrupt
1. save the state of the interrupted program so you can pick back up (PC and PSR) 2. load the state of the higher priority interrupting program
PART 1: Causing the Interrupt to Occur
1. the I/O device must want the service - KBSR/DSR ready bit is set to 1 2. the device must have the right to request that service (the interrupt enable bit is set): this is bit 14 of of KBSR and DSR 3. the device request must be more urgent (PL) than what the prcoessor is currently doing.
LC 3 idiom for PUSH
ADD R6, R6, -1 STR R0, R6, 0
Stack
Abstract data type (meaning that it's defined by operations performed on it not by the specific way that it's implemented). Last thing you store in a stack is the first thing that you remove from it - LIFO
First pass
Assembler examines each instruction in each sequence and increments location counter once for each assembly language instruction. If instruction has a label, a symbol entry is made for that label. Symbol entry: symbol and adress (which is the current contents of the location counter) First pass ends when it reaches .END.
JSRR
Base register addressing 1. loads the PC, overwritting the incremented PC PC <- BaseR 2. save the return address in R7 (return address is the incremented PC and is the address of the instructuion after the JSR(R) instruction).
More than one obj file
Common to make an executable image from more than one object file. .external lets you reference from one module to another.
PSR
Contains privilege and priority for that program. PSR[15] = privilege (0 = supervisor/privileged; 1 = user/unprivileged) PSR[10:8] = specifies the priority level (PL) (ranges from PL 7 = highest to PL 0 = lowest) Also contained the current condition code.
What happens when a character is typed
Each time a character is typed, the ready bit is set to 1 and each time the computer reads that character, the ready bit is cleared.
Second pass
Goes through all the lines again with the help of the symbol table, each line is translated into LC 3 machine language instruction.
interrupt driven IO
I/O device controls the interaction, interrupts execution of processor
Where the interrupt vector table is and how it's used
In memory location x0100 to x01FF, each containing the starting address of an interrupt service routine. each location is the starting address of an interrupt service routine.
KBDR
Keyboard data register: bit[7:0] is used for data, bit[15:8] is x00.
What happens when you display a character
LC3 transfers an ASCII code to DDR[7:0] for outputting -> DSR[15] gets cleared -> char is displayed -> DSR[15] is set (this signals to processor that it can transfer another ASCII code to DDR for outputting).
LC 3 idiom for POP
LDR R0, R6, 0 ADD R6, R6, 1
Motivation for C
Many high level languages: 1. help manage values upon which we are computing 2. provide a human-friendly way to express computation 3. provide an abstraction of the underlying hardware 4. enhance maintainability 5. provide safeguards against bugs
Two important registers associated with each program
PC and PSR
JSR
PC relative addressing 1. loads the PC, overwritting the incremented PC PC <- PC + sext(PC offset 11) 2. save the return address in R7 (return address is the incremented PC and is the address of the instruction after the JSR(R) instruction).
stack pointer
R6 contains the address of the top of the run-time stack.
Recursion
Subroutine needs to call itself. You need to use a stack with recursion - treat a recursive subroutine as an ordinary subroutine which calls another subroutine.
x0000 to x2FFF
System space : privileged memory location. contains various data structures and code of the operating system and need supervisor privilege to access.
Address of hardware registers
These are part of the privileged memory address space and accessible only to programs that have superior privilege (because if programmers mess them up then it'll mess them up for other program too)
Executable Image
This is what is getting executed when you are executing the program.
Compiler
Transforms preprocessed program into an object module two main phases: analysis and synthesis
x3000 to xFDFF
User Space : unprivileged memory locations that don't need supervisor privilege to access them
Linker
Uses the symbol table to fill in blanks in object files, can also link in our trap vectors.
bitwise XOR
^
int
allocated storage for one integer's worth of data
Two pass process
assembly is done in two complete passes from beginning to .END
Interrupt enable bit
bit 14 of KBSR and DSR and - this is set and cleared by the processor
KBSR
bit[15] contains the synchronization mechanism
Passing arguments from caller to callee
callee expects the arguments to be in specific registers, caller has to place arguments in those registers before executing the call instructions.
Caller saved
caller can also save all the registers before JSR and then restore. caller knows the damage done by its instructions - it knows every instance of JSR will destroy what's in R7 so before JSR R7 is stored and then restored once subroutine in completed.
global variable
can be accessed anywhere throughout the program.
supervisor stack
controlled by the operating system and needs supervisor privilege to access
user stack
controlled by user program and doesn't need privilege
Loader
copies our executable image into memory starting at x3000
local variable
declared within a block, a block is a subsection of a program that is contained withing a {}
declaration
declares the variable, gives it a type and identifier
DDR
display data register, drives the monitor display xFE06: bit[7:0] is the data bits[15:8] = x00
DSR
display status register xFE04: bit[15] = sync mechanism. DSR syncs the fast processor and the slow monitor display.
double
double precision floating point number: 1 bit sign
Synchronous
fixed speed of transferring data that both the system and the device know and follow -> transfer rate is tied to the processor clock speed
Synchronization
flag is the simplest form of this - a single flag (ready bit) is enough to synchronize the output to the input of the processor.
TRAPS
functions that the LC-3 handles for us, mostly deal with I/O.
Regions of mem. in C
global data section: global variables runtime stack: local variables
Priority's imporatance in interrupts
higher PL is compared to the PL of the current program, if highest PL is greater then the processor is interrupted right before the next FETCH phase; otherwise no interrupt will happen.
Privileged
if a program is executing in supervisor mode, a program in supervisor mode can execute all instructions and access all of memory.
Unprivileged
if a program is executing in user mode
intialization
initializes the variable, give it a value
4 basic data types
int, char, double and bool
Interpretation
interpreter reads in the high level language program and performs the operations indicated by the programmer - high level language program is a set of commands for the interpreter to program interepreter reads in the commands and carries them out as defined by the language.
Target Code Synthesis
machine code version of program is generated
Pseudo Op
message from assembly language programmer to assembler to help the assembler in the assembly process. it gets discarded once the assembler handles the message.
API Document
must include: subroutine's name, subroutine behavior, expected arguments, expected results/value ex. Name: max Behavior: returns the max of two numbers Expected arguments: R0: A; R1: B Expected result/value: max of A and B stored in R0 on return.
Input from the Keyboard
need: 1. data register that contains the char to be input 2. synchronization mechanism to let processor know input has occured
Flag
one bit status register and a way through which one sytem can communicate that data is ready to another system keyboard: someone has typed a character monitor: if the most recent character sent to the monitor has been displayed
Preprocessor
preprocesses the source program before handing it off to the compiler scans through the source files looking for and acting on preprocessor directives. 4 main functions: 1. inclusion of header files #include 2. macro expansion #define 3. conditional compilation 4. line control
Polling
processor control the interaction, ready bit is polled by the processor
Source Code Analysis
program is broken down into its constituent parts
Caller
program that contains the call
Callee
program the has the return instruction
stack frame
region of contiguous memory that contain all of the local variables for a given function. when a function starts to execute, its stack frame is added to the runtime stack.
What happens when you hit a key
set KBSR[15] to 1 -> ASCII of key is loaded into KBDR[7:0] -> LC3 reads KBDR -> KBSR is cleared
4 types of modifiers
short, long, signed, unsigned
RET
special case of jump, where you jump to whatever is in R7.
Object file
stores memory layout and binary encoded instructions. ex. x3000 -> starting address 2 -> number of instructions x 1020 -> hex instruction xF025
Callee saved
subroutine saves values put in the registers by the calling program, before the subroutine returns to the calling program those values are restored. callee stores and restores registers that it knows it needs to perform its job.
SSP
supervisor stack pointer
Linker
takes over after the compiler, links together all object modules to form an executable image of the program. also incorporates external library code in the form of pre-built object modules.
unsigned
tells compiler to use unsigned binary value
long
tells compiler you want more room (32 or 64 bits)
signed
tells compiler/prprocessor that you may have a sign
.BLKW
tells the asembler to set aside some number of sequential memory location in the program.
.END
tells the assembler it has reached the end of the programs; anything that comes after END will not be processed by the assembler.
.FILL
tells the assembler to set aside the next location in the programs and initialize it with the value of the operand
.ORIG
tells the assembler where in memory to place the LC-3 program.
short
tells the compiler to allocate a small amount of memory (16 bits)
Asynchronous
the clock speed of the processor and the speed of data transfer are not linked; typical of keyboard input and monitor output.
Privilege
the right to do somehting - like executing a particular instruction or accessing a particular memory location. every computer program is designated as privileged or unprivileged.
Priority
the urgency of a program to execute, every program has priority specifying its urgency compared to all other programs. Programs with greater urgency interrupt programs with lesser urgency.
Compilation
translator is a compiler, translates program into machine language and executable image is output, this image can execute on the hardware. compiler analyzes the source program as a larger unit to produce a translation into machine language
bool
true/false through 0 or 1 values
JMP
uncondtional jump to address in the baseR.
xFE00 to xFFFF
used to identify registers that take part in IO functions and some spatial registers related to the processor.
USP
user stack pointer
ternary
variable = (boolean expression) ? (if true) : if false;
char
variable's data value is a character
Executable image of the program
version of the program that can be loaded into memory and executed by the underlying hardware
frame pointer
when a function is executing, the highest numbered mem. address of its stack frame will be stored in R5 which is the frame pointer
left shift
x << y
right shift
x >> y
GETC
x20: reads in char from terminal to save into R0 in ASCII format
OUT
x21: prints whatever's in R0 to the terminal.
PUTS
x22: prints a null terminated string which starts at address stored in R0, characters are contianed in consecutive memory locations.
IN
x23: requests input, reads in character from terminal to save into R0 in ASCII.
HALT
x25: halts the execution of the LC 3, prints a message to the console.
Saving registers using memory locations
you can reserve spots in memory that will be used to the values in the registers. ex. SAVER1 .BLKW 1 ST R1, SAVER1 Restoring: LD R1, SAVER1
bitwise OR
|
logical or
||
bitwise not
~