Skip navigation
NintendoAge
Welcome, Guest! Please Login or Join
Loading...

Homebrew Nerdy Nights week 1 number systems and core programming ideas

Dec 4, 2007 at 11:32:18 PM
bunnyboy (81)
avatar
(Funktastic B) < Master Higgins >
Posts: 7704 - Joined: 02/28/2007
California
Profile
Number Systems
Decimal
The decimal system is base 10. Every digit can be 0-9. Each digit place is a power of 10. Each digit place to the left is 10 times more than the previous digit place. If you take the number 10 and put a 0 to the right, it becomes 100 which is 10 times more. Remove the 0 from the right, it becomes 1 which is 10 times less.
 
100's place    10's place    1's place
    0              0            1         = 001
    0              1            0         = 010
    1              0            0         = 100

To get the value of a number, you multiply each digit by it's place value and add them all together.
 
100's place    10's place    1's place
    3              8            0         = 3*100 + 8*10 + 0*1 = 380
    0              4            1         = 0*100 + 4*10 + 1*1 = 41

Binary
Everything in computers is done in base 2, binary. This is because the lowest level of computing is a switch; on/off, 1/0.

Base 2 binary works the same way, except each digit can be 0-1 and the place values are powers of 2 instead of 10. Insert a 0 to the right of a number and it becomes 2 times bigger. Remove a 0 and it becomes 2 times smaller.
 
8's place    4's place    2's place   1's place
   0            1            0           0       = 0*8 + 1*4 + 0*2 + 0*1 = 4
   1            1            1           1       = 1*8 + 1*4 + 1*2 + 1*1 = 15

The NES is an 8 bit system, which means the binary number it works with are 8 binary digits long. 8 bits is one byte. Some examples are:
 
 Binary    Decimal
00000000 =  0
00001111 =  15
00010000 =  16
10101010 =  170
11111111 =  255

Eventually you become fast at reading binary numbers, or at least recognizing patterns. You can see that one byte can only range from 0-255. For numbers bigger than that you must use 2 or more bytes. There are also no negative numbers. More on that later.


Hexadecimal
Hexadecimal or Hex is base 16, so each digit is 0-15 and each digit place is a power of 16. The problem is anything 10 and above needs 2 digits. To fix this letters are used instead of numbers starting with A:
 
Decimal   Hex
    0   =  0
    1   =  1
    9   =  9
   10   =  A
   11   =  B
   12   =  C
   13   =  D
   14   =  E
   15   =  F

As with decimal and hex the digit places are each a power of 16:
 
 16's place   1's place
     6           A       = 6*16 + A(10)*1 = 106
     1           0       = 1*16 +     0*1 = 16

Hex is largely used because it is much faster to write than binary. An 8 digit binary number turns into a 2 digit hex number:
 
Binary     01101010
split        |  |
in half     /    \
         0110   1010
into       |     |
 hex       6     A
           |     |
 put       \    /
 back        6A
 

01101010 = 6A


And more examples:
Binary     Hex   Decimal 
00000000 = 00  = 0
00001111 = 0F  = 15
00010000 = 10  = 16
10101010 = AA  = 170
11111111 = FF  = 255

For easy converting open up the built in Windows calculator and switch it to scientific mode. Choose the base (Hex, Dec, or Bin), type the number, then switch to another base.

When the numbers are written an extra character is added so you can tell which base is being used. Binary is typically prefixed with a %, like %00001111. Hex is prefixed with a $ like $2A. Some other conventions are postfixing binary with a b like 00001111b and postfixing hex with an h like 2Ah.

The NES has a 16 bit address bus (more on that later), so it can access 2^16 bytes of memory. 16 binary digits turns into 4 hex digits, so typical NES addresses look like $8000, $FFFF, and $4017.


Core Programming Concepts
All programming languages have three basic concepts. They are instructions, variables, and control flow. If any of those three are missing it is no longer a true programming language. For example HTML has no control flow so it is not a programming language.

Instructions
An instruction is the smallest command that the processor runs. Instructions are run one at a time, one after another. In the NES processor there are only 56 instructions. Typically around 10 of those will be used constantly, and at least 10 will be completely ignored. Some examples of these would be addition, loading a number, or comparing a variable to zero.

Variables
A variable is a place that stores data that can be modified. An example of this would be the vertical position of Mario on the screen. It can be changed any time during the game. Variables in source code all have names you set, so it would be something like MarioHorizPosition.

Control Flow
Normally your instructions run in sequential order. Sometimes you will want to run a different section of code depending on a variable. This would be a control flow statement which changes the normal flow of your program. An example would be if Mario is falling, jump to the code that checks if he hit the ground yet.



NEXT WEEK: basic NES architecture


Edited: 08/03/2016 at 12:44 PM by NintendoAge Moderator

Dec 5, 2007 at 12:34:42 AM
mb7241 (183)
avatar
(Matt B.) < Bowser >
Posts: 7167 - Joined: 05/12/2007
Tennessee
Profile
This reminds me of one of my old programming courses in college. Fun stuff .

-------------------------

Answering any and all questions related to video game collecting.  Guaranteed true and honest answers from a man with nearly 20 years in the community.  Prices for these answers vary depending on the question.  For all inquiries, please PM me.


Dec 5, 2007 at 12:41:56 AM
NationalGameDepot (279)
avatar
(Dr. NGD) < Bonk >
Posts: 15286 - Joined: 08/16/2006
Tennessee
Profile
I just complete lesson 1, and had a pretty tough time with it. I don't know jack shit about this type of stuff but I hope I can learn.
~~NGD

-------------------------
Proud replier of post #1000 in the infamous Joel thread

Dec 5, 2007 at 1:09:24 AM
Little Samson (27)
avatar
(D H) < Meka Chicken >
Posts: 916 - Joined: 09/20/2007
Maryland
Profile
This is great stuff — I was familiar with some of the concepts, but not with how they play into the way the NES runs programs. I'm looking forward to this series of threads!

-------------------------
 

Dec 5, 2007 at 4:32:30 AM
KHAN Games (89)
avatar
(Kevin Hanley) < Master Higgins >
Posts: 8124 - Joined: 06/21/2007
Florida
Profile
Ahhh my brain hurts.

-------------------------

gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin


Dec 5, 2007 at 9:26:38 AM
NewUser123456789 (226)

(New User) < Bonk >
Posts: 17574 - Joined: 07/02/2007
Other
Profile
Great Lesson Bunnyboy..

I'm looking forward to this..


Dec 5, 2007 at 11:42:13 AM
KennyB (14)
avatar
(No Go ogle result) < Eggplant Wizard >
Posts: 466 - Joined: 09/06/2006
Belgium
Profile
Great lesson , can't wait for the next one !

-------------------------
 

Dec 5, 2007 at 12:46:21 PM
Shags (65)
avatar
(--- Ben ---) < El Ripper >
Posts: 1003 - Joined: 06/01/2007
Alaska
Profile
this looks like it is going to be very good,
 can't wait for the next lesson

Dec 5, 2007 at 10:43:58 PM
burnambill333 (0)
This user has been banned -- click for more information.
(Nicholas Morgan) < King Solomon >
Posts: 3845 - Joined: 11/12/2006
New Jersey
Profile
Well, I finally got a chance to go through and read this. It looks good so far Brian. A few questions:

1 - In scientific mode on windows calculator, what is the "Oct" number system? I'm just curious about this really.

2 - Can't binary represent letters as well? For example, on this site you can convert binary to text and back again, which can hold letters, numbers, and punctuation.

3 - In assembly language, can variables be named anything? For example, would "MarioHorizPosition" be a valid name for a variable? What are the naming restrictions?

-------------------------
Don't worry about the world coming to an end today. It's already tomorrow in Australia!

Dec 6, 2007 at 12:53:24 AM
bunnyboy (81)
avatar
(Funktastic B) < Master Higgins >
Posts: 7704 - Joined: 02/28/2007
California
Profile
Originally posted by: burnambill333 1 - In scientific mode on windows calculator, what is the "Oct" number system? I'm just curious about this really.

Oct is Octal, base 8. Not really used in computing anymore. You should be able to figure out how it works based on the descriptions of bin/dec/hex

Originally posted by: burnambill333 2 - Can't binary represent letters as well? For example, on this site you can convert binary to text and back again, which can hold letters, numbers, and punctuation.

The ascii table is the standard conversion between a number and a letter or other character. The main benefits of ascii are that the letters are sequential (which seems like an obviously good idea) and you can change between lower case and upper case by adding or subtracting the same number. One byte holds almost all characters including punctuation. It is slowly being replaced by unicode, which uses more than one byte so it can do things like Chinese characters.

Originally posted by: burnambill333 3 - In assembly language, can variables be named anything? For example, would "MarioHorizPosition" be a valid name for a variable? What are the naming restrictions?
You set the name so it can be anything that doesn't have special characters like spaces or colons. There are some typical capitalizing conventions for naming which will come in a later lesson, but that again gets into personal style. Usually function names have the first letters capitalized (MarioJumpUp), variables have first letter not caps (marioVertPos), and constants are all caps (MARIOSPEED).

Dec 6, 2007 at 1:23:37 AM
jimpoleshuk (279)
avatar
(Jim Poleshuk) < King Solomon >
Posts: 3332 - Joined: 09/21/2007
Nebraska
Profile
Very Nice BunnyBoy

-------------------------

www.videogameboxprotectors.com


May 29, 2008 at 5:39:23 PM
Stan (81)
avatar
(Demonologist and Linguist Supreme) < Ridley Wrangler >
Posts: 2766 - Joined: 12/31/2006
Virginia
Profile
Originally posted by: NationalGameDepot

I just complete lesson 1, and had a pretty tough time with it. I don't know jack shit about this type of stuff but I hope I can learn.
~~NGD


N, check out my binary/hex and such tutorial.  They apply to all programming and I go into great depth.  We need to maybe put this on the main site here somehow over time, it would be a good idea.

http://www.smstributes.co.uk/view_article.asp?articleid=40

The first three sections (up through binary math), will tell you all you need to know.  There are some SMS references in there, but just to illustrate a bit.  Also, buns, maybe we should put together an assembly tutorial starting from real basics.  The instruction sets should be illustrated very slowly in my opinion, getting to how they function later, showing 'programs' that essentially look like this:

ld bc, mpdand

ld b, 8

ld c, 0

and so forth, showing essentially fake programs so users learn how to work with fake data before moving on to the real deal.  Again, not trying to say anything bad, you've done some awesome work here, I think it would be good if it was fleshed out more.


Jun 23, 2011 at 7:35:58 PM
toshi5o3 (64)
avatar
(Sergio E.) < Meka Chicken >
Posts: 807 - Joined: 03/14/2011
California
Profile
I completed this lesson last night! woooooo!!! Cheers! 

-------------------------
"A Winner is You" -- Real audio on a cart? Full length NES album? Now you're listening with power. Play it on your AVS (or toaster): RetroUSB.com

Feb 10, 2012 at 9:57:44 AM
Farid (0)
avatar
(Farid Abbasi) < Cherub >
Posts: 15 - Joined: 04/03/2010
Iran (Islamic Republic of)
Profile
First of all thanks for the great tutorial.

There is a slight typo :
 
Base 2 binary works the same way, except each digit can be 0-1 and the place values are powers of 2 instead of 10. Insert a 0 to the right of a number and it becomes 2 times bigger. Remove a 0 and it becomes 10 times smaller.


It must be "2 times" not 10 times. For example :

1110b = 14d
111b = 7d

1010b = 10d
101b = 5d

As you can see removing 0 from right makes the number only 2 times smaller not 10 times.

Feb 12, 2012 at 11:07:59 PM
muffins (0)
avatar
(I am Error.) < Tourian Tourist >
Posts: 44 - Joined: 01/31/2012
Maine
Profile
Originally posted by: Kunio

First of all thanks for the great tutorial.

There is a slight typo :
 
Base 2 binary works the same way, except each digit can be 0-1 and the place values are powers of 2 instead of 10. Insert a 0 to the right of a number and it becomes 2 times bigger. Remove a 0 and it becomes 10 times smaller.


It must be "2 times" not 10 times. For example :

1110b = 14d
111b = 7d

1010b = 10d
101b = 5d

As you can see removing 0 from right makes the number only 2 times smaller not 10 times.


I'm thinking this was a binary joke (since %10 = 2).  Maybe a tutorial on binary isn't the best place for it but chances are if you're prepared to program the NES you've had some exposure to binary already.

-------------------------


Feb 13, 2012 at 3:13:29 AM
glenn101 (0)
avatar
(Glenn Kirilov) < Tourian Tourist >
Posts: 37 - Joined: 02/11/2012
Victoria
Profile
Kunio is right, it should be 2 times smaller, it must be a binary joke as muffins suggested.

Feb 13, 2012 at 11:32:55 AM
bunnyboy (81)
avatar
(Funktastic B) < Master Higgins >
Posts: 7704 - Joined: 02/28/2007
California
Profile
Not a joke, fixed!

Feb 13, 2012 at 12:19:02 PM
mlbfan10 (100)
avatar
(Jason Trogdon) < Lolo Lord >
Posts: 1632 - Joined: 11/19/2009
North Carolina
Profile
Great stuff man! I'm glad you make more sense than my college professors did. I still struggle with programming, if you any pointers on how I can try to improve my comfort zone and ability I would be grateful. I've always been intrigued with programming but it is my weakest point.

edit: found the answer to my question.


Edited: 02/13/2012 at 03:41 PM by mlbfan10

Feb 13, 2012 at 12:20:36 PM
dra600n (300)
avatar
(Adym \m/) < Bonk >
Posts: 16975 - Joined: 09/16/2010
Connecticut
Profile
Shoulda just gone with it bunny

-------------------------
Proud owner of post #1800 in Inner Circle HQ thread

Mar 9, 2013 at 12:31:33 PM
XYZ (76)
avatar
< King Solomon >
Posts: 3985 - Joined: 03/15/2012
Ohio
Profile
Bunnyboy I know this is an old threat but I want to say I really enjoy the nerdy nights series. Thanks for these!

Feb 4, 2016 at 5:49:21 PM
WhiteCastle10 (0)
avatar
< Cherub >
Posts: 2 - Joined: 02/02/2016
Ohio
Profile
I've always wanted to make a classic NES game and I'm just now starting in on my development. I know I'll be referencing this thread a bunch. Thanks again for adding this!

-------------------------
"Leave Luck to Heaven"

Feb 4, 2016 at 9:00:24 PM
KHAN Games (89)
avatar
(Kevin Hanley) < Master Higgins >
Posts: 8124 - Joined: 06/21/2007
Florida
Profile
Be sure to ask any questions you'd like in the "Ask all programming questions here!" thread, or any of the Nerdy Nights threads if you'd like!

Welcome to the site.

-------------------------

gauauu: look, we all paid $10K at some point in our lives for the privilege of hanging out with Kevin


Jul 26, 2016 at 4:30:02 PM
Nesmaniac (3)
avatar
(Matthew Sizemore) < Eggplant Wizard >
Posts: 421 - Joined: 12/14/2010
Kentucky
Profile
Thanks for posting this information. I can't imagine how many countless hours was spent figuring all this out. I'm thinking about learning all I can about the NES programming in hopes of possibly creating my own NES game someday but even if I don't I find it fascinating to see a basic concept of how the games I grew up playing actually worked.

-------------------------
 

Jul 27, 2016 at 2:05:37 AM
Mega Mario Man (63)
avatar
(Tim ) < Ridley Wrangler >
Posts: 2743 - Joined: 02/13/2014
Nebraska
Profile
Best of luck! Its a lot of fun to go through and much more work after you completed the tutorials. I only wish I had more time to program, but I enjoy the time I do.

-------------------------
Current Project
Isometric Survival Horror

Older Projects
Tailgate Party, Power Pad Demo, Happy Hour

Links
Store, Facebook, Twitter

Jan 8, 2017 at 7:21:47 PM
Candied Hat (0)
avatar
< Cherub >
Posts: 1 - Joined: 01/08/2017
Alabama
Profile
Amazing!
I'm lazy, so I just took your files and ran.
I now realize that you had all those at your website.