Bally BASIC Hexadecimal/Decimal Conversion Process -------------------------------------------------- Version 1.0 June 2, 2013 This tutorial was created and is maintained by Adam Trionfo (ballyalley@hotmail.com) This conversion process was first discussed on the Bally Alley discussion board on May 21 and 22, 2013. It seems useful to have this information easily accessible on BallyAlley.com, so I've made a short tutorial out of it. This conversion process relies on and builds upon methods discussed in 1982's Bally Arcade/Astrocade booklet called "Z-80 Mini Course" by Larry Simioni. For reference, here is the book's location online: http://www.ballyalley.com/ml/ml_docs/Z80%20Mini%20Course/Z80%20Mini%20Course%20 (1982)(Larry%20Simioni).pdf Our First Conversion Example ---------------------------- Following the conversion instructions on pages 11 and 12 of the "Z80 Mini Course" we'll first work an example. Let's say that in Bally BASIC issuing the command: PRINT %(24576) results with 28193. We're telling BASIC to PRINT the value at location 24576, which is the first byte of the Blue Ram's expanded memory on the Astrocade. Decimal 24576 is equal to 6000h (6000 hexadecimal, or $6000). What's the hexadecimal value of the result mean? We'll have to "translate" from hex to Bally BASIC. First we're going to convert the long way, so that we understand what's going on behind the conversion process, later it will be easier using a calculator that can convert from decimal to hex, which will make the process faster, cleaner and easier. Strap yourself in because here we go: 1) When PRINT %(24576) is issued and Bally BASIC replies with 28193, that is giving the value of TWO hexadecimal numbers at once. Our task is to figure out what these two numbers are. 2) For details about WHY Bally BASIC gives you two bytes back read pages 11 and 12 of the "Z80 Mini Course." I'm going to give the cookbook method for this example. Follow this process for this example, even if the reasoning behind it isn't understood, and it will work out. Now for a little bit of cheating. I'm going to give you the answer FIRST and then we're going to check our work. The hex value expected at address $6000 (24576) is $216E. Let's check if our result from the PRINT statement, 28193, is correct: 1. Break the two byte value of $216E into two separate bytes: $21 $6E 2. Now we'll make the conversion that Bally BASIC (and AstroBASIC) makes when it reads the values and PRINTs it out. 3. Flip the two separate bytes. So $21 $6E becomes $6E $21. 4. Now convert the values to decimal and multiply by column value and then sum the columns. Like this: 6 x 4096 = 24,576 14 [$E] x 256 = 3,584 2 x 16 = 32 1 x 1 = 1 + ------ 28,193 5. The sum of the values gives our correct answer: 28,193. However, BASIC isn't done yet. 6. BASIC next checks if 28,193 is larger than 32,767. If yes, then BASIC subtracts 65,536, else it leaves the value alone. In our example the value is left alone. 7. This leaves us with the final result of 28,193. As strange as it may look, the two-byte value at memory location $6000 of $216E is exactly equal to 28,193. You just had to know how to do the math. So, is it always this complicated? Yes... but it doesn't have to be worked by hand. Thankfully, some of the first BASIC programs written and published in Bally BASIC did these conversions for you. We can use a more modern method, which I'll show now. The method previously shown was just to follow the method laid out in the "Z80 Mini Course." Here are the shortcut methods, choose the one that works for the conversion that you need to do: I) Convert Bally BASIC Hexadecimal to Decimal: ------------------------------------------- 1. Flip the two separate bytes stored. In the previous example at memory location $6000 is $216E. It becomes $6E21. 2. Using a calculator that can make decimal/hex conversions (like the one included with Windows) enter $6E21 in hex and then press decimal to convert it to decimal. 3. The calculator's result is 28,193. 4. Is the result larger than 32,767? If yes, then subtract 65,536, else leave the value alone. II) Convert Bally BASIC positive Decimal Numbers to Hexadecimal: ------------------------------------------------------------ 1) Convert the decimal number to hex using a calculator. So 28,193 becomes $6E21. 2) All hex numbers must be four digits long for this process to work. For example, if you convert 1 to hex you get $1. You must supply the leading zeros. $1 would become $0001. Another example would be: 255. This would be 255 = $FF. Make it four digits: $00FF. 3) Flip the two separate bytes. $6E21 becomes $216E. Or $0001 becomes $0100. Or $00FF becomes $FF00. III) Convert Bally BASIC Negative Decimal to Hexadecimal: ---------------------------------------------------- 1) Add 65,536 to the negative decimal number. We'll use the a very large number -2. So, -2 + 65,536 is 65,534. 2) Convert the decimal number to hex using a calculator. So 65,534 becomes $FFFE 3) Flip the two separate bytes. $FFFE becomes $FEFF. That's really all that there is to it. I hope that this helps you to understand the conversion process between Bally BASIC and hexadecimal values. Appendix - Changes/Updates -------------------------- Version 1.0 (June 2, 2013) - First release of the tutorial via BallyAlley.com