Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Programming Adapted from slides provided by McGraw-Hill Companies Inc., modified by professors at University of Wisconsin-Madison.

Similar presentations


Presentation on theme: "Chapter 6 Programming Adapted from slides provided by McGraw-Hill Companies Inc., modified by professors at University of Wisconsin-Madison."— Presentation transcript:

1 Chapter 6 Programming Adapted from slides provided by McGraw-Hill Companies Inc., modified by professors at University of Wisconsin-Madison

2 Slide Deck Info Content Slide Numbers Evaluate yourself 3 – 21 Lecture
22 – 56 Problems 57 – 68

3 Self Evaluation

4 Steps of Evaluation Step 1: Understand an instruction
x What does it do? R3 -> M[M[x300B]]

5 Steps of Evaluation Step 1: Understand an instruction
Step 2: Execute an instruction

6 Execute an instruction
x R3 -> M[M[x300B]] Initial State R1 x0005 R3 x0006 R5 x0007 x300A x300B x300C x300D Final State R1 x0005 R3 x0006 R5 x0007 x300A x300B x300C x300D

7 Steps of Evaluation Step 1: Understand an instruction
Step 2: Execute an instruction Step 3: Execute a program

8 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 PC 0x3000

9 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 0x0003 PC 0x3001

10 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 0x0003 R3 0x0000 PC 0x3002

11 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 0x0003 R3 PC 0x3003

12 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 R3 PC 0x3004

13 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 R3 PC 0x3002

14 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 R3 0x0004 PC 0x3003

15 Execute a program Address Memory Content
x3000 x2805 ; R4 <- M[PC’+5] (LD R4, #5) x3001 x56E0 ; R3 <- 0 (AND R3, R3, #0) x3002 x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2) x3003 x193F ; R4 <- R4 – 1 (ADD R4, R4, -1) x3004 x03FD ; BR if P to PC’-3(BRp #-3) x3005 xF025 ; HALT x3006 x0003 Current State R2 0x0002 R4 0x0001 R3 0x0004 PC 0x3004

16 And so on… Try out Problem 4 in the practice set

17 Steps of Evaluation Step 1: Understand an instruction
Step 2: Execute an instruction Step 3: Execute a program Step 4: Write (1-2) instructions

18 Writing Instructions Type 1: Task specifies the instruction
Example: Problem 3 To Do: R1 <- M[x3020] Given: R0 = x3030, M[x300A] = x3020 Address Instruction 0x3000 0010  = 0x221F 0x3001 1010  = 0xA208 0x3002 0110  = 0x6230 0x3003 Cannot be done (LEA)

19 Writing Instructions Type 2: Find your own way
Example: Problem 1 (2) of the HW To Do: R0 <- 2’s complement of R4 Example: Problem 1(3) of the HW To Do: R1 <- M[x0003]

20 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!

21 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.

22 Chapter 6 Programming

23 Solving a problem Problem Solving
We want to solve a problem using a computer Problem Solving Writing a program Debugging

24 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

25 Prob Stmt to Algorithm Process called
“Systematic decomposition” or “Stepwise Refinement” Decompose a task into sub-tasks and even smaller sub-tasks

26 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?

27 Assume the following String starts at memory location x4002
A null-terminated string Character to be counter at memory location x4001 Each memory location contains one ASCII character Count to be stored at location x4000

28 Three basic constructs
Task Subtask 1 Subtask 2 Sequential Cond Subtask 1 Subtask 2 Conditional True False Cond Subtask 1 Iterative True False

29 Sequential Read the value at x4001 to R0, and set R1 to 0 Start
Set the value of x4000 to the count of occurrences of a character, stored at x4001, in a null-terminated string, starting at x4002 Read each character of the string into R3, if R3 equal R0 increment R1 We are going to use specifics of LC-3, but the process can be used for any programming language Write R1 to x4000 Stop

30 Iterative Read first character into R3 False R3 ≠ null ?
Read each character of the string into R2, if R2 equal R0 increment R1 True If R3 is equal to R0, increment R1 Read next character into R3

31 Conditional R3 = R0? True False If R3 == R0, increment R1 R1 = R1 + 1

32 Which to use When? A skill to develop!
Tip1: 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?

33 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

34 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 R1==R2, do (R1-R2) and check for Z

35 Code for Conditional

36 Code for Iterative

37 The complete flow chart
Start R0 = M[x4001] M[x4000] = R1 R3=R0? R1 = 0 Stop R1 = R1 + 1 R2 = x4002 R2 = R2 + 1 R3 = M[R2] R3 = M[R2]

38 Let’s write some code!

39 Background: if-else Used to execute an operation only if a condition is satisfied, and some other operation if not. if(cond.) { } else { if(R3 == R0) { R1 = R1 + 1 } else { <do nothing>

40 Background: Loop To perform an operation over and over again, until a condition becomes false. while(cond.) { } while(R3!=0) { <check if R3 is equal to R0> <read next character> } LOOP: <Generate condition> if cond False: go to END <statements in loop> <go to LOOP> END: <statements after while>

41 The complete flow chart
Start R0 = M[x4001] M[x4000] = R1 R3=R0? R1 = 0 Stop R1 = R1 + 1 R2 = x4002 R2 = R2 + 1 R3 = M[R2] R3 = M[R2]

42 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”

43 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.

44 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? 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.

45 Debugging operations Display values in memory and registers.
Deposit values in memory and registers. Execute instruction sequence in a program. Stop execution when desired.

46 Types of errors Errors Syntax Logical Data

47 Types of Errors Syntax Errors Example: 1001 001 001 101111
Mostly the result of a typing error Not usually a problem with machine language (though can still happen!) Caught at compile time Example:

48 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.

49 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.

50 Tracing Options Tracing
Execute the program piece-by-piece, examining the affected register and memory at each step. Tracing Single-stepping Breakpoints Watchpoints

51 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.

52 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.

53 Watchpoints Tells the simulator to stop when the value of a register or memory location changes. <Not available in PennSim>

54 Debug 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 R5 = 0? HALT No Yes x x x x x Set R4 = 10, R5 =3. Run program.

55 Debugging Example 1 Logical Error Data Error
Branch (at x3203) checks for N and P which leads to an extra iteration of the loop Data Error What is the result if R5 = 0?

56 Debug 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 x x x x x x x x x x R1 = R1 + M[R2] R2 = R2 + 1 R4 = R4 - 1 R4 = 0? No Yes HALT

57 Debugging Example 2 Logical Error
Loading the value at address x3100 instead of the address x3100 into R2 by instruction at x3003 Change LD to LEA

58 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 not notice that address x3107 was loaded instead of x3100. Test your program using a variety of input data. Testing for data errors Be sure to test extreme cases (all ones, no ones, ...).

59 Problems

60 Problem 1 The program below checks to see if the value stored in R0 is greater than or equal to the value stored in R1. If R0 is smaller than R1, the value of R1 is copied to R0. Otherwise nothing is done. Insert the missing LC-3 machine language instructions. Address Instruction Comment x3000 x3001 x3002 x3003 x3004 x3005

61 Solution Address Instruction Comment x3000 1001 010 001 11111
R2 = NOT(R1) x3001 R2 = R2 + 1 x3002 R3 = R0 + R2 [R0-R1] x3003 BR if Z or P to HALT x3004 R0 = R1 x3005 HALT

62 Problem 2 Complete the program corresponding to the flowchart shown below. Address Content Comment x3000 R2=M[x3006] x3001 R2 = R2 - 1 x3002 BR to HALT x3003 R3=R3+R4 x3004 x3005 Halt x3006 Data value

63 Solution Address Content Comment x3000 0010 010 000000101 R2=M[x3006]
R2 = R2 - 1 x3002 BR to HALT x3003 R3=R3+R4 x3004 BR to x3001 x3005 Halt x3006 Data value

64 Problem 3 If the conditional branch (at x3103) redirects control to location x3100, state what is known about R1 and R2 before the execution of the program.  Address Content x3100 x3101 x3102 x3103

65 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 x3002 was executes, the value written into R2 was 0. “If you haven’t done the problem, try to do it now. (keep using the above strategy)”

66 Solution Instruction at x3002 is [R2 = NOT(R2)]. If R2 was 0 after the execution of the instruction, R2 was 0xFFFF (or -1) before its execution. Instruction at x3001 is [R2 = R2 AND R1]. If R2 = 0xFFFF after execution of this instruction, then R1 and R2 should be 0xFFFF before its execution. Instruction at x3000 is [R1 = NOT(R1)]. If R1 is 0xFFFF after the execution of the instruction, R1 = 0 before its execution. So, R1 = 0, R2 = 0xFFFF

67 Problem 4 Assume that you need to store one of your favorite numbers, 0x3030, into R1 using an instruction placed at 0x3000. 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 0x3030 into R1 using just one instruction placed at 0x3000. Note: You cannot assume the values of any of the registers or memory locations.

68 Solution Can be done with LEA:

69 Problem 5 The following program increments R0 by 1, if R1 > R2. Fill in the missing instruction. Address Contents Comments x3000 x3001 x3002 x3003 x3004 x3005

70 Solution Address Contents Comments x3000 1001 010 010 111111
R2 = NOT(R2) x3001 R2 = R2 + 1 x3002 R2 = R2 + R1 x3003 BR if N or Z to HALT x3004 R0 = R0 + 1 x3005 HALT


Download ppt "Chapter 6 Programming Adapted from slides provided by McGraw-Hill Companies Inc., modified by professors at University of Wisconsin-Madison."

Similar presentations


Ads by Google