CS225, Fall 2006, Friday Oct. 6, Day 19 Hit
reload! After class
(Re) Read Ch. 6.2, Branching and flow
of control
A
take-home midterm will be available Wednesday, Oct 11 (day 20) due the
following Wednesday.
I will be basing 6-week grades on the HW I have received on
Wednesday Oct 11 at the beginning of class.
HW Day 19, Due Wed
Day 20 If you have no access to a computer with PEP over
break, write the programs out by hand and mentally run them, looking
for bugs. (Actually,
that's a good idea even if you do have a computer)
A. Write this simple program to explore the characters with most
sig. bit 1: Get a decimal number j (1-127) from input. Output j, a space, the character with that ascii
number, a space, the
character with
that ascii number + 128, and line feed.
Example: for decimal 66, the
output will be 66 B
Â
<LF>
(For Program 5 you will put this inside a For loop.)
From Day 13/14 HW
D.
(Finish and run this) Write a program that gets two
numbers from input. If the second is smaller than the first,
output an S. (If not, do nothing). At the end, output "Bye".
Check it on input pairs 22 36, 22 22, 22 15. You may use
"global" type "variables." Hand in or email
code, hand in listing.
Textbook p. 318, #2 (optimizing)
Program 4 (Program 4, Reassigned
Day 19, Fri. Oct. 6. Due Day 21 Fri. Oct. 13. In
program C Day 18? you did
integer division by
8. What about the remainder? (Sometimes called n mod 8; in
C++ I
think mod = %. 16 mod 8 = 0, 19 mod 8 = 3, 26 mod 8 = 2.
There are
several ways to do this. Here do it
by
being clever mathematically to begin with. Find the
remainders
of some numbers on division by 8, and look at what is happening to
them
at the binary level.
38D = 26H = 10 0110
Divide
by 8; remainder =? Repeat with other numbers. Your
algorithm doesn't need to work for division by anything but 8.
When you can see how to get easily from the
number
to the remainder, only then write the program. (Bring the
algorithm to class Wednesday!) Hand
in or
email
code, hand in listing, hand in verbal description of the algorithm
unless it's in clear comments in the program.
Remember, your algorithm doesn't have to be generalizable to any
other number. JUST good for 8.
Branching:
(from Day 13/14) Textbook p.
319 #13 Don't write the
program.
Make a flow chart, with a diamond decision for each part of the if.
(For
example, one diamond for (ch>='A') and another for (ch<='Z')
)
Hand in your original flow chart, and a linearized one. (Notice,
no loop.) (Note: it took a while for a programmer trained in
a context without compound decisions like (a &&b) to learn to
use them. It's probably just as hard to figure out how to do
without.)
(From day 13) A. Run and
trace Program A, Day 13,
watching
flags and program flow.
In each of the parts, start with the original Program A.
a) Modify Program A
so that it marks a number Z, zero or N, nonzero. Hand in or email
code, hand in listing.
b) Modify Program A
so that it marks a number P, strictly positive or N, not
positive (<0). Hand in or email
code, hand in listing.
With this one, fill in a tracing form, once for a Positive number run
and
once for a Not positive,
noting especially the PC and the flags.
D?NO...).
Think about how to multiply a number
by a 4-bit number, in
binary. Come in with a
pseudocode or flowcharted algorithm. (With or without a loop, your
choice.)
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= = = = = = =
Notes:
Put on board answers to A1: For program
D2 from the Stacks handout, write
the C++ program it
corresponds to. (I won't be marking on syntax or whether
the #include stuff is right, just on the basic ideas.)
Put on board answers to
B. Take the Wallspace problem
from Day 16, and rewrite it so that door, and wall, are Local
variables. (Wallspace, if a constant, will stay the same. If you
chose to make it a global, it can remain a global.)
IF then / ELSE
Return to IF-Then-Else. Handout, Day 13, notes, Day 14, notes
Conditional branches: If flag(s)
= 0/1 then BR target. (p. 241-2, A5)
BRV
If V=1 then PC<--Operand ; branch if signed overflow
BRC
If C=1 then PC<--Operand ; branch if unsigned
overflow (
Carry)
BREQ If
Z=1,
branch if last op gave
ZERO
BRNE If Z =0... branch if last op gave Not Zero
BRLT If
N=1, branch if last op gave NEG,
<0
BRGE If N =0... branch if last op gave Not Neg >0
BRLE If
N=1 or Z=1
"
not positive, <0
BRGT If N=0 and Z=0
"
POSitive, >0
-- Often, if A true is programmed by branch if A false.
-- CPr instead of SUBr to test if 2 values equal, sign of
difference. ( register - operand)
-- NOP0 as target of branch while debugging; saves trouble if need to
insert instructions in or at end of if/else structure.
Do on board, old D:
Write a program that gets two
numbers from input. If the second is smaller than the first,
output an S. (If not, do nothing). At the end, output "Bye".
Check it on input pairs 22 36, 22 22, 22 15. You may use
"global" type "variables." Hand in or email
code, hand in listing.
After break, loops!
Not yet, yet again: How to multiply whole
numbers? (We will program this.)
This page belongs to Sally Sievers who is solely
responsible
for its content. Please see our statement
of responsibility.