NintendoAge

Loading...

bunnyboy (81)

(Funktastic B) <

Posts: 7666 - 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.

To get the value of a number, you multiply each digit by it's place value and add them all together.

**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.

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:

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:

As with decimal and hex the digit places are each a power of 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:

And more examples:

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**

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

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

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.

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.

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.

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.

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

~~NGD

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

Proud replier of post #1000 in the infamous Joel thread

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!

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

burnambill333
(0)

(Nicholas Morgan) <**King Solomon** >

(Nicholas Morgan) <

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?

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!

bunnyboy (81)

(Funktastic B) <

Posts: 7666 - Joined: 02/28/2007

California
Profile
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

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.

Very Nice BunnyBoy

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.

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

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.

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.

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

bunnyboy (81)

(Funktastic B) <

Posts: 7666 - Joined: 02/28/2007

California
Profile
Not a joke, fixed!

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.

edit: found the answer to my question.

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"

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.

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

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.

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

Isometric Survival Horror

Tailgate Party, Power Pad Demo, Happy Hour

Store, Facebook, Twitter

I'm lazy, so I just took your files and ran.

I now realize that you had all those at your website.