CS225, Fall 2006, Friday Sept. 15, Day 10 Hit
reload! After class
Read 3.2, pp. 95-104 (how to do negative
numbers)
Today, more interpretation, Carry and Overflow bits.
Reread ASL and ASR, pp, 105-9, following the discussion of the
flags.
I discussed a little in class; you're
responsible for it.
Remaining in Ch. 3: 3.5 floating point (Complicated.
Interesting? Optional. ) 3.6
Read now for overview and flavor, not much detail.
(Re)Read 5.1, picking up any leftovers. Read 5.2, all.
New today: immediate addressing, STRO .
HW
Day 10, Due Monday
Day 11
Signed integers: A. p. 103-4, Examples
3.17 (and the last one of example 3.18. The first two are repeats
from 3.17).
Convert each of these binary numbers into an unsigned decimal number
and
into a signed decimal number. Observe when the sums "go wrong"
and
check that the C and V flags work correctly. Note a 6-bit cell
can
represent 64 different numbers.
For example, look at the last addition of
Ex.
3.17:
convert binary values to unsigned: 38+ 34=8
Unsigned:
38+34
should = 72, too large. Answer 8 = 72 mod 64, C flag is set.
Signed: -26
+
-30 should = -56, out of range. Answer 8 = -56 mod 64, V flag is
set.
Textbook p. 136, 19c,f , 20
d,e.
Also translate
each binary number to its unsigned and signed decimal equivalent.
Sec.. 5.2: Immediate
addressing, etc.
Textbook p. 215, 12 a, b
(how not to
write code!) ; For (a), check the back of
the book and make sure how to do it. Do (b) by hand and hand in the
object code you found. BTW,
the book always uses 4 digits after 0x, 0x000D,
but PEP8 can read 0xD. The 4 digits are a good reminder when
you're
working with a 2-byte word, however.
D. Rewrite the PEP program from Day 8 ("Deci,
Deco, and negatives"), to use immediate addressing for adding 1
and for Linefeed.
Put in some immediate Charo output so your output (for
input of
29)
reads
Neg of 29
= -29
E. Rewrite that same program, substituting STRO for your immediate Charo output.
- - - - - - - - - - - - - - -
C++ Programs are
below. Begin now, hand in Wednesday.
A. Run program A. (If you're not
using a PC, tell me.) a) Write down the size of a Long
integer, and
its min and max values. b) Change all the Longs to Int,
repeat.
c)&d) Repeat for Short and Char. e) Repeat for Unsigned
Short.
Note that the assignment 66 to a char variable works fine.
f) Which size integer corresponds to the PEP integers?
B. Run program B. a) Write down the
largest unsigned int. b)Explain why the outputs for v and w
each
make sense.
C. Run program C. a) Examine the output.
Write down the first signed integer that is in overflow. Write
down
the first unsigned integer that is in overflow. Notice that the
numbers are the same for signed and unsigned some of the
time. (when?)
b) Rewrite the program to use short
integers, and to
calculate powers of 5. Again, to calculate powers of 6.
What is the
most noticeable difference? Explain it as best you can.
c) What is the moral of program C for your future C++ programs?
Notes: Signed
numbers Day 9 C and V
flags,
"overflow" ("wraparound", according to Hennefeld Baker
Burchard, Using C++)
Immediate addressing Day 9
STRO 0x0002, d pp.
204-5. String output routine. String is in memory as .Ascii
"whatever\x00", that is, a sequence of bytes with a final
sentinel of 00. (That is, an Array.) Address is address of first
character. Routine is what?
Just: while (byte <> 00) {charo (byte); go to next byte}
+ + + + + + + + + + + + + + + + + +
C, the precursor to C++,
was originally used to be just a step
up from Assembly Language, and was then and is now used to do much "low
level" programming as well as the "high level" class and procedure work
you did in 131-2.
Some "lower level" C++: ( ref. Hennefeld, Baker,
Burchard,
Using C++. Also an older text, . Dale/Weems/Headington pp.
350-365 )
Built-in Integer data types: short, int, long, char
(acts like integer except for output). All signed.
To get Unsigned
versions, use 2 words: unsigned int
a=6; for example. Same data storage and computation,
just different interpretation of input/output.
Constants: You can assign numbers using Hex:
a=0xCF25; unsigned short a=65535u; (to use a
number which is out of range as a signed number but in as unsigned)
.
You can assign Characters using decimal or hex ASCII:
char a = 66; a=0x42. [What happens if you use a= -66?]
You can
use "escape sequences" to signal non-character ASCII: \x42
is the ASCII character expressed in hex as 42.
char a = '\x42';
cout<< " \x42 is the same as B \n" ;
Some have also convenient letter codes: \n is LF=\x0A, \b
is Backspace [does it work?], \t is tab. (Others HBB p. 604.)
Programs let you explore the creaky (unstructured) underpinnings
in C++, do some "bad" things just to see if they work.
- - - - - - - - - - - - - - -
= = = = = =
= = = = = = = = = = = = = =
//C++ programs
//- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Program A, Sizes of C++ integers, CS225 Day 10
// climits (cf. HBB p. 51)
// defines constants LONG_MAX,
LONG_MIN,
// INT_MAX, INT_MIN, SHRT_MAX, SHRT_MIN, CHAR_MAX,
CHAR_MIN
// and the same for unsigned, ULONG_MAX, etc.
(No MIN's for unsigned. Why not?)
// sizeof is a built in function that tells the
storage space
size of any variable or constant.
#include <iostream>
#include <climits>
using namespace std;
int main()
{
cout << "Min. char: " << CHAR_MIN
<< endl;
cout << "Min. long: " << LONG_MIN
<< endl;
cout << "Max. long: " << LONG_MAX
<< endl;
long a = 66;
cout<<"a contains "<<a <<endl;
cout<<sizeof a<<" byte(s)"<<endl;
cout<< "long is " <<sizeof(long)<<" bytes \n";
return 0;
}
//- - - - - - - - - - - - - - - - - - - - - - - - - - -
//Program B, Investigate unsigned integers in C++, with some
alternative number entries, Day 10
#include <iostream.h>
int main()
{
int i = -1; // int is signed.
//Other integer types are
short, long.
cout<<i<<endl;
unsigned int u;
//u will be the biggest
unsigned value, 1 less than the # of possibles.
u = i;
// type "casting" takes place here, without notice
cout << u << " biggest unsigned INT\n";
unsigned int v;
v = 0xFFFF; // zero 0x marks hex input
cout<<v<<"= FFFF hex" <<endl;
unsigned int w;
w = 65536u; // u marks unsigned input, decimal
w= w*w;
cout<<w<<"=65536 squared"<<endl;
return 0;
}
//- - - - - - - - - - - - - - - - - - - - - - - - -
//Program C, Powers of 129, Day 10
#include
<iostream.h>
int main()
{
int i = 1; // int is
signed.
cout<<i<< "is signed ";
unsigned int u;
u = 1;
cout << u << " is unsigned \n";
for (int loop =1; loop <=20; loop++)
{
i=i*129;
u=u*129;
cout<<i<< "is
signed ";
cout << "\t"<< u
<< " is unsigned \n";
}
return 0;
}
- - - - - - - - - - - - - -
- - - - - - - - - -
This page belongs to Sally Sievers who is solely
responsible
for its content. Please see our statement
of responsibility.