http://calcg.org/newlogo2.png Not Logged in.
Login | Register

Page 2: Bases, Bytes, and Code Input

Counting 

Base 10, or Decimal, is what the world uses.  "Base 10" means that there are 10 possibilites for each numbers place.  For example, Y in 452Y2 could be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.  Base 9 could have numbers 0-8 in each place, since there would are 9 possibilites for each place and you start at 0 when counting the possibilities.  You always start at 0.

You might be wondering what it means for something to be in a certain base.  We all know how counting goes in the Decimal system from 1 to 20:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Well, here's how it looks to count from 1 to 2010 (20, base 10) in base 9:

1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22

And in base 5:

1, 2, 3, 4, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31, 32, 33, 34, 40 

When you get over base 10, you start using letters after you get past 9.  So, base 16:

1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, 13, 14

I hope you get the point: "10" in base X is equal to X.  104=4.  Also, single-digit numbers are the same in any base.  For this next part, you need to know that 5938210=5*104+9*103+3*102+8*101+2*100, that 37610=3*102+7*101+6*100, and that 59483772849612039486410=something following the same pattern with powers of 10.

Base Conversions

Converting numbers of other bases to Decimal is similarly done:  12202013= 1*36+2*35+2*34+0*33+2*32+0*31+1*30.  Likewise, 34A0B11=3*114+4*113+10*112+0*111+12*110 because A=10 and B=12, always.  Converting things to base 10 this way works because the math is being done in base 10.  That means that, if you're trying to convert to base 19, 958473620110=(9*109+5*108+8*107+4*106+7*105+3*104+6*103+2*102+0*101+1*100)19, meaning that all of that math was done in base 19. Remember that, in base 19, "10"=19.  Get it all?  I'm hoping that you test around with these numbes on your calc and some base converter while you're reading.  You'll understand everything better if you constantly mess around with what you're learning to build a personal comprehension.

It's important be familiar with this stuff because you calc works in base 16.  Now, if users didn't prefer to read numbers in Decimal, then this would be fine.  The problem is that they do and it's not.  I'll show you a couple ways to get from base to base in code-possibly ways, though it's the concepts that you should be trying to grasp right now.  The above method would work for getting Decimal numbers to base 16; Here's one method, workable by hand, for going in the opposite direction (converting a number from one base to another base with calculations done in the first base):

Base 10 to Base 2

Let's say you wanted to get 23710 to base 2 using base 10.  Find the highest power of two that is less than or equal to 237: 2^7=128.  Write down a 1:

1

Then subtract 128 from 237 to get 109.  2^6 (64) is lesser or equal to 109, so we'll subtract it for 45 and concatenate another 1.

11

2^5, or 32, is still less than or equal to 45.  Subracting 32 from 45 yields 13.  Same pattern:

111

Alas, something different happens here.  2^4, 16, is greater than 13.  We're trying to get the original 237 down to 0, and that won't happen with negative numbers.  We'll skip it, meaning that a 0's added to our base 2 number.

1110

13-2^3=13-8=5:

11101

5-2^2=5-4=1:

111011

We'll get another zero on this next one, because 2^1 (2) is greater than the 1 that we're down to from 237.  Skip it:

1110110

Lastly, 1-2^0=1-1=0.  Last base 2 digit:

111011012=23710

To summarize, you find the highest exponent of two that's lesser or equal to the number you're trying to convert, subtract it, move to the exponent of two that's just lower than the first one, only subtract it if it's lesser than or equal to the new base 10 number that you have, add a "1" or a "0"according to whether or not you could subtract the exponent, and keep moving down the line until you get to 2^0.  If you do it right, then you'll never have a remainder or anything.  Looking back at that example above: 111011012=(2^7+2^6+2^5+2^3+2^2+2^0)10=23710.Using this method to convert base 16 numbers to base 7 is nearly the same:

Base 16 to Base 7

Base 16 is the best thing in the entire world.  Still, we have to face the fact that Decimal is what everyone's familiar with.  Most calculators work only in Decimal, and base 10 is what most people can read numbers best in.  Therefore, it would be wise to go from base 16 to base 10 to base 7 instead of cutting straight from 16 to 7.

To start off, we'll use an idea that we learned at the beginning of this section: 

A28C516=10*16^4+2*16^3+8*16^2+12*16^1+5*16^1=655360+8192+2048+192+5=66579710

Highest exponent of 7 that's less than or equal to 665797:7^6.  We're not working in base 2 anymore, which means that we need numbers to multiply each exponent by.  So, what's the highest number (coefficient) that we can multiply 7^6 by so that it comes out to be less than or equal to 665797?  I say 5.  665797-5*7^6=77552

5

How about a coefficient for 7^5?  Well, 5*7^5>77552 and 4*7^5 isn't.  Let's go with that one... 77552-4*7^5=10324

54

Following the same pattern: 10324-4*7^4=720 (=6 factorial! :P)

544

720-2*7^3=34

5442

Whoops, it looks like 7^2>34.  We'll add a zero and go on to the next exponent, remember?

54420

34-4*7^1=6

544204

And, lastly, 6-6*7^0=0.

54420467

A28C516=66579710=5*7^6+4*7^5+4*7^4+2*7^3+0*7^2+4*7^1+6*7^0.  Although you can get by with an external base converter, you'll build a much greater understanding for what lies ahead if you come back later to read this garbage if you didn't understand it all the first time around.  Feel free to ask questions in the forum, too.

The Byte

Before we get into that, just know that hexadecimal means "base 16".  Also, binary means "base 2".  Those are two bases that you'll need to be very familiar with.  Because they'll be used frequently throught this tutorial, hexadecimal numbers will always be green and binary ones will be blue.  That should be much easier to get used to than having to check the subscripts for the base every time you see a number.  Decimal numbers are used too frequently to be colored.

A regular old byte can be considered to be a number capable of being any integer from 0 to 255, 00 to FF, or 00000000 to 11111111.  You probably know that zeros to the right of the last significant digit aren't necessary, but they'll still make things easier to read.  The Z80 works mostly with bytes at a time and thus is an 8-bit processor, since 8 bits=1 bytes (a bit is either 1 or 0).  When you're examining a byte, it will probably be in binary.  The bits of a byte go from the leftmost one ((0 or 1)*2^7) to the right one.  The leftmost bit is called the most significant bit because its value has the largest impact on the value of the whole byte.  The rightmost bit is called the least significant bit because it sucks.  Theyre numbered as follows:

bit 7
bit 6
bit 5
bit 4
bit 3
bit 2
bit 1
bit 0

The numbering will be important for several reasons later on.

Code, Finally!

Ok, whip your calculator out of your pocket and spin the case around so that it's covering the the back of the calc instead of the front.  Practice this awhile until you can do it out in public or in your math class without not making it look like the coolest thing ever.

Then turn it on.  Press [PRGM], the left arrow key, and [ENTER] to be prompted with the "Give this boy a name!" screen.  Name it something and press [ENTER].  You should be in the program editor.  The first line of any Assembly program should be "AsmPrgm".  Even if you have lowercase letters enabled, don't try typing it in.  It may look fine, but it doesn't work.  Instead, press [2ND], [0] to get to the catalog.  Scroll down until you find it:

Eat it for breakfast

Press [ENTER] to get it in the program and press [ENTER] again.  Now, remember that the Z80 works with bytes at a time.  All code input will be in hexadecimal (don't worry, you'll get used to it), so the only valid characters to use are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.  Think back: how many hexadecimal digits are in one byte?  Two.  Actually, you'll get an error if you ever try to run a program with an odd number of characters.  Type in C9 (for you calc newbies, press [ALPHA] to access the letters in green above each key on the keypad).  C9 is ret, which you'll learn later on.  You'll need it at the end of every program you make, or it'll crash.  Speaking of crashes, you can alternatively type in 18FE (jr onelinebeforethisone) to put your calc in an endless loop that can't be gotten out of until you remove a battery and put it back in.  Please don't actually run it unless you've backed up everything else on your calc beforehand...  Back to C9:

Aww... isn't it cute?

It should look like that.  Press [2ND], [DEL] to get back to the home screen and find the catalog again.  This time, get "Asm(" from it, press [ENTER], [PRGM] , find the new program that you just made, and press [ENTER].  The home screen should show "Asm(prgmSOMETHING", and, if it does, press [ENTER] one more time.  If you did everything correctly, you should be greeted with a "Done" message.  Congratulations!

 

Table of Contents

Forum Discussion 

Previous

Next

Portal | My Account | Register | Lost Password or Username | TOS | Disclaimer | Help | Site Search | File Archives Copyright © 2002-2019 CalcG.org