CS270 Colorado State University ========= LC3 ========= ---------------- Addressing Modes Addressing mode indicates where the operand is located. 1. Register 2. Immediate 3. Memory The effective address is encoded in instruction (direct or PC-relative). Pointer to effective address is encoded in instruction (indirect) Pointer to the effective address is in a register. (relative, or base+offset) ---------------- traps Each trap invokes an operating system service. In current LC3sim, just modeling the effect of the TRAP and not what it does in detail. TRAP trap vector Trap vector table Like a virtual method table used to implement Java or C++. Each entry holds the address of the function to service the trap. PC is set to the value at trap vector location. LC3sim currently implements x25 halt program x23 input character from keyboard with prompt and echo, and place the character in the 8 LSB of R0 x21 output character in 8 LSB of R0 to the console display ============ LC3 assembly ============ Ch. 7 ------------------------------------------ Example: Finding the first 1 bit in a Word Input: 0x300A is location in memory of Word to be examined Output: R1 contains bit position of first 1 bit in Word or -1 if no 1 bit (Similar to example 4 in Chapter 6) ------ C code Assume variable w is located at memory location x3009 for the C code to "match" the assembly. #include int main() { //short w = 0x0010; //short w = 0x0000; short w = 0x0020; // Put value in R2 "register" short R2 = w; // R1 iterates from MSB position to LSB position int R1; for (R1=15; R1>=0; R1--) { // If MSB is 1, then number is negative if (R2 < 0) { break; } // shift word in question one big to the left R2 = R2 << 1; } printf("R1 = %d\n", R1); } ----------------- LC3 assembly code ----------------- LC3 machine code 0x3000 0x24?? # R2 = Mem(x300A) 0x3001 0x5260 # R1 <- 0 0x3002 0x126F # R1 <- 15 0x3003 0x08?? # if (R1<0) goto 0x3009 0x3004 0x16A0 # R3 <- R2 0x3005 0x08?? # if (R2<0) goto 0x3009 0x3006 0x1482 # R2 <- 2*R2 0x3007 0x127F # R1 <- R1 - 1 0x3008 0x0F?? # goto 0x3003 0x3009 0xF025 # HALT 0x300A 0x0020 ------------ LC3 assembly Makes writing machine code easier. - Enables the use of labels instead of calculating PC offsets. - Converts decimal values to binary. - Pseudo-ops - indicate where data and instructions should be placed in memory ------------ Instructions LABEL OPCODE OPERANDS COMMMENTS ---------- Pseudo Ops ARE NOT translated into machine instructions. .ORIG address Tells assembler where to locate the program in memory (starting address). .FILL value Store value in the next location in memory. .BLKW n Set aside a block of n words in memory. .STRINGZ string Store the string, one character per word, in memory. Add a word of x0000 after the string. .END Marks the end of the source program (not HALT!) .EXTERNAL The label so indicated is allocated in another module and therefore cannot be resolved until link time. ------------------------ mstrout@cs.colostate.edu, 10/6/08