Jump to content

Robert M

Members
  • Content Count

    1,533
  • Joined

  • Last visited

Posts posted by Robert M


  1. BTW, the trademark on the word "Wolfenstein" for video games expired in 1996.   Of course the recent titles including "Wolfenstein" from id software are trademarked, but the word "Wolfenstein" by itself is not.  So it should be okay to use "Wolfenstein" as a title for this hack without raising legal hell.

     

    Cheers

     

    Good news!! How did you confirm this?

     

    Go here:

    http://www.uspto.gov/

     

    Search for "Wolfenstein" in the Trademarks database.


  2. BTW, the trademark on the word "Wolfenstein" for video games expired in 1996. Of course the recent titles including "Wolfenstein" from id software are trademarked, but the word "Wolfenstein" by itself is not. So it should be okay to use "Wolfenstein" as a title for this hack without raising legal hell.

     

    Cheers


  3. I have managed to disassemble some more of the venture code. Here is my latest work. The attached file is zipped so you need to unzip it. Winzip for windows users, and gzip for UNIX and Linux users. Since the file originated on a Unix system, I recommend that Windows users after unzipping the file should open it in WordPad and save as a .txt file. That will fix any carriage return issues with the file.

     

    Cheers!

    venture.s.gz


  4. Yoink!  :D   8)

     

     

    Thank you very much sir, I owe you $4.00 ! You paid for shipping twice. :)

     

     

    That's kind of you. If you want to just put a personal check in the box that's fine by me. I had to pay for the power supply immediately according to ebay if I used the but it now option.

     

    Thanks, for the stuff. I have been shopping for a supercharger for some time now. Getting the thing already modded is a big plus!

     

    Cheers!


  5. Lesson 5 - Excercise Answers: 
    
    
    
    Exercises: 
    
    
    
    1.  Perform the following binary additions of 2's complement numbers.  Express the final result in 8 bits and indicate whether UNDERFLOW or OVERFLOW occurred. 
    
    
    
    a.  %10010100 
    
     + %01101000
    
       ---------
    
       %11111100 -> No overflow or underflow.
    
    
    
    
    
    b.
    
        11111    -> carry the 2.
    
       %00110100 
    
     + %01101111 
    
       ---------
    
       %10100011 -> Overflow occurred!  Adding 2 positive numbers resulted in a negative.
    
    
    
    
    
    c.  
    
       11111     -> carry the 2.
    
       %10011100
    
     + %11111000
    
       --------- 
    
       %10010100 -> No Underflow.
    
    
    
    
    
    d.  11 11111  -> Carry the 2.
    
       %01010011 
    
     + %11011101
    
       ---------
    
       %00110001 -> No overflow or underflow. 
    
    
    
    
    
    2.  Perform the following binary subtractions of 2's complement numbers.  Express the final result in 8 bits and indicate whether UNDERFLOW or OVERFLOW occurred. 
    
    
    
    a.            
    
       %10110100 
    
     - %01001000
    
     -  1  1     -> borrow 2
    
       ---------
    
       %01001100 -> Underflow occurred!  (-) minus (+) should not equal a (+)
    
    
    
    b.  
    
       %00110100 
    
     - %01101011
    
     - 11  1 11  -> borrow 2
    
       ---------
    
       %11001001 -> No overflow or underflow.
    
    
    
    
    
    c.  
    
       %10111100 
    
     - %11011010
    
     - 11    1   -> borrow 2
    
       ---------
    
       %11100010 -> No overflow or underflow.
    
    
    
    
    
    d.  
    
       %00010111 
    
     - %11010111 
    
     - 11        -> borrow 2
    
       ---------
    
       %01000000 -> No overflow or underflow.
    
    
    
    
    
    3.  Convert the following 8-bit 2's complement numbers to their negative equivalent 
    
    
    
    a.  %00010010 
    
        ||||||||
    
        vvvvvvvv 
    
       %11101101 -> invert all bits.
    
     +         1 -> add 1.
    
       ---------
    
       %11101110
    
    
    
    b.  %01000101 = %10111011
    
    
    
    c.  %01111111 = %10000001 
    
    
    
    d.  %00000000
    
        ||||||||
    
        vvvvvvvv
    
       %11111111
    
     +         1
    
       ---------
    
       %00000000  -> Pretty cool hey!  There is no negative zero in two's complement! 
    
    
    
    
    
    4.  Convert the following 8-bit 2's complement numbers to their positive equivalent 
    
    
    
    a.  %10110110
    
        ||||||||
    
        vvvvvvvv
    
       %01001001
    
     +         1 
    
       ---------
    
       %01001010
    
    
    
    b.  %11001101 = %00110011 
    
        
    
    c.  %11111111 = %00000001
    
    
    
    d.  %10000000 = %10000000 -> Note this is the one case that doesn't
    
                                work because there is no way to show
    
                                positive 128 in 8 bits using two's 
    
                                complement format.  
    
    
    
    
    
    5.  Provide a description of what would constitute OVERFLOW and UNDERFLOW for addition and subtraction involving 16-bit (word) 2's-complement numbers. 
    
    
    
    ANSWER:  The number of bits used to represent a two's complement number determines the range of values that can be represented.  For 8-bit numbers the range is from +127 to -128.   For 16-bit numbers the range is from 32767 to -32768.  Therefore overflow occurs in math with 16 bit numbers if the result of the operation is larger than 32767.  Underflow occurs if the result is less than -32768.
    
    
    
    


  6. Contest ends on the 15th. Is anyone working on something that will require an extension of the deadline?

     

    Yes please. I am still working to understand the code. I am putting in a couple hours a day, but I need more time. How about extending to the 1st of Feb?

     

    I have attached my partial disassembly so others can play around with it. All The graphics data and colors are labeled.

     

    Cheers!

    venture.txt.gz


  7. I, too, have been looking at the code, and adding comments, but it's been slow going so far.

     

    My approach idea is to locate the routine that doesn't allow you to reenter a room once you've taken its treasure. Then, set up the opposite, where you are not allowed to enter the final room( probably made with the remaining PF graphics not currently shaped into a chamber. Once you've nabbed all the treasures, then the final room would allow access, and contain the Nazi plans!  :)  

     

    That sounds reasonable. I haven't found where it tracks if you have completed a room yet or not. Maybe you could impose an order on all the rooms so that the player has to complete the rooms in a specific order. Only one room is unlocked at a time. If the locks are single bits in a byte, assuming %0000 means all rooms unlocked and %1111 means all rooms locked. Then, intialize to %1110. Then as each room is completed set the carry bit and perform a bit shift to lock the previous room and unlock the next.

     

    %1110 Room 1 unlocked

    %1101 Room 2 unlocked

    %1011 Room 3 unlocked

    %0111 Room 4 unlocked

    %1111 All rooms completed

     

    Also, the points of entry to each room need to be located in the data, in order to change their locations. If neo wants  new room shapes, we'd have to know how to signal the new entry ways when the ball( dot ) enters a room.

     

    Yes, I haven't found that data yet either. I have found the data that tells the starting position for each sprite in each room.

     

    The positions are packed into single bytes. The upper nybble is the starting X position. The lower nybble is the starting Y position. You have to mulitply the X nybble by 10 to get the true value, and multiply the Y nybble by 5.

     

    @Robert M.: adding more chamber rooms sounds interesting, and may work better in the long run, than my plan above. Hmmm.

     

    The big rooms consume the a lot of ROM space in both data and code, so replacing them with other zoom-in rooms will free up considerable ROM space. (I hope).

     

    Also, I agree that using the current Wolfenstein label may cause problems. Perhaps neo would be interested if I submitted some alternate artwork... :ponder:  

     

    I smell a label contest... :ponder:


  8. OKay, I started looking over the code. It seems to me that an end screen hack is very doable. Its going to take some big changes. I am thinking of removing the 2 rooms where you are just a dot, and adding about 10 - 15 of the "zoomed-in" rooms. One of which code be an end screen. Walking out a door in a room leads directly to another zoomed room.

     

    I noticed you are using the trademarked Wolfenstein logo on your label mockup. Is that wise? I would hate to see the legal heads from id shutting down this project.

     

    Cheers!


  9. ...
    
    
    
    
    
    
    
    4.  Convert %1000010100011110 to decimal
    
                |    | |   ||||
    
                |    | |   |||+---- 1 * 2^1 = 2
    
                |    | |   ||+----- 1 * 2^2 = 4
    
                |    | |   |+------ 1 * 2^3 = 8
    
                |    | |   +------- 1 * 2^4 = 16
    
                |    | +----------- 1 * 2^8 = 256
    
                |    +------------- 1 * 2^10 = 1024
    
                +------------------ 1 * 2^15 = 32768
    
    
    
         2+4+8+16+1024+32768 =  33822 decimal
    
    
    
    
    
    

     

    Did you mean: 2+4+8+16+256+1024+32768 = 34068 decimal? ;)

     

    Keep up the lessons! I'm stretching the left-side of my brain passed its limits now! 8)

     

     

    Yes, Thanks for catching that. I will edit the original post.


  10. Sorry it took me so long to post these answers. :dunce:

     

    ANSWERS TO EXERCISES:
    
    1.  Convert 213 to binary
    
    
    
    213 / 2 = 106 with a remainder of 1 ---------+
    
    106 / 2 = 53  with a remainder of 0 --------+|
    
    53 / 2  = 26  with a remainder of 1 -------+||
    
    26 / 2  = 13  with a remainder of 0 ------+|||
    
    13 / 2  = 6   with a remainder of 1 -----+||||
    
    6 / 2   = 3   with a remainder of 0 ----+|||||
    
    3 / 2   = 1   with a remainder of 1 ---+||||||
    
    1 / 2   = 0   with a remainder of 1 --+|||||||
    
                                        %11010101 binary = 213 decimal.
    
    
    
    2.  Convert %00101100 to decimal
    
    
    
      %00101100
    
       ||||||||
    
       |||||||+--- 0 * 2^0 = 0 * 1   =   0
    
       ||||||+---- 0 * 2^1 = 0 * 2   =   0
    
       |||||+----- 1 * 2^2 = 1 * 4   =   4
    
       ||||+------ 1 * 2^3 = 1 * 8   =   8
    
       |||+------- 0 * 2^4 = 0 * 16 =   0
    
       ||+-------- 1 * 2^5 = 1 * 32  =  32
    
       |+--------- 0           
    
       +---------- 0
    
    
    
         0 + 0 + 4 + 8 + 0 + 32 + 0 + 0 = 44 decimal.
    
    
    
    
    
    
    
    3.  Convert 1087 to binary
    
    
    
    1087 / 2 = 543 with a remainder of 1 
    
    543 / 2 = 270  with a remainder of 1
    
    270 / 2 = 135  with a remainder of  0
    
    135 / 2 = 67  with a remainder of 1
    
    67 / 2 = 33  with a remainder of 1
    
    33 / 2 = 16  with a remainder of 1
    
    16 / 2 = 8  with a remainder of  0
    
    8 / 2 = 4  with a remainder of  0
    
    4 / 2 = 2  with a remainder of  0
    
    2 / 2 = 1  with a remainder of  0
    
    1 / 2 = 0  with a remainder of  1
    
    
    
    So 1087 decimal = %10000111011 binary.
    
    
    
    
    
    4.  Convert %1000010100011110 to decimal
    
                |    | |   ||||
    
                |    | |   |||+---- 1 * 2^1 = 2
    
                |    | |   ||+----- 1 * 2^2 = 4
    
                |    | |   |+------ 1 * 2^3 = 8
    
                |    | |   +------- 1 * 2^4 = 16
    
                |    | +----------- 1 * 2^8 = 256
    
                |    +------------- 1 * 2^10 = 1024
    
                +------------------ 1 * 2^15 = 32768
    
    
    
         2+4+8+16+256+1024+32768 =  34078 decimal
    
    
    
    
    
    5.  Using the list of binary numbers below:
    
    
    
      %00110101
    
      %01000101
    
      %11100001
    
      %10100110
    
      %01001011
    
      %01001110
    
      %11001001
    
    
    
        Take the LSB from each of the above numbers, in order from top to bottom, to create a new binary number.  The LSB from the first number in the list should be the LSB of the new number.   The LSB of the last number in the list is the MSB of the new binary number.  What is the new binary number?  Now convert that number to decimal.
    
    
    
    answer: The LSB is the rightmost digit of each number, so the new binary number is:   %1010111  which in decimal is: 64+0+16+0+4+2+1 = 87 
    
    
    
    
    
    6.  For each of the numbers below, convert them to decimal twice.  The first time assume that the numbers are in unsigned positive integer format.  The second time assume that the numbers are in sign-magnitude format. 
    
    
    
    a. %1000101
    
    
    
    unsigned = 64 + 0 + 0 + 0 + 4 + 0 + 1 = 69 decimal
    
    signed = (-1) * (0 + 0 + 0 + 4 + 0 + 1 ) = -5 decimal
    
    
    
    b. %0101010
    
    
    
    unsigned = 0 + 32 + 0 + 16 + 0 + 2 + 0 = 50
    
    signed = (+1) * ( 32 + 0 + 16 + 0 + 2 + 0 ) = 50
    
    
    
    c. %1100110
    
    
    
    unsigned = 64 +32 + 0 + 0 + 4 + 2 + 0 = 102
    
    signed  = (-1) * ( 32 + 0 + 0 +4 +2 +0 ) =  -38
    
    
    
    d. %0000010
    
    
    
    unsigned = 2
    
    signed =  2
    
    
    
    
    
    7. What is the range of positive unsigned integers that can be stored in a word.
    
    
    
    answer:  There are two basic ways to solve this problem.  I will demonstrate both because I think its important to see the relationships that make both methods arrive at the same answer.
    
    
    
    a.  The first approach is to realize that smallest unsigned integer is always zero.  The binary value is unsigned so no bits are needed to store the sign of the number.  In that case the largest unsigned number is the value of the binary number with all bits set to 1.    A word has 16 bits, so the biggest unsigned binary number in a word is %1111111111111111
    
    
    
    If we convert that to decimal we get:
    
    2^15 + 2^14 + 2^13 + 2^12 + 2^11 + 2^10 + 2^9 + 2^8 + 2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 
    
    32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 =  65535.
    
    
    
    b.  The second approach is to recall the formula from lesson 2 to find the maximum number of items that you can enumerate given N bits.   The binary number system is just an enumeration of the natural numbers.
    
    
    
    combinations = 2 ^ 16 = 65536 combinations.  Recall however that the enumeration begins at 0, so that leaves 65536 - 1 = 65535 as the largest possible unsigned integer in a word. 
    
    
    
    
    
    BONUS QUESTION 1:  For each number a-d in problem 6, is each number signed or unsigned?   (Hint: refer to lesson 1). 
    
    
    
    answer:  This is a trick question.  The correct answer is both.   As we  learned in the first lesson bits represent whatever you the programmer say that they represent.  They can represent both at the exact same time.  It doesn't matter, they are only bits.  Meaning is given by you the programmer with the logic of your code.
    
    
    
    BONUS Question 2:  Is the sign-magnitude format an Enumeration (Lesson 2) or a code (Lesson3), and why?
    
    
    
    It is a code, because enumerations represent only positive values from 0 to N.
    
    

     

     

    I just thought of another valuable piece of information I forgot to include in this lesson. Please note that the LSB of a binary number indicates whether the number is odd or even. If the LSB is 1 then the number is odd. If the LSB is zero, then the number is even. In your code you may wish to take an action only on odd or even scanlines or frames. You will know if the scanline or frame is even or odd by examining the LSB of the scanline or frame counter.[/code]


  11. Well, now I have a projct to try :D    That looks really cool, I'm going to give it a shot.

     

    QUICK TIP:

     

    The added buttons in the picture are from Radio shack. They are part number 275-1566A. I tried a different switch first, but that switch took too much force to be comfortable to play with.

     

    Cheers!


  12. Thanks for the positive responses. I ordered some switches from Mouser Electronics last night. I am hoping that some of them will work for my improved second version. It will be several days before the new switches arrive, so I probably won't have a version 2 for a couple weeks.

     

    As for selling the modified controllers, If I think the final design is worthy of selling I will consider doing so. Right now its too much of a hack job and I wouldn't trust them to keep working for the long term. At the least I will post detailed instructions on how to put one together.

     

    Cheers!


  13. Once I get a version I like, I will post instructions on how to make one.  

     

    Cheers!

     

    Bah, just use an NES controller modified for atari. I already beat it, there are no special controllers required. ;)

     

     

    Bah! I have never been good with thumb pads. ;)

     

     

    What's really interesting to me is that there is no reason Atari couldn't have put 3 buttons on the driving controller in the first place. There are enough free signal lines for 2 more buttons on a single joystick cord. It could have made for a truly awesome driving controller: Accel, Brake, and shift. 8)

     

    Cheers!


  14. Hi,

     

    I have been dabbling with creating a custom controller for Thrust. I like the driving controller for rotation, but I could never get comfortable with the footpedals for the shields and thrust. So I decided to try and add 2 buttons to a driving controller so that all the needed buttons are on one controller. After several false starts and trial and error, I have finally suceeded. :D

     

    In this first version, I retained the original fire button on the driving controller, and it is still used for firing. I hold the controller in my right hand, and turn the knob with my left. My thumb is on the button where the cable used to come out of the driving controller. That button is for thrusting. The other button rests under my pinky finger and it activates the shields.

     

    In play, I like it. My pinky gets tired though. Plus with all the changes to the casing the fire button can twist a tiny bit and bind up a little bit. It doesn't get stuck it just bounces back slower than normal.

     

    I have learned alot putting this one together, and I have some ideas for improvements for version 2. Once I get a version I like, I will post instructions on how to make one.

     

    Cheers!

    post-2784-1073095500_thumb.jpg


  15. I have been playing this title quite a bit lately trying to get a handle on it. I don't think I would give it an A mainly for the reason that there is no clear indication how far down your ship can move. I lose lots of ships because I try to fly downward out of the way only to bump into the invisible barrier at the bottom.

     

     

    TIPS:

    - Don't play with the mode where collisions with purple ships cause you to die. That's impossible because they move too fast.

     

    - Get past the shield as soon as possilble, and spend as much time as you can up by the Death Star:

    Reasons:

    - Each block of the death star is 20 points.

    - The more blocks you destroy the longer the DS takes to explode earning you more points (If you survive.)

    - There is no shield to bump into and die.

     

    - There are a number of audio cues in the game. Listen closely. When you hear certain noises it means events are about to happen.

     

    - The Green ships are where the big points are. Don't attack them head on! Your shot will pass through and you will crash.

     

    - Stay to the center of the screen to avoid getting whacked by ships entering the screen edge. Just dodge the DS shots and keep shooting the DS blocks to rack up points.

     

    - Keep moving! Enemies seem to shoot less often.

     

    - When you blow the DS, be sure you are ready to get out of the way. Hide on the bottom left or right, but not in the center or corners! Only a few slower fireballs will enter those 2 areas be ready to run to the other area when one comes your way.

     

     

    I always break 20000 pts using these tips and I have gotten over 30000 a couple times. Don't blow the 1st DS until you have at least 10000.

    Good-luck!


  16. So even if I get one of the rollerballs available, I won't be able to use it with Centipede? Why would anyone purchase it, then? I'm slightly confused!

     

    You can use it with centipede, but you will have to use it in joystick mode. What that means is it will behave like a rolling joystick and not a trackball like in the arcade. Since you are a fan of centipede, it would probably be worth your time to pick one up and see if you like it better than a plain joystick.

     

    Sorry if I came off as very negative about Atari trackballs. I paid $40 for one when they first came out and I was disappointed when Atari provided zero games that supported true trackball mode.

     

    Cheers!


  17. While it is true that there are rollerball controllers available for the 2600, there are no original titles that support it. :P The trackball requires special code to implement its proportional movement. Atari in their infinite wisdom, did not implement a coporate policy of supporting exotic controllers in as many titles as possible. Hence the lack of support for the light gun and the trackball. :x The trackball has a "joystick" mode which reduces motion to 8 directions like a joystick, but you have to keep rolling the ball to move. You also have to roll at top speed constantly, or the player will move along jerkily. :P

     

    There is a hack of missle command that supports the proportional mode of the track ball. http://www.atariage.com/store/product_info...&products_id=31

     

    Maybe you can convince Thomas to make a similar hack for centipede.

     

    Cheers!


  18. So far I have beaten:

     

    Switchblade II

    Crystal Mines II (assuming its okay to have beaten all 180 levels over time. I have never sat down and tried to beat them all on one set of lives :wink: )

     

    I am trying to beat:

     

    Toki (Stuck on level 5 boss)

    Dracula

     

    Cheers!


  19. Is the 13 decimal ($13...%00010011 binary) or hex ($0d...%00001101 binary)?

     

     

    If you are asking if the 13 in the PUZZLER is decimal, then yes. I am avoiding teaching Hexadecimal until we start using DASM.

     

     

    Please post your solution for the puzzler to the thread. It would be nice to see some different solutions presented. I find I learn this stuff the best by reading work from other people solving the same problem different ways.

     

    Cheers!


  20. Is the 13 decimal ($13...%00010011 binary) or hex ($0d...%00001101 binary)?

     

    Ah, good question.

     

    13 decimal = %00010011 in BCD (binary coded decimal)

    -AND-

    13 decimal = %00001101 in binary number format

     

    -OR-

     

    13 decimal = $13 in Hexadecimal notation of 13 decimal inBCD number format.

    -AND-

    13 decimal = $0D in Hexadecimal notation of 13 decimal in binary number format

     

    I haven't covered hexadecimal notation yet, so other readers should ignore the second half of this reply for now.

     

    Did I answer your question?


  21. Lesson 6 - Binary Logic
    
    ======================
    
    
    
    I know I promised to start discussing State Machines in Lesson 6, but now I see that I forgot to talk about binary logical functions.  Since I just covered binary counting, and binary math; I think it would be best to push State Machines off to lesson 7 and cover binary logic now.  
    
    
    
    Introduction:
    
    -------------
    
    
    
    What is logic?   I could spend days on that topic, so I need to focus on logic as it applied to programming computers.  Recall our discussion of bits in Lesson 2.  Each bit has one of two possible values 0 or 1.   As the programmer you can apply any meaning you wish to each bit your program uses.   A common use for bits is to indicate  TRUE or FALSE.   TRUE is usually represented by 1, and FALSE is represented by 0.  Such an arrangement is called positive logic.   You can also use negative logic, where TRUE is 0 and FALSE is 1.  For this lesson I will confine the discussion to positive logic for all of the examples, since the instructions in the 650X microprocessors use positive logic.
    
    
    
    For this class we are interested in logic functions that we can perform between pairs of bits representing TRUE and FALSE.  Some interesting things can be done with bits using just a few simple logical rules. 
    
    
    
    Logic Functions:
    
    ----------------
    
    There are four basic logical functions: AND, OR, XOR, NOT.   You can also combine NOT with the other 3 to form 3 more functions: NAND, NOR, and XNOR.   I will discuss each logical function in detail.  
    
    
    
    *****************************
    
    NOTE: For all the experienced programmers reviewing this material, I decided to exclude the logical bit-shift operations from this lesson.  I will cover bit-shifting and rotations when I cover those instructions.
    
    *****************************
    
    
    
    The best way to think about binary logical functions is as a special math operator akin to adding and subtracting as we covered in the previous lesson.   For all the logical operations, except NOT, there are 2 arguments and a single result.  So like addition we can write logical operations as A (oper) B = C, where (oper) is the symbol of the logical function to be performed on A and B resulting in C.  
    
    
    
    
    
    Logical AND:
    
    ------------
    
    The first function I will present is AND.   The function AND is easy to understand.  Given A and B, C is TRUE only if A and B are TRUE.  Otherwise, C is FALSE.   We can present the AND function as a truth table:
    
    
    
    A       B       | C
    
    -----   -----	| -----
    
    FALSE   FALSE   | FALSE
    
    FALSE   TRUE    | FALSE 
    
    TRUE    FALSE   | FALSE
    
    TRUE    TRUE    | TRUE
    
    
    
    As a programmer you will use AND to determine if multiple conditions are TRUE at the same time.  
    
    
    
    For example:
    
    A = TRUE if the player is carrying the Blue Key.
    
    B = TRUE if the player is touching the Blue Door.
    
    C = TRUE if the player has the Blue key and is touching the blue door.
    
    If C is TRUE, then unlock the Blue Door and play sound effect.
    
    
    
    ***************** NOTE: ******************************
    
    Above, is an example of what is called pseudocode.  Its a list of instructions similar  to what an actual program section will look like, but it is written in english rather than the programming language.  I will be using psuedocode more and more in these lessons, and you need to get comfortable both reading and writing pseudo code as part of this class.  Expect to see exercises where you will read and write pseudocode.
    
    ******************************************************
    
    
    
    The symbol for AND is &.  So, C = A AND B = A & B is the same thing.
    
    
    
    
    
    Logical OR:
    
    -----------
    
    The next logical function is OR.  Given A and B, C is TRUE if either A or B is TRUE, or both A and B are TRUE.   Logical OR in an 'inclusive-OR', not an 'exclusive-OR' as represented by the XOR function to be discussed next.  Here is the truth table for OR.
    
    
    
    A       B       | C
    
    -----   -----   | -----
    
    FALSE   FALSE   | FALSE
    
    FALSE   TRUE    | TRUE 
    
    TRUE    FALSE   | TRUE
    
    TRUE    TRUE    | TRUE
    
    
    
    The shorthand symbol for OR is '|'.  So, C = A OR B = A | B, is equivalent.
    
    
    
    
    
    Logical XOR:
    
    ------------
    
    XOR stands for exclusive OR.  C is TRUE if either A or B is TRUE, but it is FALSE if A and B are both TRUE or both FALSE.  Here is the truth table for XOR:
    
    
    
    A       B       | C
    
    -----   -----   | -----
    
    FALSE   FALSE   | FALSE
    
    FALSE   TRUE    | TRUE 
    
    TRUE    FALSE   | TRUE
    
    TRUE    TRUE    | FALSE
    
    
    
    The shorthand symbol for XOR is '^'.  So, C = A XOR B = A ^ B is equivalent.
    
    
    
    Logical NOT:
    
    ------------
    
    The function NOT is special in that it takes only 1 argument, not 2.  It is akin to using the negative sign in arithmetic to make a number negative.   C is the opposite state of the input A.  So C is FALSE if A is TRUE.  C is TRUE if A is FALSE.  Here is the truth table for NOT:
    
    
    
    A       | C
    
    -----   | -----
    
    FALSE   | TRUE
    
    TRUE    | FALSE
    
    
    
    A common way to represent the function NOT is to place a bar over the input like this:
    
    _
    
    A = NOT A
    
    
    
    Another way is to use a tilde like this:
    
    
    
    ~A = NOT A
    
    
    
    The second method is easier to implement in the ASCII text of this lesson, but the first method is easier to read (I think).  I will try to use the bar notation in my lessons, but if it gets too annoying to type, I may start using the tilde. 
    
    
    
    
    
    LOGICAL NAND, NOR, XNOR:
    
    ------------------------
    
    The functions NAND, NOR, and XNOR are the logical opposites of AND, OR, and XOR.  Its shorthand:
    
    
    
    A NAND B = NOT (A AND B)
    
    A NOR B  = NOT (A OR B)
    
    A XNOR B = NOT (A XOR B)
    
    
    
    Here are the truth tables for NAND, NOR, and XNOR:
    
    
    
    NAND:
    
    A       B       | C
    
    -----   -----   | -----
    
    FALSE   FALSE   | TRUE
    
    FALSE   TRUE    | TRUE 
    
    TRUE    FALSE   | TRUE
    
    TRUE    TRUE    | FALSE
    
    
    
    NOR:
    
    A       B       | C
    
    -----   -----   | -----
    
    FALSE   FALSE   | TRUE
    
    FALSE   TRUE    | FALSE 
    
    TRUE    FALSE   | FALSE
    
    TRUE    TRUE    | FALSE
    
    
    
    XNOR:
    
    A       B       | C
    
    -----   -----   | -----
    
    FALSE   FALSE   | TRUE
    
    FALSE   TRUE    | FALSE 
    
    TRUE    FALSE   | FALSE
    
    TRUE    TRUE    | TRUE
    
    
    
    For notation, I will simply use the NOT bar or tilda in combination with the exiting notation for AND, OR, or XOR.
    
    
    
                              _____
    
    A NAND B = NOT (A AND B) = A & B = ~(A & B)
    
                            _____
    
    A NOR B = NOT (A OR B) = A | B = ~(A | B)
    
                              _____
    
    A XNOR B = NOT (A XOR B) = A ^ B = ~(A ^ B)
    
    
    
    FROM BITS TO BYTES:
    
    -------------------
    
    I described the functions above in terms of pairs of bits resulting in a single bit.  In programming 650X assembly language, you will perform these functions on pairs of bytes.  For each input byte you perform the logic function between the corresponding pairs of bits from each input byte, and place the result bit in the corresponding position in the result byte.  Look at the graphic examples below for a picture of the operation.
    
    
    
    
    
    SOME PRACTICAL LOGIC:
    
    ---------------------
    
    
    
    As an assembly language programmer you will make frequent use of binary logic functions in your programs.  In this section I provide some practical examples for AND, OR, and XOR.  As a programmer you will often use individual bits within bytes to indicate whether conditions in your game environment are TRUE or FALSE, such bits are often refered to as flags.  You will often group flags together into bytes to save space in the limited memory of your computer.  You will use the AND, OR, XOR logical functions to clear, set, and toggle those flags as events happen in your game. 
    
    
    
    ------
    
    AND - The function AND is often used in programs to reset one or more bits in a byte to zero, while leaving the other bits unchanged. 
    
    
    
    Example:
    
    
    
    Given: %11011010
    
    
    
    Say you want to be sure that the MSB and LSB of the given byte are clear.  In this example the LSB is already clear, but you don't want to waste time in your code to figure out if the bits are set and then clear them.  With AND you can just clear them.
    
    The first step is to create the necessary bit-mask.  The bit mask is a byte value that will preserve the bits you want unchanged and clear the bits you want cleared.  For each bit to be cleared, reset the bit in the bit mask to 0.  For each bit in the given byte to be unchanged, set the bit in the bit mask to 1.  So to clear the MSB and LSB, but preserve all other bits as is, the bit mask is:  %01111110
    
    
    
    C = Given & bitmask = %11011010 & %01111110 = %01011010
    
                                                  ^^^^^^^^
    
           %11011010                              ||||||||
    
         & %01111110                              ||||||||
    
         -----------                              ||||||||
    
            ||||||||                              ||||||||
    
            |||||||+--> 0 & 0 = 0 ----------------++++++++
    
            ||||||+---> 1 & 1 = 1 ----------------+++++++
    
            |||||+----> 0 & 1 = 0 ----------------++++++
    
            ||||+-----> 1 & 1 = 1 ----------------+++++
    
            |||+------> 1 & 1 = 1 ----------------++++
    
            ||+-------> 0 & 1 = 0 ----------------+++
    
            |+--------> 1 & 1 = 1 ----------------++
    
            +---------> 1 & 0 = 0 ----------------+
    
    
    
    ------
    
    OR - The OR function is often used to set individual bits to 1 within a byte without changing the state of other bits in the byte.  As with using AND to clear bits in a byte, we don't care if the bits are already set will be set by OR'ing the byte with a corresponding bit mask.  Every bit we want set in the byte must be set in the bit mask.  Any bit clear in the bit mask will be unchanged after the OR.
    
    
    
    Example:
    
    
    
    Given: %11011010 - use OR to set the MSB and LSB.
    
    
    
    bitmask = %10000001
    
    
    
    C = Given | bitmask = %11011010 | %10000001 = %11011011
    
                                                  ^^^^^^^^
    
           %11011010                              ||||||||
    
        or %01111110                              ||||||||
    
           ---------                              ||||||||
    
            ||||||||                              ||||||||
    
            |||||||+--> 0 | 1 = 1 ----------------++++++++
    
            ||||||+---> 1 | 0 = 1 ----------------+++++++
    
            |||||+----> 0 | 0 = 0 ----------------++++++
    
            ||||+-----> 1 | 0 = 1 ----------------+++++
    
            |||+------> 1 | 0 = 1 ----------------++++
    
            ||+-------> 0 | 0 = 0 ----------------+++
    
            |+--------> 1 | 0 = 1 ----------------++
    
            +---------> 1 | 1 = 1 ----------------+
    
    
    
    ------
    
    XOR - The XOR function is often used in code to flip/toggle the state of specific bits in a byte without effecting other bits in the byte.   If the bit is 1 it is flipped to 0.  If the bit is 0 it is flipped/toggled to 1.   As with AND to clear bits, and OR to set bits, for XOR you must create a bitmask to indicate which bits are to be toggled and which are to be unchanged.  Each bit set in the bitmask will toggle the bit in the target byte.  Each bit in the bitmask reset to 0 will be unchanged in the target byte.
    
    
    
    Example:
    
    Given: %11011010 - use XOR to toggle the MSB and LSB.
    
    
    
    bitmask = %10000001
    
    
    
    C = Given ^ bitmask = %11011010 ^ %10000001 = %01011011
    
                                                  ^^^^^^^^
    
           %11011010                              ||||||||
    
        or %01111110                              ||||||||
    
           ---------                              ||||||||
    
            ||||||||                              ||||||||
    
            |||||||+--> 0 ^ 1 = 1 ----------------++++++++
    
            ||||||+---> 1 ^ 0 = 1 ----------------+++++++
    
            |||||+----> 0 ^ 0 = 0 ----------------++++++
    
            ||||+-----> 1 ^ 0 = 1 ----------------+++++
    
            |||+------> 1 ^ 0 = 1 ----------------++++
    
            ||+-------> 0 ^ 0 = 0 ----------------+++
    
            |+--------> 1 ^ 0 = 1 ----------------++
    
            +---------> 1 ^ 1 = 0 ----------------+
    
    
    
    -------
    
    
    
    MORE LOGIC:
    
    
    
    There is much more to binary logic, often called Boolean math after its inventor.  This lesson has covered enough for our needs, but there is plently more information and advanced techniques for simplifying complex logical equations, for example:
    
       ____________________
    
    E = ((A & B) | (C ^ D ))  & A
    
    
    
    We aren't interested in such complex logic in a beginners course, but feel free to explore the topic yourself if interested.  Search the internet for "Boolean Math".
    
    
    
    ===========
    
    Excercises:
    
    
    
    1. Given the following bytes (A) and bitmask (B) values calculate the result C, for C = A & B, C = A | B, and C = A ^ B
    
    
    
    a. A = %11111111, B=%10101010
    
    b. A = %00000000, B=%01010101
    
    c. A = %11110000, B=%01110111
    
    
    
    
    
    2.  Given the following bytes (A) and bitmask (B) values calculate the result C, for C = A NAND B, C = A NOR B, and C = A XNOR B
    
    
    
    a. A = %10110110, B=%00000000
    
    b. A = %11111111, B=%11111111
    
    
    
    
    
    3. Fill in the blanks '_____' in this pseudocode example with what you believe is the correct logical function.
    
    
    
    SUBROUTINE CheckPlayerStatus
    
    BEGIN
    
    IF (PLAYER.has_green_key = TRUE) ___ (Player.is_touching_door) THEN
    
     Goto PlayerExitsLevel
    
    ELSE IF (Player.touching_monster = TRUE) ___ (Player.touching_arrow) THEN
    
     Goto PlayerKilled 
    
     ELSE IF (Player.touching_gold = TRUE) ___ (Monster.died) THEN
    
     Goto PlayerGetsPoints
    
    ENDIF
    
    END CheckPlayerStatus
    
    
    
    
    
    4. BONUS PUZZLER!!!!
    
    
    
    You are running low on available memory for your game.  You need to store 2 different counters of events occuring in your game.  The first counter counts up from 0 to 7, when the counter reaches 7 the next time it is added to it resets to zero. The second counter, will count down from 13 to 0, once the counter is at zero it stays at zero even if it is decremented again.  Devise a method for using binary addition and subtration in combination with AND, OR, or NOR logic functions to allow you to store both counters in a single byte.  You must be able to change either counter without affecting the other.  Write the pseudo code to change each counter without affecting the other.  Your solution must not share any bits between the two counters even though such a solution is technically possible, you do not have enough free clock cycles left in your program to use such a technique.
    
    


  22. Lesson 5 - Binary Math
    
    
    
    In lesson 4, I introduced counting with bits using the binary number system.  In this lesson I will cover binary arithmetic and conclude my description of negative numbers using two's complement notation.
    
    
    
    Binary Addition:
    
    
    
    Adding binary numbers is as easy as 1, 2, 3.   In fact all you ever need to know is:
    
    
    
    0+0 = 0
    
    0+1 = 1
    
    1+0 = 1
    
    1+1 = 2
    
    1+1+1 = 3
    
    
    
    Adding 2 binary numbers uses the same method as adding decimal numbers.  The only difference is that in decimal when you add each pair of digits, if the sum is greater than 9, you carry the 10 to the next column.  In binary when the sum is greater than 1, then you carry the 2 to the next column.  Except the 2 is in binary notation so its %10 instead of the decimal 2.
    
    
    
    The best way to see this method is probably by example.  Let's add 2 numbers in decimal, and then add the same numbers in binary.
    
    
    
    Example:
    
    
    
    Decimal:
    
           34
    
         + 67
    
    ------------
    
           ||
    
           |+-> 4+7 = 11 ------------------+
    
           |          |                    |
    
           |          | Carry the 10       |
    
           |          v                    |
    
           +--> 3+6 + 1 = 10 -------------+|
    
                          |               ||
    
                          | Carry the 10 ||
    
                          v               ||
    
                0+0 +     1 = 1 -------> 101 = Final answer.
    
                ^ ^
    
                | |
    
                +-+---- Note the leading zeros needed to finish the calculation.
    
    
    
    Binary:
    
      %0100010 (=34)
    
     +%1000011 (=67)
    
      --------
    
       |||||||
    
       ||||||+- 0+1 = %01----------------------------+
    
       ||||||          |                             |
    
       ||||||          | No carry                    |
    
       ||||||          v                             |
    
       |||||+-- 1+1 +  0 = %10 (=2) ----------------+|
    
       |||||                |                       ||
    
       |||||           +----+  Carry the 2 (=%10)   ||
    
       |||||           v                            ||
    
       ||||+--- 0+0 +  1 = %01 --------------------+||
    
       ||||                 |                      |||
    
       ||||            +----+  No Carry            |||
    
       ||||            v                           |||
    
       |||+---- 0+0 +  0 = %00 -------------------+|||
    
       |||                  |                     ||||
    
       |||             +----+  No Carry           ||||
    
       |||             v                          ||||
    
       ||+----- 0+0 +  0 = %00 ------------------+||||
    
       ||                   |                    |||||
    
       ||              +----+  No Carry          |||||
    
       ||              v                         |||||
    
       |+------ 1+0 +  0 = %01 -----------------+|||||
    
       |                    |                   ||||||
    
       |               +----+  No Carry         ||||||
    
       |               v                        ||||||
    
       +------- 0+1 +  = = %01 ----------------+||||||
    
                            |                  |||||||
    
                            |                  vvvvvvv 
    
                            +- No Carry       %1100101 = 101 decimal = Final answer
    
    
    
    
    
    Ta Da!
    
    
    
    Unfortunately, I chose a poor example in that there is no case of 1 + 1 + 1 in the above example, so lets try another one:
    
    
    
    Binary Addition exmaple 2:
    
      %1101111 (=111 decimal, are you confused yet :P )
    
     +%1111011 (=123 decimal)
    
      --------
    
       |||||||
    
       ||||||+- 1+1 = %10 (=2) ----------------------+
    
       ||||||          |                             |
    
       ||||||          | Carry the 2 (=%10)          |
    
       ||||||          v                             |
    
       |||||+-- 1+1 +  1 = %11 (=3) ----------------+|
    
       |||||                |                       ||
    
       |||||           +----+  Carry the 2 (=%10)   ||
    
       |||||           v                            ||
    
       ||||+--- 1+0 +  1 = %10 --------------------+||
    
       ||||                 |                      |||
    
       ||||            +----+  Carry the 2 (=%10)  |||
    
       ||||            v                           |||
    
       |||+---- 1+1 +  1 = %11 -------------------+|||
    
       |||                  |                     ||||
    
       |||             +----+  Carry the 2 (=%10) ||||
    
       |||             v                          ||||
    
       ||+----- 0+1 +  1 = %10 ------------------+||||
    
       ||                   |                    |||||
    
       ||              +----+  Carry the 2       |||||
    
       ||              v                         |||||
    
       |+------ 1+1 +  1 = %11 -----------------+|||||
    
       |                    |                   ||||||
    
       |               +----+  Carry the 2      ||||||
    
       |               v                        ||||||
    
       +------- 1+1 +  1 = %11 ----------------+||||||
    
                            |                  |||||||
    
                            +-- Carry the 2 --+|||||||
    
                                              ||||||||
    
                                              vvvvvvvv 
    
                                             %11101010 = 234 decimal = Final answer
    
                                              |
    
                                              v
    
                                              Please note that the final carry forms a
    
                                              new digit replacing what was a leading 
    
                                              zero in the numbers being added.
    
    
    
    So much for addition, now let's look at subtraction.
    
    
    
    
    
    Binary Subtraction:
    
    
    
    You may be beginning to suspect, binary subtraction is very similar to decimal subtraction in the same way that binary addition is similar to decimal addtion.  If so, then you are correct.  Binary subtraction follows the same basic rules as decimal subtraction.  The difference is that when you don't have enough to perform the substraction of 2 digits as in decimal, then you do not borrow 10.  Rather you will borrow 2.  
    
    
    
    So:
    
    
    
    0 - 0 = 0
    
    1 - 0 = 1
    
    0 - 1 = 1 borrow 2
    
    1 - 1 = 0
    
    1 - 0 - 1 = 0
    
    1 - 1 - 1 = 0 borrow 2
    
    |
    
    v
    
    This 3rd digit in the subtraction represents the borrow from the
    
           previous step of the subtraction.
    
    
    
    
    
    As I did for addition, let's try an example:
    
    
    
    Decimal example:
    
     124
    
    - 115
    
     ---
    
     |||
    
     ||+-->  3 - 5 = 9 with borrow of 10 -------+
    
     ||                               |         | 
    
     ||              +---- borrow ----+         |
    
     ||              v                          |
    
     |+--->  2 - 1 - 1 = 0 --------------------+|
    
     |                   |                     ||
    
     |               +---+ no borrow           ||
    
     |               |                         ||
    
     +---->  1 - 1 - 0 = 0 -------------------+||
    
                         |                    |||
    
                         +- no borrow         vvv
    
                                              009 = 9 final answer.
    
    
    
    
    
    same example subtraction in Binary:
    
     %1111100 (= 124 decimal)
    
    - %1110011 (= 115 decimal)
    
     --------
    
      |||||||
    
      |||||||
    
      ||||||+---> 0 - 1     = 1 with borrow of 2 ---------+
    
      ||||||                                   |          |
    
      ||||||              +------ borrow ------+          |
    
      ||||||              v                               |
    
      |||||+----> 0 - 1 - 1 = 0 with borrow of 2 --------+|
    
      |||||                                    |         ||
    
      |||||               +------ borrow ------+         ||
    
      |||||               v                              ||
    
      ||||+-----> 1 - 0 - 1 = 0 ------------------------+||
    
      ||||                    |                         |||
    
      ||||                +---+  no borrow              |||
    
      ||||                v                             |||
    
      |||+------> 1 - 0 - 0 = 1 -----------------------+|||
    
      |||                     |                        ||||
    
      |||                 +---+  no borrow             ||||
    
      |||                 v                            ||||
    
      ||+-------> 1 - 1 - 0 = 0 ----------------------+||||
    
      ||                      |                       |||||
    
      ||                  +---+  no borrow            |||||
    
      ||                  v                           |||||
    
      |+--------> 1 - 1 - 0 = 0 ---------------------+|||||
    
      |                       |                      ||||||
    
      |                   +---+  no borrow           ||||||
    
      |                   v                          ||||||
    
      +---------> 1 - 1 - 0 = 0 --------------------+||||||
    
                              |                     |||||||
    
                              v                     vvvvvvv
    
                              no borrow            %0001001 = 9 decimal = final answer
    
    
    
    Ta Da!
    
    
    
    So you see that binary subtraction closely parallels decimal subtraction.  
    
    There is a problem, however, that we have not yet addressed.  If we subtract a larger number from a smaller one, the result will be negative.  In lesson 4, I began discussion of the 2's complement format for binary negative numbers.  In that discussion I said that a prime advantage of 2's complement was that addition and substraction of numbers in 2's complement resulted in the correct answer in 2's complement format.  Now, let's perform a subtraction with a negative result and see what happens.  No new rules are required to perform the subtraction, just follow the same procedure as if the expected result will be positive.
    
    
    
    Binary subtraction example 2:
    
     %1110011 (= 115 decimal)
    
    - %1111100 (= 124 decimal)
    
     --------
    
      |||||||
    
      |||||||
    
      ||||||+---> 1 - 0     = 1 --------------------------+
    
      ||||||                  |                           |
    
      ||||||              +---+ no borrow                 |
    
      ||||||              v                               |
    
      |||||+----> 1 - 0 - 0 = 1 -------------------------+|
    
      |||||                   |                          ||
    
      |||||               +---+ no borrow                ||
    
      |||||               v                              ||
    
      ||||+-----> 0 - 1 - 0 = 1 with borrow of 2 -------+||
    
      ||||                                     |        |||
    
      ||||                +------ borrow ------+        |||
    
      ||||                v                             |||
    
      |||+------> 0 - 1 - 1 = 0 with borrow of 2 ------+|||
    
      |||                                      |       ||||
    
      |||                 +------ borrow ------+       ||||
    
      |||                 v                            ||||
    
      ||+-------> 1 - 1 - 1 = 1 with borrow of 2 -----+||||
    
      ||                                       |      |||||
    
      ||                  +------ borrow ------+      |||||
    
      ||                  v                           |||||
    
      |+--------> 1 - 1 - 1 = 1 with borrow of 2 ----+|||||
    
      |                                        |     ||||||
    
      |                   +------ borrow ------+     ||||||
    
      |                   v                          ||||||
    
      +---------> 1 - 1 - 1 = 1 with borrow of 2 ---+||||||
    
                                               |    |||||||
    
    Leading Zeros!         +------ borrow ------+    |||||||
    
     |                    v                         |||||||                         
    
     +----------> 0 - 0 - 1 = 1 with borrow of 2 --+|||||||
    
                              |                    ||||||||
    
                              v                    vvvvvvvv
    
                              BORROW!             %11110111 = -9 decimal = final answer
    
    
    
    
    
    Two' Complement Revisited:
    
    
    
    The answer above is indeed the expected answer of -9 represented in 2's complement binary notation.  Don't worry if you are confused, it will all become clear shortly.  The most important thing to note from the above example, is that at the end of our calculation there is still a borrow.  Since all that remains to subtract are leading zeros, the final borrow can never be resolved.  It will produce an infinite set of leading 1's to our final answer if we keep calculating forever.  This is the first clue to understanding 2's complement.  Negative numbers in two's complement have an infinite number of leading 1's as opposed to positive numbers which have infinite leading zeros.
    
    
    
    From this we can deduce that for all 2's complement numbers the MSB indicates the sign of the number, exactly the same as sign magnitude notation (lesson 4).  If the MSB is set, then the number is negative, if the MSB is clear, then the number is positive.  The difference is that 2's complement has no representation for negative 0.   %10000000 in 2's complement is not negative zero.  Instead it is the largest negative number that the bits other than the sign bit can represent.  So %10000000 has 7 zeros that 7 bits, 2^7 = 128, so %10000000 is -128 in 2's complement.
    
    
    
    FINDING THE TWO' COMPLEMENT:
    
    
    
    Given any positive binary number you can find its two's complement by inverting all the bits (change all 0's to 1, 1's to 0's) and then add 1.
    
    
    
    For example:
    
    
    
    55 = %01010101
    
         ||||||||  Invert all bits
    
         vvvvvvvv
    
        %10101010
    
       +        1  Add one 
    
        ---------
    
        %10101011 = -55
    
    
    
    Likewise we can perform the same process in reverse to convert any negative number in two's complement format to its positive equivalent.
    
    
    
    -55 = %10101011
    
        -        1  Subtract 1
    
         ---------
    
         %10101010
    
          ||||||||  Invert all bits.
    
          vvvvvvvv
    
         %01010101 = 55  Ta Da!
    
    
    
    
    
    [u]THE DREADED OVERFLOW AND UNDERFLOW!:[/u]
    
    
    
    In lesson 4, I mentioned underflow and overflow.  Now I will attempt to explain what they are in detail.  In lesson 1, we learned that all information in a computer is stored using bits.  In lessons 2 and 3 we explored using enumerations and codes made of bits to store information like kinds of fruit.  In lesson 4, we learned the standard method for counting in binary.  A key point in all these lessons has been finding the number of bits needed to store the information in question.   Up until now, in this lesson, I have assumed I had infinite bits to work with (leading zero's and one's were not shown but implied), but in a real computer the number of bits available is limited.
    
    
    
    Therefore, the storage of each number used by your program must be restricted to a finite number of bits.  Since the 650X famliy of processors are 8-bit processors (Each instruction works with 8 bit chunks of data at a time), we will assume you will be using multiples of 8 bits to store all your numbers.  You don't have to use multiples of 8, it simply makes programming easier.
    
    
    
    Earlier, I showed that the MSB of a 2's complement number is the indicator of the sign of the number.  For an 8 bit number (a byte) this means that the magnitude of the number is restricted to 7 bits.  There are 128 possible combinations for 7 bits.  So for positive numbers we can represent values 0 through 127 with a single byte in two's complement.  There is no negative zero, so for negative numbers 8 bits can represent the values -1 through -128.   Therefore, 1 bytes can hold signed integers in the range from -128 to 127.  Recall from lesson 4, a byte can hold UNSIGNED integers from 0 to 255.
    
    
    
    Overflow and Underflow are terms meant to describe when an addition or subtraction results in an answer outside the range of values that can be represented by the number of bits being used.  Overflow is when the result exceeds the maximum that the number of bits and the chosen format can represent.  Underflow is when the result is less that the minimum value that the number of bits and format can represent.  Specifically for 8 bits:
    
    
    
    For signed 8-bit numbers:  
    
    IF A + B is > 127 then OVERFLOW has occurred.
    
    IF A + B is < -128 then UNDERFLOW has occurred.
    
    IF A - B is > 127 then OVERFLOW has occurred.
    
    IF A - B is < -128 then UNDERFLOW has occurred.
    
    
    
    For unsigned 8-bit numbers:
    
    IF A+B is > 255 then OVERFLOW has occurred.
    
    IF A-B is < 0 then UNDERFLOW has occurred. 
    
    
    
    The 650X processors include hardware support for detecting overflow and underflow during math operations.  In your programs if overflow or underflow is possible, then you will need to check if it has happened and respond to correct the situation when it does. The details of code to handle overflow and underflow will have to wait for many lessons.  For now just understand what it means.
    
    
    
    [b]BONUS QUIZ[/b]:  What are possible correct responses for a program to handle overflow or underflow?
    
    
    
    
    
    BINARY MULIPLICATION AND DIVISION:
    
    
    
    Let's take some time to understand how multiplication and division are accomplished with binary numbers.  All modern processors have built in support for multiplying and dividing integers, but the old 650X processors do not have specific opcodes (instructions) to multiply or divide 2 integers.  That means you have to write code to perform a multiply or divide using the instructions that the processor does have.  Luckily, the 650X processor family has build in support for adding and subtracting binary numbers.  We will therefore use addition to implement multiplication, and subtraction to perform division.
    
    
    
    Multiplying:
    
    
    
    Think for a moment about decimal multiplication.  What does it mean to say 3 time 5?
    
    One way to visualize multiplication is as an area of a rectangle, Area = width times height.
    
    
    
    Example:  Area = Width x height = 3 x 5 = ?
    
    
    
         5
    
       VVVVV   
    
    
    
     > OOOOO
    
    3 > OOOOO  
    
     > OOOOO
    
       |
    
       v
    
       The rectangle of O characters above represents the area of a 3 by 5 rectangle.  To calculate the area we simply count the O's.  There are 15 O's, which is the same as 3 times 5 = 15!
    
    
    
    Likewise, to mulitply on a 650X processor, you will solve the problem by adding up the O's of an imaginary rectangle.   Your program will Add 5 three times 5+5+5 = 15, or it could add 3 five times 3+3+3+3+3=15.  Both ways you accomplish multiplication via repeated addition.
    
    
    
    Division:
    
    
    
    In a similar iterative fashion, division is accomplished via repeated subtraction.
    
    
    
    Example: 15 divided 5 = ?
    
    
    
    15 / 5 = 15 - 5 = 10 - 5 = 5 - 5 = 0  ->    Remainder = 0 in this case.
    
                 |        |       |
    
                 v        v       v
    
                 1    +   1    +  1 = 3  ->    15 / 5 = 3
    
    
    
    
    
    Okay, that's enough for today's lesson.  Please try the following exercises.  Stay tuned for a big change of pace in lesson 6 - State Machines!
    
    
    
    Exercises:
    
    
    
    1.  Perform the following binary additions of 2's complement numbers.  Express the final result in 8 bits and indicate whether UNDERFLOW or OVERFLOW occurred.
    
    
    
    a.  %10010100 + %01101000 = ? 
    
    
    
    b.  %00110100 + %01101111 = ?
    
    
    
    c.  %10011100 + %11111000 = ?
    
    
    
    d.  %01010011 + %11011101 = ?
    
    
    
    
    
    2.  Perform the following binary subtractions of 2's complement numbers.  Express the final result in 8 bits and indicate whether UNDERFLOW or OVERFLOW occurred.
    
    
    
    a.  %10110100 - %01001000 = ? 
    
    
    
    b.  %00110100 - %01101011 = ?
    
    
    
    c.  %10111100 - %11011010 = ?
    
    
    
    d.  %00010111 - %11010111 = ?
    
    
    
    3.  Convert the following 8-bit 2's complement numbers to their negative equivalent
    
    
    
    a.  %00010010
    
    b.  %01000101
    
    c.  %01111111
    
    d.  %00000000
    
    
    
    4.  Convert the following 8-bit 2's complement numbers to their positive equivalent
    
    
    
    a.  %10110110
    
    b.  %11001101
    
    c.  %11111111
    
    d.  %10000000
    
    
    
    5.  Provide a description of what would constitute OVERFLOW and UNDERFLOW for addition and subtraction involving 16-bit (word) 2's-complement numbers.
    
    
    
    

×
×
  • Create New...