The LC-2 Instruction Set Architecture

The LC-2 Instruction Set Architecture

Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin Madison Chapter 6 Part II: Debugging Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging Youve written your program and it doesnt work. Now what? What do you do when youre lost in a city? Drive around randomly and hope you find it? Return to a known point and look at a map? In debugging, the equivalent to looking at a map is tracing your program. Examine the sequence of instructions being executed. Keep track of results being produced. Compare result from each instruction to the expected result. 6-3 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging Operations Any debugging environment should provide means to: 1. 2. 3. 4. Display values in memory and registers. Deposit values in memory and registers. Execute instruction sequence in a program. Stop execution when desired. Different programming levels offer different tools. High-level languages (C, Java, ...) usually have source-code debugging tools. For debugging at the machine instruction level: Simulator any universal computing device can emulate another UCD operating system monitor tools in-circuit emulators (ICE)

plug-in hardware replacements that give instruction-level control 6-4 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. PennSim Simulator Start/stop execution Command window set/display registers, memory, and frame buffer set breakpoints 6-5 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Types of Errors Syntax Errors You made a typing error that resulted in an illegal operation. Not usually an issue with machine language, because almost any bit pattern corresponds to some legal instruction. In high-level languages, these are often caught during the translation from language to machine code. Logic Errors Your program is legal, but wrong, so the results dont match the problem statement. Trace the program to see whats really happening and determine how to get the proper behavior. Data Errors Input data is different than what you expected. Test the program with a wide variety of inputs. 6-6 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Tracing the Program Execute the program one piece at a time,

examining register and memory to see results at each step. Single-Stepping Execute one instruction at a time. Tedious, but useful to help you verify each step of your program. Breakpoints Tell the simulator to stop executing when it reaches a specific instruction. Check overall results at specific points in the program. Lets you quickly execute sequences to get a high-level overview of the execution behavior. Quickly execute sequences that your believe are correct. Watchpoints (not available in PennSim) Tell the simulator to stop when a register or memory location changes or when it equals a specific value. Useful when you dont know where or when a value is changed. 6-7 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 1: Multiply This program is supposed to multiply the two unsigned integers in R4 and R5. clear R2 add R4 to R2 decrement R5 No R5 = 0? Yes HALT x3200 x3201 x3202 x3203 x3204 0101010010100000 0001010010000100 0001101101111111 0000011111111101 1111000000100101 Set R4 = 10, R5 =3. Run program. Result: R2 = 40, not 30. 6-8

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging the Multiply Program PC PC and registers at the beginning of each instruction R2 R4 R5 x3200 -- 10 3 x3201 0 10 3 x3202 10 10 x3203 10 10 x3201 10 10 x3202

20 10 x3203 20 10 x3201 20 10 x3202 30 10 x3203 30 10 0 x3201 30 10 0 x3202 40 10 0 x3203 40 10

-1 x3204 40 10 -1 40 10 -1 3 Single-stepping Breakpoint at branch (x3203) PC R2 R4 R5 x3200 0101010010100000 x3203 10 10 2 2 x3201 0001010010000100 x3203 20 10 1 2 x3202 x3203 0001101101111111 30 10 0 2 x3203

40 10 -1 1 x3203 0000011111111101 40 10 -1 1 x3204 1111000000100101 1 Should stop looping here! Executing loop one time too many. Branch at x3203 should be based on P bit only, not Z and P. 6-9 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 2: Summing an Array of Numbers This program is supposed to sum the numbers stored in 10 locations beginning with x3100, leaving the result in R1. R1 = 0 R4 = 10 R2 = x3100 R1 = R1 + M[R2] R2 = R2 + 1 R4 = R4 - 1 No R4 = 0? Yes HALT x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009

0101001001100000 0101100100100000 0001100100101010 0010010011111100 0110011010000000 0001010010100001 0001001001000011 0001100100111111 0000001111111011 1111000000100101 6-10 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging the Summing Program Running the the data below yields R1 = x0024, but the sum should be x8135. What happened? Address Contents x3100 x3107 x3101 Start single-stepping program... x2819 PC R1 x3000 -- x3102 x0110 x3001 x3103 x0310 x3104

x0110 x3105 x1110 x3106 x11B1 x3107 x0019 x3108 x0007 x3109 x0004 R2 R4 -- -- 0 -- x3002 0 -0 x3002 0001100100101010 x3003 0 -10 x30030 0010010011111100 x3004 x3107 10 x3004 0110011010000000 Should be x3100! Loading contents of M[x3100], not address. Change opcode of x3003 from 0010 (LD) to 1110 (LEA).

6-11 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 3: Looking for a 5 This program is supposed to set R0=1 if theres a 5 in one of ten memory locations, starting at x3100. Else, it should set R0 to 0. R0 = 1, R1 = -5, R3 = 10 R4 = x3100, R2 = M[R4] R2 = 5? No No R3 = 0? R4 = R4 + 1 R3 = R3-1 R2 = M[R4] Yes R0 = 0 HALT Yes x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 x300A x300B x300C x300D x300E x300F x3010 0101000000100000 0001000000100001

0101001001100000 0001001001111011 0101011011100000 0001011011101010 0010100000001001 0110010100000000 0001010010000001 0000010000000101 0001100100100001 0001011011111111 0110010100000000 0000001111111010 0101000000100000 1111000000100101 0011000100000000 6-12 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging the Fives Program Running the program with a 5 in location x3108 results in R0 = 0, not R0 = 1. What happened? Address Contents x3100 9 x3101 7 x3102 32 x3103 Perhaps we didnt look at all the data? Put a breakpoint at x300D see x3007 to 0110010100000000 how many times wex3008 branch0001010010000001 back. PC

R0 R2 x300D 1 7 0 x300D 1 32 x3104 -8 x300D 1 0 x3105 19 0 0 x3106 6 x3107 13 x3108 5 x3109

61 x3009 0000010000000101 R3 x300AR40001100100100001 9 x3101 x300B 0001011011111111 8 x3102 x300C 0110010100000000 x300D 0000001111111010 7 x3103 x300E 0101000000100000 7 x3103 Didnt branch x300F 1111000000100101 back, even x3010 0011000100000000 though R3 > 0? Branch uses condition code set by loading R2 with M[R4], not by decrementing R3. Swap x300B and x300C, or remove x300C and branch back to x3007. 6-13 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example 4: Finding First 1 in a Word This program is supposed to return (in R1) the bit position of the first 1 in a word. The address of the word is in location x3009 (just past the end of the program). If there are no ones, R1 should be set to 1. R1 = 15 R2 = data R2[15] = 1? No decrement R1 shift R2 left one bit No R2[15] = 1? Yes HALT

Yes x3000 x3001 x3002 x3003 x3004 x3005 x3006 x3007 x3008 x3009 0101001001100000 0001001001101111 1010010000000110 0000100000000100 0001001001111111 0001010010000010 0000100000000001 0000111111111100 1111000000100101 0011000100000000 6-14 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging the First-One Program Program works most of the time, but if data is zero, it never seems to HALT. Breakpoint at backwards branch (x3007) PC R1 PC R1 x3007 14 x3007 4 x3007 13

x3007 3 x3007 12 x3007 2 x3007 11 x3007 1 x3007 10 x3007 0 x3007 9 x3007 -1 x3007 8 x3007 -2 x3007 7 x3007 -3

x3007 6 x3007 -4 x3007 5 x3007 -5 If no ones, then branch to HALT never occurs! This is called an infinite loop. Must change algorithm to either (a) check for special case (R2=0), or (b) exit loop if R1 < 0. 6-15 Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Debugging: Lessons Learned Trace program to see whats going on. Breakpoints, single-stepping When tracing, make sure to notice whats really happening, not what you think should happen. In summing program, it would be easy to not notice that address x3107 was loaded instead of x3100. Test your program using a variety of input data. In Examples 3 and 4, the program works for many data sets. Be sure to test extreme cases (all ones, no ones, ...). 6-16

Recently Viewed Presentations

  • Governors Office of the Deaf and Hard of

    Governors Office of the Deaf and Hard of

    Such disorders can include clang associations (rhyming to handshapes in ASL), loose associations, flight of ideas, incoherence, tangentiality and fragmentation. The latter is particularly difficult to sort out in a patient who does not present fluency in sign language.
  • Ms. Taylor Ms. Taylors Week of December 4,

    Ms. Taylor Ms. Taylors Week of December 4,

    Rabbit Words (Syllable Division) Language Skill. Root Words . We will learn how to use a known root word as a clue to find the meaning of an unknown word with the same root. Ex: addition - additional . December...
  • Understanding Semi-monthly - Jackson State University

    Understanding Semi-monthly - Jackson State University

    Understanding Semi-monthly. Number of pays. What's the difference between a . ... How will my paycheck be impacted by this transition? A. 1st Pay check pays you for services perform from the 1st through 15th of the month. 2. nd....
  • Solar Cell Testing

    Solar Cell Testing

    SOLAR CELL TESTING Basic Structure of a Solar Cell Basic Photovoltaic Cell Model This model consists of Built-in voltage Current due to optical generation Series resistance Shunt Resistance Key Parameters Open Circuit Voltage, Voc (V) Short Circuit Current Density, Jsc...
  • Foundations of Leadership Development - From the Division to ...

    Foundations of Leadership Development - From the Division to ...

    In this rapidly growing division approximately 75% of the members are under 30 years of age—young members who are still in the process of getting an education and finding their way in life. "If we do not support them they...
  • Crystal Ball : On the Future High Energy

    Crystal Ball : On the Future High Energy

    Crystal Ball : On the Future High Energy Colliders * Vladimir Shiltsev. Fermilab, Batavia, IL , USA. Accelerator Physics Center. August 4, 2015 *FRA, LLC operates Fermilab under contract No. DE-AC02-07CH11359 with the U.S. DOE
  • Adjunctive Pharmacotherapy In Sepsis Host Response To Sepsis

    Adjunctive Pharmacotherapy In Sepsis Host Response To Sepsis

    In The Cochrane Library Issue 2. Oxford : Update Software Ltd ; 2002, 4 Polyclonal IVIG Systematic review 27 RCTs N = 492 Reduce overall mortality (RR = 0.64, 95% CI 0.51-0.80) Conclusion: all trials were small and the evidence...
  • PushDown Automata

    PushDown Automata

    Although non-deterministic and deterministic FA are equivalent this is not the case with PDA. Non-determinism helps us recognize more languages. Intuition: L. R = { wwR : w in {0,1}* } An NPDA for this language pushes the first half...