160 likes | 302 Views
Overview. Review Trap Instruction Program in LC-3 machine language Use LC-3 Simulator. TRAP. Calls a service routine , identified by 8-bit “trap vector.” When routine is done, PC is set to the instruction following TRAP. LC-3 Editor / Simulator.
E N D
Overview • Review Trap Instruction • Program in LC-3 machine language • Use LC-3 Simulator
TRAP • Calls a service routine, identified by 8-bit “trap vector.” • When routine is done, PC is set to the instruction following TRAP.
LC-3 Editor / Simulator • Go to Authors Web page (http://www.mhhe.com/patt2 ) • Download LC-3 (LC301.exe) • LC-3 Edit • LC-3 Simulate • Review “Guide to Using the Windows Version of the LC-3 Simulator and LC-3 Edit”
Example Opcode
clear R2 add R4 to R2 decrement R5 R5 = 0? No Yes HALT Example 1: Multiply • This program multiplies two unsigned integers in R4 and R5. Write program
Program to multiply [R4] x [R5] and place result in R2 • x3200 Clear Accumulator (R2) R2 <- 0 • x3201 Add [R4] to Accumulator R2 <- R2 + R4 • x3202 Dec R5 R5 <- R5 – 1 • x3203 Do again if [R5] > 0 BR p x3201 • x3204 Stop HALT
Program to multiply [R4] x [R5] and place result in R2 Write program
Program to multiply [R4] x [R5] and place result in R2 • x3200 Clear Accumulator (R2) R2 <- 0 0101 010 010 1 00000 54A0 • x3201 Add [R4] to Accumulator R2 <- R2 + R4 0001 010 010 0 00 100 1484 • x3202 Dec R5 R5 <- R5 – 1 0001 101 101 1 11111 1B7F • x3203 Do again if [R5] > 0 BR p x3201 0000 011 111111101 07FD • x3204 Stop HALT 1111 0000 00100101 F025 Test on Simulator
Example 2 • Compute sum of 12 integers.Numbers start at location x3100. Program starts at location x3000. R1 x3100R3 0 (Sum)R2 12(count) R2=0? R4 M[R1]R3 R3+R4R1 R1+1 R2 R2-1 NO YES Write program
Sample program 4 • Write a program to place the absolute value of the [R2] in R2
Program to display absolute value of number in R2 (-99 >= [R2] >= 99) Write program
Program to display absolute value of number in R2 (-99 >= [R2] >= 99) • Find absolute value • ADD R2, R2, #0 ; set cc 14A0 • BRzp +2 0602 • NOT R2, R2 94BF • ADD R2, R2, #1 14A1 • R5 will contain x30 (#48), the offset for ASCII numbers • AND R5, R5, #0 5B60 • ADD R5, R5, #15 1B6F • ADD R5, R5, #15 1B6F • ADD R5, R5, #15 1B6F • ADD R5, R5, #3 1B63 • Check for [R2] >9 • AND R4, R4, #0 ; init high digit holder to zero 5920 • ADD R2, R2, #-10 14B6 • BRn +2 0802 • ADD R4, R4, #1 1921 • BR -4 0FFC • ADD R3, R2, #10 ; save low digit 16AA • If high digit = 0, don't display it • ADD R4, R4, #0 ; set CC 1920 • BRz +2 0402 • Display high digit • ADD R0, R4, R5 1105 • TRAP x21 F021 • Display low digit • Add R0, R3, R5 10C5 • TRAP x21 F021 • Halt • TRAP x25 F025