CS/ECE 252 Midterm 4 Review =========================== Author: Ryan Johnson Date: 2009.12.09 Assembly Language ----------------- * Know the LC-3 Instructions * What does LDI do? STI? JSR? Appendix A of the textbook is a goldmine of information. * What's the range of values you can fit in the imm5 fields? PCoffset9 fields? (remember 2's complement?) * Labels * A way to symbolically name memory locations. Can be used in place of offset values. Ex: .orig x3000 AGAIN AND R0, R0, #0 BRnzp AGAIN .end AGAIN is the symbolic name for memory location x3000. .orig x3000 LD R0, NUMBER NUMBER .FILL xABCD .end NUMBER is the symbolic name for memory location x3001. * Pseudo Ops * .orig - marks memory location of first instruction * .fill VALUE - fills memory location with VALUE * .blkw NUMBER - initializes NUMBER sequential blocks * .stringz "STRING"- sequence of memory locations with ascii characters in STRING PLUS one extra location for a NULL (x0000) sentinel character * .end - marks the end of the source code Page 182 of textbook for more info Two Step Assembly Process ------------------------- * First Pass: * Checks for errors * Unknown instructions * Numbers out of range * Illegal labels (ex: cannot use HALT as a label) * Creates symbol table. Associates the label with the memory address. Ex: .ORIG x3000 AND R2, R2, #0 LD R0, M0 LD R1, M1 LOOP BRz DONE ADD R2, R2, R0 ADD R1, R1, #-1 BR LOOP DONE ST R2, RESULT HALT RESULT .FILL x0000 M0 .FILL x0004 M1 .FILL x0023 .END * Symbol table is: Symbol Name Page Address ---------------- ------------ M1 300B M0 300A DONE 3007 LOOP 3003 RESULT 3009 * Second pass: * Using symbol table, converts assembly into machine code (binary) and replaces values with memory address/offsets. * The binary output for the code above (comments after ; are not part of the binary): 0011 0000 0000 0000 ;.orig x3000 0101 0100 1010 0000 ;AND R2, R2, #0 0010 0000 0000 1000 ;LD R0, M0 0010 0010 0000 1000 ;LD R1, M1 0000 0100 0000 0011 ;LOOP BRz DONE 0001 0100 1000 0000 ;ADD R2, R2, R0 0001 0010 0111 1111 ;ADD R1, R1, #-1 0000 1111 1111 1100 ;BR LOOP 0011 0100 0000 0001 ;DONE ST R2, RESULT 1111 0000 0010 0101 ;HALT 0000 0000 0000 0000 ;RESULT .FILL x0000 0000 0000 0000 0100 ;M0 .FILL x0004 0000 0000 0010 0011 ;M1 .FILL x0023 * Look at the 3rd line: 0010 0000 0000 1000 In assembly, this is equivalent to: LD R0, #8 Where does #8 come from? This instruction is in memory address x3001 and M0 is in memory location x300A. LD instruction adds offset to incremented PC, so PC = x3002, x3002 + x8 = x300A = M0. Same process used for LOOP, DONE, etc... I/O --- * Various types * Special I/O instructions - instructions defined specifically for I/O functions * Memory mapped - allows to use existing memory access instructions (LD, ST, etc) for I/O * Synchronous - requires I/O to occur in sync with processor clock * Asynchronous - I/O happens independent of processor clock, handshaking must occur * Polling - processor uses execution cycles to check the status of I/O * Interrupt Driven - external signal interrupts the processor's normal execution when I/O is ready * LC-3 I/O registers * DSR - Display status register: display ready for new data or not * DDR - Display data register: register that accepts data to be written to display * KBSR - Keyboard status register: keyboard contains new data or not * KBDR - Keyboard data register: contains the input data from the keyboard TRAP ---- * TRAP (Opcode 1111) allows for program to access privileged (restricted) functionality * Memory locations can be marked as protected, only privileged functions can access them * If you need to access protected memory, must use a special call (TRAP) * Some useful LC-3 TRAP functions, can be used in assembly code in place of writing TRAP instruction * HALT (TRAP x25: 1111 0000 0010 0101) - stops the execution of the LC-3 * GETC (TRAP x20: 1111 0000 0010 0000) - reads character from keyboard into R0 * OUT (TRAP x21: 1111 0000 0010 0001) - outputs a single character stored in R0 * PUTS (TRAP x22: 1111 0000 0010 0000) - outputs a string pointed to by R0 See Table A.2 in textbook. Subroutines ----------- * Allows for reuse of code * JSR: store the current PC (remember it is incremented!) in R7, change PC to first line of subroutine code (see also JSRR) * RET (equivalent to JMP R7): loads PC with the value stored in R7 -> returns to executing code immediately after JSR * Saving registers - subroutine (or TRAP) may change the value of a register, need to make sure to save any important data to memory (especially R7 inside a subroutine!) * Caller-saved: code that calls the JSR instruction stores register values in memory for safekeeping * Callee-saved: subroutine code stores register values in memory for safekeeping Halting Problem --------------- * Theory is a big part of the SCIENCE in computer science * Alan Turing proved that the Halting Problem is UNDECIDABLE (i.e. impossible to create a general algorithm for all cases) * Halting Problem: can we create a program that can detect if some arbitrary program with some arbitrary input will halt or not? Answer is NO! * Proof by contradiction - don't worry about having to prove it for the midterm * Take Away points: * Cannot write a general solution for the halting problem * Some problems are undecidable no matter how much time or computer power we throw at them