CS225,  Fall 2006,  Friday Sept 22, Day 13  Hit reload! .After class

Read  Flow of control: Ch.2.2 p. 40-41(figure 2.9 if-then,else) & Ch.6.2 pp. 241-47, ahead  (postpone switch)while, do while, for loops, pp. 42-46.

HW Day 13, Due Monday Day 14
Re-assigned, Day 19 A.  Run and trace Program A, 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.

Need more  templates.?  Link is now also on 225 main page, bottom.
Re-assigned, Day 18 B.  I wrote today's programs  so that the numbers were stored in an absolute address (the equivalent of a global variable in C++). 
             Modify Program B so that the numbers are stored on the user stack (the equivalent of a local variable).  (Day 12
Hand in or email  code, hand in listing.

C.
  a) A very simple PEP program, no ifs, no loops, you may use  "global" type "variables".  Get a number from input, and multiply it by 8; output that.
         Hint:  8 is a power of 2.  Multiplying by 2 can be done how?
      b)  Modify your program of part a to do integer division by 8.  For example, 16/8 = 2, 19/8 = 2, 26/8 = 3
        Hand in or email  code, hand in listings.

Re-assigned Day 19 Postpone 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.

Re-assigned Day 19: Program 4, assigned day 13, Fri. Sept 22.  Due day 16,  Friday Sept 27.  In program C 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.  (You may ask me to look at your algorithm)  Hand in or email  code, hand in listing, hand in verbal description of the algorithm unless it's in clear comments in the program..

HW  11 comments:  In Microsoft C++ and in Oren's C++ on Macintosh, int is 32 bits (8 hex digits), = long.  Short is 16 bits (4 hex digits) =PEP word size.  
         Program B:  w is Unsigned Int:  32 bits.  w=65536D fits comfortably.  But w=216 , so w*w = 232  = 1 00 00 00 00H = 0 in a 32 bit word.
Output of powers of 6? link

D. a) We now know C++  will do logical ops on any kind of integers including Char (you may get funny symbols for the low numbers, originally developed for the IBM PC. Or nothing.  Microsoft doesn't put anything in the lower numbers, but does use the higher ones. )
Some ASCII links:
http://www.csgnetwork.com/asciiextset.html 
http://www.fact-index.com/e/ex/extended_ascii.html  An article about Ascii extensions, historically
http://www.jimprice.com/jim-asc.htm  Scroll down to "IBM PC Extended ASCII Display Characters" to see the two most common versions.

b) Mask to change lower case to upper case:  & m,
    where m is 1101 1111, char m,  can be assigned  by:
     m=0xDF,  or  m=223 (decimal for DF),
    m =(0)101 1111 works too since the MSBit of a letter is always 0.  So m =0x5F, or m=95, or m='_'  (check the Ascii table)

c) C++ shift right:  Appears to use ASR if signed type (-6--> -3) but LSR if unsigned (65530--> 32765)

textp. 229 #13:  Code:  70 00 05
                                    00 09
                                    F1 00 03
                                     00


Notes:
Recall:  ASR
shifts 1 bit right.  Integer division by 2.  "Lost bit" (remainder) goes to Carry.  Retains sign (fills in on left with 0 or 1)
ASL shifts 1 bit left.  Multiplication by 2.   Carry flags a result out of range.
    Multiplication by other numbers?  Work toward general integer multiplication.  By 4? by 8 ( for hw.)  By 3??  5?  6?

Flow of control:  so far, Unconditional branch   BR  target.  Replaces PC so next instruction is at location target.
    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


Patterns for IF THEN ELSE    IF (condition) then Sequence T ELSE  Sequence F
Computation affecting Flag
   BR  If         ; Branch if flag/condition TRUE
   "Else" body
      Sequence F
    BR EndIFELS   ;Unconditional branch past "If"
If:  "If" body:
         Sequence T;
EndIFELS:  post-condition instructions

This maintains the "If", but the else and the if bodies are reversed.  Warford uses another version:  Branch if the condition is FALSE.
SEE HANDOUT
Note that the numerical branches (using Z or N) all have corresponding negation branches, so you can branch if the condition is false or if it's true, no problem.  But there is no "Branch if C = 0", no "Branch if V = 0" so you don't have a choice for those.

Examples on handout.  (Paste-able below)

Start here Monday:
Branches must happen as soon as the flag is set.  E.g. insert
    Adda 0x0000, i  ; between Add and BRV.  Does nothing,  but messes up flags.
(But:  Flags stay,  if not affected by next op.
     Note Store and Output instructions don't affect flags.  Could put between ADD and BRV.  But dangerous!)

Branches are used very often to change flow if one number is 0, or <, >, etc. than another.  How?
    X<Y = = X-Y < 0   Branch if X<Y = = Branch if X-Y < 0
        LdA  X, d
        SubA  Y, d
        BRLT target ;  Branch if A register (X) is less than number subtracted (Y)
 You don't care about the subtraction?  Use the Compare instruction!
        LdA  X, d
        CPA  Y, d does the subtraction and sets flags,  doesn't store result in A!
        BRLT target

=== === === === === === === === === ===
;Program A to see if number is even or odd, day 13
            BR  main;
num:   .block 2;
linef:     .equate 0x000A;
;
main:  deci  num, d
          lda num, d;
           deco num, d; ; output preliminary
            charo ':', i
             charo ' ', i ; space
   ;Calculate Condition          If C = 1, number is odd, If C = 0, number is even.
             ASRA ;                   shifts right, stores "1-bit"  in C:    AFFECTS C FLAG
             BRC  ODD;      IF C= 1, branch; otherwise proceed to next instruction
    ; if not odd                 output even marker  {
               charo 'E', i ;                            }
             BR endIFELS;     unconditional jump over else
    ; if odd (branched on flag)                          {
ODD:         charo 'O', i ; 
;                                                                   }
endIFELS:  charo linef, i;
STOP;
.END



 
;ProgramB to do error check on signed computation, day 13
            BR  main;
num1:   .block 2;
num2:   .block 2;
sum:     .block 2;
linef:     .equate 0x0A;
;
main:  deci  num1, d
          deci  num2, d;
           lda num1, d;
           deco num1, d; ; output preliminary
            charo '+', i
            deco num2, d;
            charo '=', i
           ADDA num2, d;   adds num2 to num1, stores in A  AFFECTS V FLAG
            BRV  error;      IF overflow, do error message
; if not error                    process sum
               sta sum, d;
               deco sum, d;
           BR endIFELS;     jump over else
; if error
error:     charo 'N', i
              charo 'O', i
              charo 'T', i
              sta sum, d;
               deco sum, d;
endIFELS:  charo linef, i;
STOP;
.END

 = = = = = = = =

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