CS225,  Fall 2006,  Monday Sept. 11, Day 8  Hit reload!After class

Read  3.2, pp. 95-104  (how to do negative numbers)   Today, computation and interpretation.  Next time, Carry and Overflow bits.
5.2, pp200-204, BR, DECO and DECI.  We'll return to Immediate addressing soon but not just now.

Quiz Wednesday, closed book:
-- How many different numbers can be represented using an x-bit cell?
-- Hex<-->binary conversion
-- Binary--> decimal conversion  (unsigned only, no more than 8 bits)
-- Decimal-->binary conversion (unsigned only, no more than 8 bits)
--Making a mask to force a certain bit or bits, leaving others unchanged.
--Translating an instruction from binary to Assembly Language (given Figures A7, A8)


HW Day 8, Due Wednesday Day 9 Link to consolidated Old HW. But do Day 8's first!
Textbook p. 136 (do the conversions by hand, showing your work) 15 all, 16 all, 17 all , 18 all, 21. 
A. What are the largest and smallest (most negative) integers which can be expressed with 16 bits (2 bytes), using 2's complement representation? (i.e. the largest and smallest PEP can handle?)
B.  Run the program below,  entering -1, 287, 32767, -32768.  For each, give
decimal number | hex for it | binary for it|  Not (binary) |Not (hex)|  Neg (binary)| Neg (hex)| negative in decimal.
(You can read all these off from the stepping program or memory, except for the binary versions, which you'll have to translate from hex.)
C.  Collect some information on what DECI does if you give it an out-of-range input.  (Use the program below which outputs the decimal number you just inputted.)

Representing Signed (positive and negative) integers in the computer:   -a = ?

   Desire:   a + (-a) = 0

Your PEP program counting down:  0001, 0000, FFFF, FFFE, ....
 00 01 + FF FF = 1 00 00   00 02 + FF FE = 1 00 00  (change to binary and try it.  Carry bit in red. )  It works; all we have to do is ignore the Carry.

 Assume for simplicity we have a 4-bit cell computer,  16 possible values:
Can count from 0 to 15, in an unsigned integer system.
For signed, desire every number to have its negative, as far as possible.  Since 0 should = -0 , there will be an odd one left over.

bin    hex & dec     bin    hex  unsigned dec  signed dec
0000  = 0             1000 =  8 =  8           =  -8
0001  = 1             1001 =  9 =  9           =  -7
0010  = 2             1010 = A  = 10         =  -6
0011  = 3             1011  = B = 11         =  -5      
0100  = 4             1100  = C = 12         -4       
0101  = 5             1101 = D = 13          =  -3
0110  = 6             1110 = E = 14           -2
0111 = 7              1111 = F = 15           -1    Note the negative interpretation = unsigned interpretation -16 (24)

-0 should be 0.  -1 should be 1111 since 0001+1111 = 0000 (with a carry into somewhere else).
-2 should be  1110 since 0010+ 1110 = 0000 (with carry).  Etc.
1000 would have to represent both 8 and -8 with this system.  Arbitrarily decide on -8: then a negative number can be recognized by the leading 1

With 4 bits, signed numbers go from -8 up to +7.

Notice to find the negative of a number, you have to know how many bits are in the representation!
8 bits: NEG(2) = NEG (0000 0010) =  (1111 1110)
No matter how many bits:  (example, 8 bits)  Most negative number:  1000 0000 (leading 1, rest 0's), -1 = 1111 1111 (all 1's), 0 = 0000 0000, Biggest positive number: 0111 1111 (leading 0, rest 1's)

Negative of a = "two's complement" of a = (one's complement of a) +1 = Not (a) +1
NEG(a) = NOT(a) +1.  Try it.
NEG(NEG(a)) = a

Decimal to binary, for negative value -a: Find binary for positive a, then find two's complement
  -9D, six-bit cell:   +9D = 00 1001.  NOT(00 1001) = 11 0110.  Add 1 to get Neg = 11 0111= -9D.   Check.

Binary to decimal, for negative a:  (has leading 1. )  Find negative of binary;  this is positive.  Convert to decimal.  Put minus sign in front of it.
Neg(10 1100) = 01 0011 +1 =01 0100 = 16 + 4 = 20.  answer -20
(Optional Short cut:  The 0's of the negative binary number will become the 1's of the one's complement.  Sum the powers of 2 at which the original binary negative has 0's.  This gives the one's complement in decimal.  Add 1 to get the two's complement.  Apply the minus sign.   10 1100--> 16+  2+1= 19.  Add 1, get 20.  answer -20) 
Go to program below.
Note:  A 16 bit "number" in PEP can be interpreted as: an unsigned integer (the "dec"s in the CPU), a signed integer (assumption of DECO), or two characters (assumption of CHARO).  And others....

- - - - - - - - - - - - - - - - - - - - - - - -
In an n-bit cell, the number of different items which can be expressed is 2n If you keep adding 1, the numbers "roll over" from 1111 ...1111 to 0000...0000.  In effect, you are doing arithmetic mod 2n.  (The unsigned integers are the "remainders" after dividing by 2n . )  The text uses the number line to demonstrate this: here's the "clock" version.  n = 4, 2n = 16.
All the numbers in the same place ( they are called congruent mod 16) differ from one another by (a multiple of) 16.  So you can find directly from the decimal representation of a number what its representation will be in an n-bit cell, in unsigned or signed form, by subtracting (or adding) 2n till you get in the correct range.
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /

Procedures DECI and DECO (built-in) input and output decimal numbers, including - signs.   Not part of machine's instruction set, but programs built out of the instruction set and provided for our convenience--"Traps"
In the step trace, they appear as RETTR (return from Trap)

BR  address:  simply loads address into the PC: the program jumps or BRanches to that new place in the program.  Here: Put data up front, Branch around it; then you can mess with program without messing up data addresses.

- - - -  - - - - - - - - - - - - - - - - - - - - -
;Program which uses Deci and Deco, and BR, to examine negatives
BR      0x000A     ; branch around data!  PC<--000A
; DATA up front
.word   0x0001 ; 1 for adding to 1's complement
.block    2     ; 2 bytes= a word space for the decimal input
.block    2    ;  a word space for the negative of the input.
.byte     0x0A     ; new line
; Now INSTRUCTIONS
deci          0x0005   ,d  ; Input a decimal number This instruction is at address 000A
deco         0x0005, d   ;output the same decimal number
charo        0x0009, d  ;outputs new line
lda            0x0005 ,d   ; load into Accumulator
NotA                          ;1's complement
AddA        0x0003, d   ; Add 1 for 2's complement
Sta            0x0007,d   ;Store the negative
deco          0x0007,d                     ; output the negative
STOP
.END
- - - - Same program Below without formatting, for pasting into PEP-------

;Program which uses Deci and Deco, and BR to examine negatives
BR      0x000A     ; branch around data!  PC<--000A
; DATA up front
.word   0x0001 ; 1 for adding to 1's complement
.block    2     ; 2 bytes= a word space for the decimal input
.block    2    ;  a word space for the negative of the input.
.byte     0x0A     ; new line
; Now INSTRUCTIONS
deci          0x0005   ,d  ; Input a decimal number This instruction is at address 000A
deco         0x0005, d   ;output the same decimal number
charo        0x0009, d  ; new line
lda            0x0005 ,d   ; load into Accumulator
NotA                          ;1's complement
AddA        0x0003, d   ; Add 1 for 2's complement
Sta            0x0007,d   ;Store negative
deco          0x0007,d                     ; output the negative
STOP
.END


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