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. vectorroutine x23input a character from the keyboard x21output a character to the monitor x25halt the program
LC-3 Editor / Simulator Go to Authors Web page ( ) 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 AddressInstructionComments x30F R1 PC – 3 = x30F4 x30F R2 R = x3102 x30F M[PC - 5] R2 M[x30F4] x3102 x30F R2 0 x30FA R2 R2 + 5 = 5 x30FB M[R1+14] R2 M[x3102] 5 x30FC R3 M[M[x30F4]] R3 M[x3102] R3 5 Opcode
Example 1: Multiply This program multiplies two unsigned integers in R4 and R5. clear R2 add R4 to R2 decrement R5 R5 = 0? HALT No Yes Write program
x3200 Clear Accumulator (R2)R2 <- 0 x3201 Add [R4] to AccumulatorR2 <- R2 + R4 x3202 Dec R5R5 <- R5 – 1 x3203 Do again if [R5] > 0BR p x3201 x3204 StopHALT 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 < A0 x3201 Add [R4] to AccumulatorR2 <- R2 + R x3202 Dec R5R5 <- R5 – B7F x3203 Do again if [R5] > 0BR p x FD x3204 StopHALT F025 Program to multiply [R4] x [R5] and place result in R2 Test on Simulator
Example 2 Compute sum of 12 integers. Numbers start at location x3100. Program starts at location x3000. R1 x3100 R3 0 (Sum) R2 12(count) R2=0? R4 M[R1] R3 R3+R4 R1 R1+1 R2 R2-1 NO YES Write program
Example 2 – Sum integers form x3100 – x310B AddressInstructionComments x R1 x3100 x R3 0 x R2 0 x R2 12 x If Z, goto x3009 x Load next value to R4 x Add to R3 x Increment R1 (pointer) X Decrement R2 (counter) x Goto x3004
Write a program to place the absolute value of the [R2] in R2 Sample program 4
Example 3 - # Occurrences of Inputted Char
Example 3 - # Occurrences of Inputted Char Program (1 of 2) AddressInstructionComments x R2 0 (counter) x R3 M[x3102] (ptr) x Input to R0 (TRAP x23) x R1 M[R3] x R4 R1 – 4 (EOT) x If Z, goto x300E x R1 NOT R1 x R1 R1 + 1 X R1 R1 + R0 x If N or P, goto x300B
Example 3 - # Occurrences of Inputted Char Program (2 of 2) AddressInstructionComments x300A R2 R2 + 1 x300B R3 R3 + 1 x300C R1 M[R3] x300D Goto x3004 x300E R0 M[x3013] x300F R0 R0 + R2 x Print R0 (TRAP x21) x HALT (TRAP x25) X3012Starting Address of File x ASCII x30 (‘0’)
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 cc14A0 BRzp NOT R2, R294BF ADD R2, R2, #114A1 R5 will contain x30 (#48), the offset for ASCII numbers AND R5, R5, #05B60 ADD R5, R5, #151B6F ADD R5, R5, #31B63 Check for [R2] >9 AND R4, R4, #0 ; init high digit holder to zero5920 ADD R2, R2, #-1014B6 BRn ADD R4, R4, #11921 BR -40FFC ADD R3, R2, #10 ; save low digit16AA If high digit = 0, don't display it ADD R4, R4, #0 ; set CC1920 BRz Display high digit ADD R0, R4, R51105 TRAP x21F021 Display low digit Add R0, R3, R510C5 TRAP x21F021 Halt TRAP x25F025