Document 18028355

advertisement
Adapted from slides provided by McGraw-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
Steps of Evaluation

Step 1: Understand an instruction
x3003
1011 0110 0000 0111
What does it do?
R3 -> M[M[x300B]]
Steps of Evaluation
Step 1: Understand an instruction
 Step 2: Execute an instruction

Execute an instruction
x3003
1011 0110 0000 0111
R3 -> M[M[x300B]]
Initial State
Final State
R1
x0005
R1
x0005
R3
x0006
R3
x0006
R5
x0007
R5
x0007
x300A
x300B
x300A
x0006
x300B
x300A
x300B
x300A
x300C
x300D
x300C
x300D
x300D
x300C
x300D
x300C
Steps of Evaluation
Step 1: Understand an instruction
 Step 2: Execute an instruction
 Step 3: Execute a program

Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
PC
0x3000
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0003
PC
0x3001
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0003
R3
0x0000
PC
0x3002
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0003
R3
0x0002
PC
0x3003
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0002
R3
0x0002
PC
0x3004
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0002
R3
0x0002
PC
0x3002
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0002
R3
0x0004
PC
0x3003
Execute a program
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Memory Content
x2805 ; R4 <- M[PC’+5] (LD R4, #5)
x56E0 ; R3 <- 0 (AND R3, R3, #0)
x16C2 ; R3 <- R3 + R2 (ADD R3, R3, R2)
x193F ; R4 <- R4 – 1 (ADD R4, R4, -1)
x03FD ; BR if P to PC’-3(BRp #-3)
xF025 ; HALT
x0003
Current State
R2
0x0002
R4
0x0001
R3
0x0004
PC
0x3004
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: R1 <- M[x3020]
Given: R0 = x3030, M[x300A] = x3020
Address
Instruction
0x3000
0010 001 000011111
= 0x221F
0x3001
1010 001 000001000
= 0xA208
0x3002
0110 001 000 110000 = 0x6230
0x3003
Cannot be done (LEA)
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]
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.
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 Stmt 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 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

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 x4001
to R0, and set R1 to 0
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
Write R1 to x4000
Stop
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
Conditional
True
If R3 == R0, increment
R1
R1 = R1 + 1
R3 = R0?
False
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?
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 R1==R2, do (R1-R2)
and check for Z
Code for Conditional
Code for Iterative
The complete flow chart
Start
R3≠0?
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]
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(R3 == R0) {
R1 = R1 + 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(R3!=0) {
<check if R3
is equal to
R0>
<read next
character>
}
The complete flow chart
Start
R3≠0?
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]
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?
 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.
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 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 PennSim>
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
No
R5 = 0?
Yes
HALT
x3200
x3201
x3202
x3203
x3204
0101010010100000
0001010010000100
0001101101111111
0000011111111101
1111000000100101
Set R4 = 10, R5 =3.
Run program.
Debugging Example 1

Logical 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?
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
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
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
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, ...).
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
x3000
x3001
0001 0100 1010 0001
x3002
0001 0110 0000 0010
x3003
x3004
0001 0000 0110 0000
x3005
1111 0000 0010 0101
Comment
Solution
Address
Instruction
Comment
x3000
1001 010 001 11111
R2 = NOT(R1)
x3001
0001 0100 1010 0001
R2 = R2 + 1
x3002
0001 0110 0000 0010
R3 = R0 + R2 [R0-R1]
x3003
0000 011 000000001
BR if Z or P to HALT
x3004
0001 0000 0110 0000
R0 = R1
x3005
1111 0000 0010 0101
HALT
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
0001 0110 1100 0100
R3=R3+R4
x3005
1111 0000 0010 0101
Halt
x3006
0000 0000 0000 1010
Data value
x3004
Solution
Address
x3000
x3001
x3002
x3003
x3004
x3005
x3006
Content
010 000000101
0100 1011 1111
100 000000010
0110 1100 0100
Comment
R2=M[x3006]
R2 = R2 - 1
BR to HALT
R3=R3+R4
0000 111 111111100
1111 0000 0010 0101
0000 0000 0000 1010
BR to x3001
Halt
Data value
0010
0001
0000
0001
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
1001 001 001 111111
x3101
0101 010 010 000001
x3102
1001 010 010 111111
x3103
0000 010 111 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 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)”
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
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.
Solution
Can be done with LEA:
1110 001 000101111
Problem 5
The following program increments R0 by 1, if R1 > R2. Fill in the missing
instruction.
Address
Contents
x3000
1001 010 010 111111
x3001
0001 010 010 1 00001
x3002
0001 010 001 000 010
x3003
x3004
0001 000 000 1 00001
x3005
1111 0000 0010 0101
Comments
Solution
Address
Contents
Comments
x3000
1001 010 010 111111
R2 = NOT(R2)
x3001
0001 010 010 1 00001
R2 = R2 + 1
x3002
0001 010 001 000 010
R2 = R2 + R1
x3003
0000 110 000000001
BR if N or Z to
HALT
x3004
0001 000 000 1 00001
R0 = R0 + 1
x3005
1111 0000 0010 0101
HALT
Download