CS225,  Fall 2006,  Monday Sept 25, Day 14  Hit reload! After class

(Re) Read  6.2 ,  while, do, for.   Next, 6.3, procedure with no parameters.

PEP8.03 looks good.  I'll put it in the lab this afternoon.  Feel free to download it from the usual site.
Another wrinkle in PEP:  If you re-Execute without re-Load ing first, your memory locations will not be restored to their original ones (e.g. if you initialize something to be 0 using .word 0, it won't be re-initialized. )  All that happens is the PC is reset to 0.  So ReLOAD, at least.


HW Day 14, Due Wednesday Day 15
Give me some Programs!  I'll extend the due date on Program 3 till Wednesday also.  Catch up!
Do the Day 13 hw, including D, which was postponed.  (D is a good place to experiment with Compare.  Try it with SubA first if you want a better handle on what happens at the branch, then substitute CPA.)

Also E. (following up on class work) Edit  Program B (Bottom of Day 13 page, with the V flag) to make these "improvements":
 a) The first LDA is next to the rest of the instructions that find the sum (as suggested in class).
   b) You take advantage of the fact that the Store instructions don't change the flags, and move the duplicated Store instructions out of the if/else  structure to one instruction at the beginning of it (thus putting the sum = num1+num2 instructions all together). 
   c)  The DECO instructions are also duplicated.  Move the  duplicated Deco  instructions out of the if/else  structure to one instruction at the end  of it.  Be careful with your labels.
The  resulting program should give results that look exactly like the original one.

Please hand in the Listing from each program.   And draw those arrows from each Branch to its target!

- - - - - - - - - - - - - - - - - - - - - -
Postpone A, B, C below.

Reassigned Day 17 A. 
Linearize the  flowchart on your handout. (Redraw it so all the boxes are in one column.)
Reassigned Day 17 B.  Run the C++ program of Figure 6.14 (p. 250) Verify that the loop counter continues to exist and is one bigger than the last value in the loop, after the loop is done.  In a highly structured language you might expect that it should disappear after the counted loop is finished.   Write down your opinion on using the value in the loop counter after the loop is finished, when working in a structured environment.  
Reassigned Day 17 C.  Make a  detailed pseudocode analysis or a  flowchart for this problem: count the number of 1's in the rightmost 4 bits of the binary representation of a number input from the keyboard.  Output the original number and the number of 1's.

Reassigned Day 19 D.(postponed from day 13).  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".  No loop, just an if.
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.

Postpone the rest:
Reassigned Day 19 Textbook p. 318, #2, #3(loop),
  Reassigned Day 18   #4 Before (or after??) you figure out what it does, print out the program of figure 6.16.  Put in all the arrows.  There are small bits of "dead", "inaccessible" code, that never will be executed.  Find and mark these. )
   #12 (Write a while loop)
Reassigned Day 19     #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.)

- - - - - - - - - - - - - - - - - - - -
Program 5, assigned Day 14 15, due Monday Day 17, Oct 22   For loop: What will CHARO output?  Write a program.  "Global" variable style is 0K.
    For j = 0 to 127 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> 
Save your output file, open it in Word or equivalent, and print out the results for 30 up; describe any effects from values below 30 (you know hex 0A = 10D is the linefeed)
   Programming/debugging suggestions.  One problem is writing the "for" loop.  Write it so it just prints out the decimal numbers from 0 to 127, to make sure it works.     The other is printing out a correct line of output.  Write this so it works for a single decimal value, for instance 66.  Then put the two together.  (Or work on one and then the other. )  If you try to do both at once, at this point, you're likely to get lost.  "Top down" is excellent, but you have to understand your building materials for it to work.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Notes:  Continuing with If/else, Day 13

Branches must happen as soon as the flag is set, or at least before something else changes a flag.
 Flags stay if not affected by next op.   Check front of book: Which ops do NOT affect flags?
    Str, Stbyte, Chari, Charo, Deco, Stro, all BR's  (& CALL, jump to procedure, RETn return from proc., RETTRreturn from trap, MOVSPA, MOVFLGA, STOP, and the NOPs) )
Some ops only affect N, Z. Which?  Ldr, Ldbyte, And, Or, NotDeci affects NZV, ASR affects NZC , Rotates affect only C.
So it's safe to Store a value before branching on it.

Altering a program with branches:  It's very easy to find you have to insert some code at a target, or (less often) take it out, and without the structures of the higher order language, you can forget to move your target labels!  A helpful aid for this is a NOP (there are NOP0, NOP1, 2, 3 in PEP8--they are "placeholders" we can use to program traps).  But NOP is a "no operation operation" which does nothing, just "marks time".  If you make your "target" a NOP, you (probably) won't mess up your branching  when you change your code.

Loops:  While, Do while (once thru at least), For j = 1 to 5.  Handout.

Examples, pp247-251.  Fig. 6.10, while: inputs letters and outputs them  while letter != '*'  Uses global variable
Fig 6.11, do while: Adds 25 to cop's distance and 20 to driver's, while cop < driver.  Uses globals.  Could we make it shorter by leaving driver in A and comparing to cop?  What should we branch back on then?
Fig 6.12, for :  Outputs 0, 1, 2 within the loop, then (why in the world?!?  To point out that j is one too big once it's outside.) outputs 3 outside.   Uses locals on the stack.   Loop is equivaent to For (j = 0; j < 2; j++)   Yuck:  i = counter, i = immediate. Go through and change all the counter  i's to j's, reserving i for immediate mode.
  Compare j to 3= compute j - 3.  Branch out of the loop if j is > 3.  Increment j at the bottom.  Basically a "while", branching on a counter.


To Sievers Home Page
CS225-Fall06/Day14.htm 
1:30pm, 9/25/06
This page belongs to Sally Sievers who is solely responsible for its content. Please see our statement of responsibility.