Useful
Inventions
Favorite
Quotes
Game
Design
Atari
Memories
Personal
Pages

Assembly Language Programming

Lesson 5: Binary Math

By Robert M (adapted by Duane Alan Hahn)

Table of Contents

Original Lesson

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.

 

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 let's try another one.

 

Binary Addition Example 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 addition. 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 subtraction 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 subtraction example 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 subtraction 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's 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.

 

 

 

 

 

 

 

 

Binary Multiplication 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.

 

 

 

 

 

 

 

 

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.
    1. %10010100 + %01101000 = ?
    2. 00110100 + %01101111 = ?
    3. %10011100 + %11111000 = ?
    4. %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.
    1. %10110100 - %01001000 = ?
    2. %00110100 - %01101011 = ?
    3. %10111100 - %11011010 = ?
    4. %00010111 - %11010111 = ?

     

  3. Convert the following 8-bit 2's complement numbers to their negative equivalent.
    1. %00010010
    2. %11001101
    3. %11111111
    4. %10000000

     

  4. Convert the following 8-bit 2's complement numbers to their positive equivalent.
    1. %10110110
    2. %11001101
    3. %11111111
    4. %10000000

     

  5. Provide a description of what would constitute OVERFLOW and UNDERFLOW for addition and subtraction involving 16-bit (word) 2's-complement numbers.

 

 

 

 

 

Answers

  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.
    1. %10010100 + %01101000 = ?
    2. 
          %10010100 
      
        + %01101000
      
          ---------
      
          %11111100 -> No overflow or underflow.
      
      

       

    3. 00110100 + %01101111 = ?
    4. 
           11111    -> carry the 2.
      
          %00110100 
      
        + %01101111 
      
          ---------
      
          %10100011 -> Overflow occurred!
                       Adding 2 positive numbers
                       resulted in a negative.
      
      

       

    5. %10011100 + %11111000 = ?
    6. 
           11111     -> carry the 2.
      
          %10011100
      
        + %11111000
      
          --------- 
      
          %10010100 -> No Underflow.
      
      

       

    7. %01010011 + %11011101 = ?
    8. 
           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.
    1. %10110100 - %01001000 = ?
    2. 
          %10110100 
      
        - %01001000
      
        -  1  1     -> borrow 2
      
          ---------
      
          %01001100 -> Underflow occurred!
                       (-) minus (+) should not equal a (+)
      
      

       

    3. %00110100 - %01101011 = ?
    4. 
          %00110100 
      
        - %01101011
      
        - 11  1 11  -> borrow 2
      
          ---------
      
          %11001001 -> No overflow or underflow.
      
      

       

    5. %10111100 - %11011010 = ?
    6. 
          %10111100 
      
        - %11011010
      
        - 11    1   -> borrow 2
      
          ---------
      
          %11100010 -> No overflow or underflow.
      
      

       

    7. %00010111 - %11010111 = ?
    8. 
          %00010111 
      
        - %11010111 
      
        - 11        -> borrow 2
      
          ---------
      
          %01000000 -> No overflow or underflow.
      
      

     

     

  3. Convert the following 8-bit 2's complement numbers to their negative equivalent.
    1. %00010010
    2. 
          %00010010 
      
           ||||||||
      
           vvvvvvvv 
      
          %11101101 -> invert all bits.
      
        +         1 -> add 1.
      
          ---------
      
          %11101110
      
      

       

    3. %11001101
    4.    %01000101 = %10111011
      

       

    5. %11111111
    6.    %01111111 = %10000001
      

       

    7. %10000000
    8. 
          %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.
    1. %10110110
    2. 
          %10110110
      
           ||||||||
      
           vvvvvvvv
      
          %01001001
      
        +         1 
      
          ---------
      
          %01001010
      
      

       

    3. %11001101
    4.    %11001101 = %00110011 
      

       

    5. %11111111
    6.    %11111111 = %00000001
      

       

    7. %10000000
    8. 
         %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.
  6. 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.

 

 

 

Other Assembly Language Tutorials

Be sure to check out the other assembly language tutorials and the general programming pages on this web site.

 

 

< Previous Lesson

 

 

Next Lesson >

 

 

 

 

Lesson Links

Lesson 1: Bits!

Lesson 2: Enumeration

Lesson 3: Codes

Lesson 4: Binary Counting

Lesson 5: Binary Math

Lesson 6: Binary Logic

Lesson 7: State Machines

 

 

 

 

Useful Links

Easy 6502 by Nick Morgan

How to get started writing 6502 assembly language. Includes a JavaScript 6502 assembler and simulator.

 

 

Atari Roots by Mark Andrews (Online Book)

This book was written in English, not computerese. It's written for Atari users, not for professional programmers (though they might find it useful).

 

 

Machine Language For Beginners by Richard Mansfield (Online Book)

This book only assumes a working knowledge of BASIC. It was designed to speak directly to the amateur programmer, the part-time computerist. It should help you make the transition from BASIC to machine language with relative ease.

 

 

The Second Book Of Machine Language by Richard Mansfield (Online Book)

This book shows how to put together a large machine language program. All of the fundamentals were covered in Machine Language for Beginners. What remains is to put the rules to use by constructing a working program, to take the theory into the field and show how machine language is done.

 

 

6502 Instruction Set with Examples

A useful page from Assembly Language Programming for the Atari Computers.

 

 

6502.org

Continually strives to remain the largest and most complete source for 6502-related information in the world.

 

 

Guide to 6502 Assembly Language Programming by Andrew Jacobs

Below are direct links to the most important pages.

 

 

Stella Programmer's Guide

HTMLified version.

 

 

Nick Bensema's Guide to Cycle Counting on the Atari 2600

Cycle counting is an important aspect of Atari 2600 programming. It makes possible the positioning of sprites, the drawing of six-digit scores, non-mirrored playfield graphics and many other cool TIA tricks that keep every game from looking like Combat.

 

 

How to Draw A Playfield by Nick Bensema

Atari 2600 programming is different from any other kind of programming in many ways. Just one of these ways is the flow of the program.

 

 

Cart Sizes and Bankswitching Methods by Kevin Horton

The "bankswitching bible." Also check out the Atari 2600 Fun Facts and Information Guide and this post about bankswitching by SeaGtGruff at AtariAge.

 

 

Atari 2600 Specifications

Atari 2600 programming specs (HTML version).

 

 

Atari 2600 Programming Page (AtariAge)

Links to useful information, tools, source code, and documentation.

 

 

MiniDig

Atari 2600 programming site based on Garon's "The Dig," which is now dead.

 

 

TIA Color Charts and Tools

Includes interactive color charts, an NTSC/PAL color conversion tool, and Atari 2600 color compatibility tools that can help you quickly find colors that go great together.

 

 

The Atari 2600 Music and Sound Page

Adapted information and charts related to Atari 2600 music and sound.

 

 

Game Standards and Procedures

A guide and a check list for finished carts.

 

 

Stella

A multi-platform Atari 2600 VCS emulator. It has a built-in debugger to help you with your works in progress or you can use it to study classic games.

 

 

JAVATARI

A very good emulator that can also be embedded on your own web site so people can play the games you make online. It's much better than JStella.

 

 

batari Basic Commands

If assembly language seems a little too hard, don't worry. You can always try to make Atari 2600 games the faster, easier way with batari Basic.

 

 

Back to Top

 

Disclaimer

View this page and any external web sites at your own risk. I am not responsible for any possible spiritual, emotional, physical, financial or any other damage to you, your friends, family, ancestors, or descendants in the past, present, or future, living or dead, in this dimension or any other.

 

Use any example programs at your own risk. I am not responsible if they blow up your computer or melt your Atari 2600. Use assembly language at your own risk. I am not responsible if assembly language makes you cry or gives you brain damage.

 

Home Inventions Quotations Game Design Atari Memories Personal Pages About Site Map Contact Privacy Policy Tip Jar