CS225,  Fall 2004,  Monday Sept. 4, Day 5 Hit reload!v2

Handout:  templates for tracing program operations
Read: (Re)read in Ch.4
4.3 pp. 166-172, Von Neumann cycle.  Be sure to go thru the cycle pp.170-1.
   PC starts (in our machine) at address h#0000.
Von Neumann cycle:  Fetch instruction, increment PC (and get remainder of instruction if any), execute, repeat.
Read NOT p.96 (0-->1, 1-->0), AND OR &shifts 3.3 pp.105-109
Read 172-177   Von Neumann bugs:  computer does not know what is instructions and what is data.  (all just bits) 

Day 5HW:  Due Wed. Day 6.  (* problems/parts have answers in the back.  )
Text p. 228 #7  (hand trace program)

Text p. 132 #22 a,b,c,d,f  (and or not) --remember N flag is set (1) when MSB is 1.

Make A and B due on Friday.  Pep 8.01's flags aren't functioning! (always =1).  Pep800B8's flags work as they're supposed to, but the saving is a mess.  So to do this properly you have to:  Load it into pep801, debug if needed, save,  get the Assembler listing. Trace, recording everything but the flags.  Load the same program into pep800B8, and trace it there to watch an record the flags.  (pull down the edge of the pane to find the single step button.)
A) Tracing:  (Get templates ) a) Trace Program A below, Memory trace and Program trace, including flags (on the computer).
     b) Note how Ldbyte and Stbyte affect the registers, the memory locations, and the flags.
   Hand in the trace.
     c)Underline instructions where N or Z were "set" (turned to 1) and check that each corresponds to:
                 a number with 1 in the leftmost (Most significant) bit; a Zero number.
     d) The Add instruction adds 2 numbers with F's in the leftmost hex digit.  If you do the binary addition, you'll get a carry out of the left side.  Does the C get set (= 1) at this instruction?  Circle the C value for this instruction.

B) a) Make a program that will: study the behavior of  ASL and ASR, including the flags.  (ASR shifts right: fills in on the left with 0 if MSB was 0, with 1 if MSB was 1.  Check both kinds of numbers)
     b) And study the logical ops And Or Not.  (All in the same program? or not; your choice).   Check that the ops and the flags operate as they are supposed to.
     Hand in:  What numbers you operated on, the resulting numbers, and the resulting flags.

C) Create a byte size Mask (on paper only) and an operation that will (different for each part) 
         a) change ASCII  A to a (or any capital to its small letter)
         b) change a to A (or any small to its capital)
         c) change "3 " the character  to  3 the number
         d) change 3 the number to "3 " the character
D)  Load or Type in program 5.7 (add 3 and 5, p. 197), run it and check that the output is as shown.  Change the program so that the two numbers to be added are 2 and 4.  Check that the output is 6.  Now change it so that the two numbers to be added are 6 and 7.  What is the output now?  Explain why the program gave this output.

E) .BLOCK reserves memory and puts 0's in it. When you declare a variable in C++, does the program just reserve a memory space for it or does it give it a default value (e.g. 0) if you don't give it a value?  Write a C++ program to find out! (hint: just declare the variable, and then output it, without assigning anything to it.) What value did you get for an INT variable that you don't assign a value to?

F)  Program F below will make a preliminary investigation into how negative numbers are handled in the PEP8.  It should: Load the number 1 (0001H  ) into A.  Then repeatedly subtract 1 from it.
   a) The program is not quite finished; the address for the memory location  of 0001H is not determined.  Determine the address and finish writing the program.  (Hint:  The from-scratch way is to count by hand how many bytes are used by the program instructions,  and thus find out where the .Word will be.  Or, the lazy way, you can also put in 00FF or something for the address, knowing it's wrong.  Assemble the program and look at the Assembler Listing to find the address you want.  Go back and change the address to the correct one. Find is under the Edit menu in PEP8 . 
    b) Run the program and  write down the hex and decimal results.
         Hand in a table:  Decimal    Hex  showing what the Hex equivalent of the Decimal negative numbers is.
                                       1          0001
                                       0          0000
                                     -1           ?       etc. to -7

Also Program 1 is due at class time:   Email me the source code.  Hand in Assembler Listing & output,  with the same cover stuff as for a C++ program:  Running? remaining bugsHelp from?


NOTES:
Last HW: 
ASL does the "same" thing as multiplying by 2; adding a number to itself. 
Abstractly: binary number anan-1...a2a1a0 = an2n + an-12n-1+...+a222+a121+a020, where the a's are all either 0 or 1.  Multiplying by 2 raises the power of all the 2i 's by one, so a0 is now the coefficient of 21.  The new coefficient of 20 is 0, and all the other bits have shifted left; the new number in binary is anan-1...a2a1a00.  One bit longer than before.

Labeling bits in a word:  a15a14....anan-1...a2a1a0   USUALLY the bits are labeled from "right to left":  0--Least significant (a0 ),  to 15--Most significant (a15).  So the bit label is the power of 2 that that place represents.  Warford, bless his heart, labels them backward:  The most significant (leftmost) byte is bits 0...7, the least significant (rightmost) is bits 8-15.

Flags: Op just executed resulted in a number that is:  (1 for true, 0 for false) 
    N negative.  (Most Significant Bit MSB =1.  Explained later, sec. 3.2.  Acts only on Word in register)
    Z zero (word is zero)
    V oVerflow (for arithmetic with signed numbers.  More later)
    C carry--a carry out of the MSB when adding, other places where a bit moves off either end of the "cell", (or is borrowed)  Reread p.94  (Appendix A7-8 give all ops, including flag actions, in RTL

Byte ops: only 2: LdbyteR, StbyteMainly for moving characters around.  pp. 163-4.  Load a byte only into the lower byte in the register (the upper byte stays what it was), Store the lower byte from the register to the memory byte of the operand.
.Block 5  reserves 5 bytes of memory (for you to store things in).  Pep8 puts in 00's; not all assemblers do.  Cf. declaring a variable in C++.  pp. 195-6
Tracing:  Get templates
    Trace program below, check out new ops and flags.

Logical Ops ("bitwise"--no carrys or borrows to neighboring bits)   1=true, 0=false  and usual truth tables work.  Read NOT p.96, AND OR &shifts 3.3 pp.105-109
   NOTR: (unary)   "one's complement"  0-->1,  1-->0.
   ANDR  oprndspec:  p AND q =1 (true) only when both p and q are 1, otherwise =0  (think p*q with no carries).
   ORR  oprndspec:  p OR q =1 (true)  when one or both of p and q are 1,   =0 only if both are 0 (think p+q with no carries).

Masking:  To force certain bits to become 0 or 1, leaving others unchanged.  (A<-->a,   "3"<-->3)

Force to 0 "clear"
Force to 1 "set"
0<--b AND 0 
b<--b AND 1
1<--b OR 1
b<--b OR 0
e.g. X AND 00001111 will give 0's in left 4 bits, preserve rest.  X OR 00001111 will give 1's in right 4 bits, preserve rest

Look at ops:  2-argument ops all do something like r<--r OP operand, and operand is in memory.   Design choice.
   No move directly from X to A. No Add X to A. No add # in memory location 1 directly to # in memory location 2.  Etc.  

We are working our way up to being able to proceed through 5.1 and on.


Notes on PEP8 and changing applications: 
--Pep can't stand an empty line; crashes.  Use ; to mark an empty line.
--The minimal program is .END.  You can comment out everything else, uncommenting one at a time to find what line crashes. Rewrite the neighbor lines.. 
The <br> and <p> codes in HTML cause conflicts with PEP.   The <p> comes into PEP as some character that crashes PEP.  But if you copy and paste from PEP to HTML (at least the Firefox editor) it inserts <p>s for the end of line marker; if you copy back to PEP it crashes.
OTOH Word works fairly smoothly.  Copying back and forth from PEP works.  Pasting into HTML (paste without formatting) works fairly well. 


;Program A to investigate Status Registers Z, C, N, Ldbyt &Stbyt HW Day5
;Sievers
;Sept 4, 2006
;This program uses Ldbyt, .block new
LDa 0x0016, d ;loads zeros into reg. A. Check flags.
LDa 0x0018, d ;loads F0f0 into A
ldbytea 0x0016,d ; loads a 00 byte into a
ldbytea 0x001d,d ;loads a not-0 byte into a (01)
stbytea 0x001a,d ; stores a byte from a to the memory saved for it
Adda 0x0018,d ; adds F0f0 to a
Suba 0x001e,d ; subtracts E0f1 from a
STOP ; STOP must be at the end of the instructions for the cpu to run
.word 0x0000
.word 0xF0F0
.BLOCK 2 ; reserves a block of "empty" space
.word 0x0001
.word 0xe0F1
.END ;END tells assembler we're done.




;Program F to investigate Negative numbers, HW Day 5
;Sievers  (Put YOUR NAME here)
;Sept 4, 2006
;This program repeatedly subtracts 1, starting from 1, so should find 0, -1, -2, etc.
LDa     0x00FF, d     ;should loads 0001 into reg. A.  It doesn't now. Fix it. (& change this comment)
Suba     0x00FF, d     ;should subtract 0001 from reg. A.  Fix it.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
Suba     0x00FF, d     ;should subtract 0001 from reg. A.
STOP            ; STOP must be at the end of the instructions for the cpu to run
.word 0x0001
.END                ;END tells assembler we're done.

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