CHAPTER 6 PROGRAMMING Adapted from slides provided by
CHAPTER 6 PROGRAMMING Adapted from slides provided by Mc. Graw-Hill Companies Inc. , modified by professors at University of Wisconsin-Madison
Slide Deck Info Content Slide Numbers Evaluate yourself 3 – 21 Lecture 22 – 56 Problems 57 – 68
Self Evaluation
Steps of Evaluation Step 1: Understand an instruction x 3003 1011 0110 0000 0111 What does it do? R 3 -> M[M[x 300 B]]
Steps of Evaluation Step 1: Understand an instruction Step 2: Execute an instruction
Execute an instruction x 3003 1011 0110 0000 0111 R 3 -> M[M[x 300 B]] Initial State Final State R 1 x 0005 R 3 x 0006 R 5 x 0007 x 300 A x 300 B x 300 A x 0006 x 300 B x 300 A x 300 C x 300 D x 300 C
Steps of Evaluation Step 1: Understand an instruction Step 2: Execute an instruction Step 3: Execute a program
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 PC 0 x 3000
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0003 PC 0 x 3001
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0003 R 3 0 x 0000 PC 0 x 3002
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0003 R 3 0 x 0002 PC 0 x 3003
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0002 R 3 0 x 0002 PC 0 x 3004
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0002 R 3 0 x 0002 PC 0 x 3002
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0002 R 3 0 x 0004 PC 0 x 3003
Execute a program Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Memory Content x 2805 ; R 4 <- M[PC’+5] (LD R 4, #5) x 56 E 0 ; R 3 <- 0 (AND R 3, #0) x 16 C 2 ; R 3 <- R 3 + R 2 (ADD R 3, R 2) x 193 F ; R 4 <- R 4 – 1 (ADD R 4, -1) x 03 FD ; BR if P to PC’-3(BRp #-3) x. F 025 ; HALT x 0003 Current State R 2 0 x 0002 R 4 0 x 0001 R 3 0 x 0004 PC 0 x 3004
And so on… Try out Problem 4 in the practice set
Steps of Evaluation Step 1: Understand an instruction Step 2: Execute an instruction Step 3: Execute a program Step 4: Write (1 -2) instructions
Writing Instructions Type 1: Task specifies the instruction Example: Problem 3 To Do: R 1 <- M[x 3020] Given: R 0 = x 3030, M[x 300 A] = x 3020 Address Instruction 0 x 3000 001 000011111 = 0 x 221 F 0 x 3001 1010 001 000001000 = 0 x. A 208 0 x 3002 0110 001 000 110000 = 0 x 6230 0 x 3003 Cannot be done (LEA)
Writing Instructions Type 2: Find your own way Example: Problem 1 (2) of the HW To Do: R 0 <- 2’s complement of R 4 Example: Problem 1(3) of the HW To Do: R 1 <- M[x 0003]
Steps of Evaluation Step 1: Understand an instruction Step 2: Execute an instruction Step 3: Execute a program Step 4: Write (1 -2) instructions Step 5: Write a program Topic of Chapter 6!
Things to keep in mind Hex arithmetic vs. Decimal Arithmetic Instructions and Data are one and the same. It is all about interpretation. Memory is one big sequence of values, each of which may be data or instruction. Try yourself: Write a program which stores an instruction and then executes it.
CHAPTER 6 PROGRAMMING
Solving a problem We want to solve a problem using a computer Problem Solving Writing a program Debugging
Writing a program Start from the Problem statement From chapter 1: Problem statement is in a Natural language Ambiguous, cannot be understood by a computer Convert to Algorithm
Prob Statement to Algorithm Process called “Systematic decomposition” or “Stepwise Refinement” Decompose a task into sub-tasks and even smaller sub-tasks
Running example "We wish to count the number of occurrences of a character in a string” Ambiguities Where is the string located? Where is the character to be counted? Where should the count be stored?
Assume the following String starts at memory location x 4002 A null-terminated string Character to be counter at memory location x 4001 Each memory location contains one ASCII character Count to be stored at location x 4000
Three basic constructs Task Subtask 1 True False Cond True Subtask 2 Subtask 1 Sequential Cond Subtask 2 Conditional Subtask 1 Iterative False
Sequential Start Read the value at x 4001 to R 0, and set R 1 to 0 Set the value of x 4000 to the count of occurrences of a character, stored at x 4001, in a null-terminated string, starting at x 4002 Read each character of the string into R 3, if R 3 equal R 0 increment R 1 Write R 1 to x 4000 Stop
Iterative Read first character into R 3 False R 3 ≠ null ? Read each character of the string into R 2, if R 2 equal R 0 increment R 1 True If R 3 is equal to R 0, increment R 1 Read next character into R 3
Conditional True If R 3 == R 0, increment R 1 = R 1 + 1 R 3 = R 0? False
Which to use When? A skill to develop! Tip 1: Look at it as a puzzle or a word problem to solve in a Math class What is the starting state of the system? What is the desired ending state? How do we move from one state to another?
Which to use when Tip 2: Look for English words in your algorithm "do A then do B" sequential "if G, then do H" conditional "for each X, do Y" iterative "do Z until W" iterative
How to convert to LC-3 code? Sequential Instructions normally flow from one to the next Iterative and Conditional Use the BR instruction Create code to convert the condition to value of the CC register Example, to check if R 1==R 2, do (R 1 -R 2) and check for Z
Code for Conditional
Code for Iterative
The complete flow chart Start R 3≠ 0? R 0 = M[x 4001] M[x 4000] = R 1 R 3=R 0 ? R 1 = 0 Stop R 1 = R 1 + 1 R 2 = x 4002 R 2 = R 2 + 1 R 3 = M[R 2]
Let’s write some code!
Background: if-else Used to execute an operation only if a condition is satisfied, and some other operation if not. if(cond. ) { … } else { … } if(R 3 == R 0) { R 1 = R 1 + 1 } else { <do nothing> }
Background: Loop To perform an operation over and over again, until a condition becomes false. while(cond. ) { … } LOOP: <Generate condition> if cond False: go to END <statements in loop> <go to LOOP> END: <statements after while> while(R 3!=0) { <check if R 3 is equal to R 0> <read next character> }
The complete flow chart Start R 3≠ 0? R 0 = M[x 4001] M[x 4000] = R 1 R 3=R 0 ? R 1 = 0 Stop R 1 = R 1 + 1 R 2 = x 4002 R 2 = R 2 + 1 R 3 = M[R 2]
Writing pseudo-code Pseudo-code: A textual way of representing the flowchart (or algorithm) Change conditional constructs to if-else Change iterative constructs to use “if” and “go to”
Writing LC-3 code Convert the pseudo-code/flowchart to LC-3 code All conditions to be converted to CC Fill in the PC offset values (for LD, LDI, ST, STI, LEA, BR) after you have completed the entire code. If you delete/add an instruction, revisit all your PC offsets All data required by the program are stored after HALT. Far away addresses can be stored as data values.
Debugging You’ve written your program and it doesn’t work. Now what? What do you do when you’re lost in a city? Drive around randomly and hope you find it? PReturn 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.
Debugging operations Display values in memory and registers. Deposit values in memory and registers. Execute instruction sequence in a program. Stop execution when desired.
Types of errors Errors Syntax Logical Data
Types of Errors Syntax Errors Mostly the result of a typing error Not usually a problem with machine language (though can still happen!) Caught at compile time Example: 1001 001 101111
Types of Errors Logical Errors Outputs of your program don’t match your problem statement Solution: Trace the program to check against an expected output.
Types of Errors Data Errors Program works for most of the programs But for a few, they give an incorrect output. Identification is hard! Solution: Test your program for variety of inputs.
Tracing Options Execute the program piece-by-piece, examining the affected register and memory at each step. Tracing Singlestepping Breakpoints Watchpoints
Single Stepping Execute one instruction at a time + Most fine-grained approach to debugging. Can verify each instruction of your program. - Tedious. Almost impossible to do for large programs.
Breakpoints Tell the simulator to stop when it reaches a specific instruction. + Allows you to quickly execute a parts of the program you believe are correct, and concentrate on parts you believe are buggy. - Hard to know where to place the breakpoint unless you understand the program well.
Watchpoints Tells the simulator to stop when the value of a register or memory location changes. <Not available in Penn. Sim>
Debug Example 1: Multiply This program is supposed to multiply the two unsigned integers in R 4 and R 5. clear R 2 add R 4 to R 2 decrement R 5 No R 5 = 0? Yes HALT x 3200 x 3201 x 3202 x 3203 x 3204 01010100000 00010100100 0001101101111111 000001111101 1111000000100101 Set R 4 = 10, R 5 =3. Run program.
Debugging Example 1 Logical Error Branch (at x 3203) checks for N and P which leads to an extra iteration of the loop Data Error What is the result if R 5 = 0?
Debug Example 2: Summing an Array of Numbers This program is supposed to sum the numbers stored in 10 locations beginning with x 3100, leaving the result in R 1 = 0 R 4 = 10 R 2 = x 3100 R 1 = R 1 + M[R 2] R 2 = R 2 + 1 R 4 = R 4 - 1 No R 4 = 0? Yes HALT x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 x 3007 x 3008 x 3009 0101001001100000 0101100100100000 0001100100101010 0010010011111100 011010000000 0001010000100100100001100100111111 0000001111111011 1111000000100101
Debugging Example 2 Logical Error Loading the value at address x 3100 instead of the address x 3100 into R 2 by instruction at x 3003 Change LD to LEA
Debugging: Lessons Trace program to see what’s going on. Breakpoints, single-stepping When tracing, make sure to notice what’s really happening, not what you think should happen. In summing program, it would be easy to notice that address x 3107 was loaded instead of x 3100. Test your program using a variety of input data. Testing for data errors Be sure to test extreme cases (all ones, no ones, . . . ).
Problems
Problem 1 The program below checks to see if the value stored in R 0 is greater than or equal to the value stored in R 1. If R 0 is smaller than R 1, the value of R 1 is copied to R 0. Otherwise nothing is done. Insert the missing LC-3 machine language instructions. Address Instruction x 3000 x 3001 0100 1010 0001 x 3002 0001 0110 0000 0010 x 3003 x 3004 0001 0000 0110 0000 x 3005 1111 0000 0010 0101 Comment
Solution Address Instruction Comment x 3000 1001 010 001 11111 R 2 = NOT(R 1) x 3001 0100 1010 0001 R 2 = R 2 + 1 x 3002 0001 0110 0000 0010 R 3 = R 0 + R 2 [R 0 -R 1] x 3003 0000 011 00001 BR if Z or P to HALT x 3004 0001 0000 0110 0000 R 0 = R 1 x 3005 1111 0000 0010 0101 HALT
Problem 2 Complete the program corresponding to the flowchart shown below. Address Content Comment x 3000 R 2=M[x 3006] x 3001 R 2 = R 2 - 1 x 3002 BR to HALT x 3003 0001 0110 1100 0100 R 3=R 3+R 4 x 3005 1111 0000 0010 0101 Halt x 3006 0000 1010 Data value x 3004
Solution Address x 3000 x 3001 x 3002 x 3003 x 3004 x 3005 x 3006 Content 010 000000101 0100 1011 1111 100 000000010 0110 1100 0100 Comment R 2=M[x 3006] R 2 = R 2 - 1 BR to HALT R 3=R 3+R 4 0000 111111100 1111 0000 0010 0101 0000 1010 BR to x 3001 Halt Data value 0010 0001 0000 0001
Problem 3 If the conditional branch (at x 3103) redirects control to location x 3100, state what is known about R 1 and R 2 before the execution of the program. Address Content x 3100 1001 001 111111 x 3101 010 000001 x 3102 1001 010 111111 x 3103 0000 010 111100
Solution This is a problem where we have to “backtrack” the program: Branch is taken, implies the Z=1, N=P=0 This means the previous instruction which wrote to a register wrote the value 0 This implies after the instruction at x 3002 was executes, the value written into R 2 was 0. “If you haven’t done the problem, try to do it now. (keep using the above strategy)”
Solution Instruction at x 3002 is [R 2 = NOT(R 2)]. If R 2 was 0 after the execution of the instruction, R 2 was 0 x. FFFF (or -1) before its execution. Instruction at x 3001 is [R 2 = R 2 AND R 1]. If R 2 = 0 x. FFFF after execution of this instruction, then R 1 and R 2 should be 0 x. FFFF before its execution. Instruction at x 3000 is [R 1 = NOT(R 1)]. If R 1 is 0 x. FFFF after the execution of the instruction, R 1 = 0 before its execution. So, R 1 = 0, R 2 = 0 x. FFFF
Problem 4 Assume that you need to store one of your favorite numbers, 0 x 3030, into R 1 using an instruction placed at 0 x 3000. Do you think this can be done? If you agree, give a reason why it is not possible to do this. If you do not agree, then write the instruction (in hex) which stores the value 0 x 3030 into R 1 using just one instruction placed at 0 x 3000. Note: You cannot assume the values of any of the registers or memory locations.
Solution Can be done with LEA: 1110 001 000101111
Problem 5 The following program increments R 0 by 1, if R 1 > R 2. Fill in the missing instruction. Address Contents x 3000 1001 010 111111 x 3001 010 010 1 00001 x 3002 0001 010 001 000 010 x 3003 x 3004 0001 00001 x 3005 1111 0000 0010 0101 Comments
Solution Address Contents Comments x 3000 1001 010 111111 R 2 = NOT(R 2) x 3001 010 010 1 00001 R 2 = R 2 + 1 x 3002 0001 010 001 000 010 R 2 = R 2 + R 1 x 3003 0000 110 00001 BR if N or Z to HALT x 3004 0001 00001 R 0 = R 0 + 1 x 3005 1111 0000 0010 0101 HALT
- Slides: 70