home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / Information / BONKERS01.TXT < prev    next >
Encoding:
Text File  |  2019-04-13  |  14.1 KB  |  356 lines

  1.                               _Bonkers_
  2.  
  3. Level #01
  4.                          (c) Curtis White 
  5.  
  6. FREEWARE: NO SELL OF THIS MAGAZINE IS ALLOWED WITHOUT PRIOR WRITTEN 
  7.                             PERMISSION.
  8.  
  9. Use the force!
  10.  
  11. This is the first issue of the coding magazine Bonkers. Our mission is 
  12. to provide the reader with a strong background in 65XX machine language. 
  13. This magazine is intended for someone with little or no experience in 
  14. coding.  
  15.  
  16. Before we get started, I'd like to thank Coolhand and Dokken for their 
  17. help in making this the best magazine possible, thanks! 
  18.  
  19.  
  20. Why learn 65xx machine and assembly language? Well, for several reasons. 
  21. It will allow you to code demos, games, applications, and even entire 
  22. operating systems. But more then that, many people find it to be the 
  23. most fun and rewarding thing about computers. And what you learn here 
  24. will be of benefit if you decide to code on other computers.
  25.  
  26. We hope you will enjoy reading and following each issue of Bonkers.    
  27.                     
  28.                                     Signed, The Bonkers Staff
  29.  
  30. Editor: Coolhand                  < Send your opinions, thanks,
  31. Technical Director: Dokken        < and gripes to these clowns!
  32. Author: Light                     < 
  33.  
  34.  
  35. Stage #01 - I turn the light off, I turn the light on.
  36.  
  37. These are the only two states the computer knows, on and off. Every 
  38. digital computer only knows these two properties. Why is that? Because 
  39. the computer uses a series of electrical pulses, and each 'location' in 
  40. the computer either has a charge or doesn't have a charge. It is easy to 
  41. represent off as the number 0 and on as the number 1. In fact, there is 
  42. a number base specifically for this; that system is binary. You may not 
  43. know that us humans use the number base 10, or the decimal base. We call 
  44. a single binary digit (recall this is either on or off) a bit. As you 
  45. would expect, a bit can only be 0 or 1. Now that is not very powerful, 
  46. until you realize that you can string these together to command the 
  47. computer to do anything you wish.
  48.  
  49. %110111 - this is 55 in binary. Note the % tells us that 
  50.           the number does not equal 110,111.
  51.  
  52. Your saying, 'duh duh duh' about now, right? Well, just keep reading! I 
  53. promise it gets better then this.
  54.  
  55. Each bit in a binary number can be given a weight so we can more easily 
  56. understand it.
  57.  
  58.      Weight: 128:64:32:16:8:4:2:1
  59. Binary Code:   0: 0: 0: 0:0:1:0:0 
  60.  
  61. Each one in the binary code is given the weight corresponding to it.
  62.  
  63. So the binary code of 00000100 = 4. Yeah, it isn't easy for us, but it's 
  64. a snap for the computer.
  65.  
  66. Here are some more examples:
  67.  
  68.      Weight:128:64:32:16:8:4:2:1
  69. Binary Code:  1: 1: 1: 1:1:1:1:1 = 255 Decimal
  70.  
  71.      Weight:128:64:32:16:8:4:2:1
  72. Binary Code:  1: 0: 0: 0:0:0:0:1 = 129 Decimal
  73.  
  74.      Weight:128:64:32:16:8:4:2:1
  75. Binary Code:  0: 1: 1: 0:1:0:1:0 = 106 Decimal
  76.  
  77. Have you noticed how each bit to the left has a weight of double the 
  78. previous amount? Yep, that's important. Let's try 16 binary bits now! 
  79. You can understand why coders are always hungry...
  80.  
  81. The weight:
  82. 32768:16384:8192:4096:2048:1024:512:256:128:64:32:16:8:4:2:1
  83.     1:    0:   1:   1:   0:   1:  0:  0:  1: 0: 0: 0:0:1:0:1
  84.  
  85. = 46212 Decimal!
  86.  
  87. Oh no! I can't even verify that on my TI-68 calculator. Guess you'd 
  88. better learn now. No fret, you will not be doing much in binary. You 
  89. just need to know that it's there.
  90.  
  91. Eight bits = 1 BYTE = 255 DECIMAL = 11111111 BINARY
  92.  
  93. Okay.. so we know a bit about binary, bah. Now, lets get into 
  94. hexadecimal, another number system, base 16.  When you represent a 
  95. binary number in hexadecimal you use a group of 4 bits or a nybble.
  96.  
  97. 0000 - 0   0100-4   1000-8   1100-C
  98. 0001 - 1   0101-5   1001-9   1101-D
  99. 0010 - 2   0110-6   1010-A   1110-E
  100. 0011 - 3   0111-7   1011-B   1111-F
  101.  
  102. Here are some examples:
  103.  
  104.     0001 1111 0100:Binary
  105.        1    F    4:Hex
  106.                    500:Decimal
  107.                   
  108.     0001 0100 0110:Binary
  109.            1    4    6:Hex    
  110.                    326:Decimal
  111.  
  112. When counting in hex, it goes like this:
  113.  
  114. HEX\/\/\/
  115. 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 
  116.  
  117. Here is a table to help you when your practicing:
  118.  
  119. $10 - 16d   $1b - 27d  $26 - 38d
  120. $11 - 17d   $1c - 28d  $27 - 39d
  121. $12 - 18d   $1d - 29d  $28 - 40d
  122. $13 - 19d   $1e - 30d  $29 - 41d
  123. $14 - 20d   $1f - 31d  $2a - 42d
  124. $15 - 21d   $20 - 32d  $2b - 43d
  125. $16 - 22d   $21 - 33d  $2c - 44d
  126. $17 - 23d   $22 - 34d  $2d - 45d
  127. $18 - 24d   $23 - 35d  $2e - 46d
  128. $19 - 25d   $24 - 36d  $2f - 47d
  129. $1a - 26d   $25 - 37d  $30 - 48d
  130.     
  131. Useful Notation:  $ for hex numbers
  132.                   % for binary numbers
  133.                   d for decimal numbers
  134.  
  135.  
  136. ___ Power-up Time____
  137.  
  138. Questions //\\//\\//
  139.  
  140. 1.     What number system do computers use?
  141. 2.     What number system do people generally use?
  142. 3.     What number system is a go-between decimal and binary?
  143. 4.     Convert %100001001 TO HEX
  144. 5.     Convert %111100001 TO DECIMAL
  145. 6.     Convert FF to Decimal
  146. 7.     Convert FA to Decimal
  147. 8.     Convert FB to Binary
  148. 9.     Convert FC to Binary
  149.  
  150. ___Stage Boss___
  151.  
  152. Finish the chart up to 255 decimal! :D
  153.  
  154. Stage #01 Completed_
  155.  
  156. ______Things You've Learned_____
  157.  
  158. You learned about binary, decimal, and hexadecimal. You learned how to 
  159. convert between these three popular number systems. You've gained an 
  160. understanding of the internal workings of the computer.
  161.  
  162.  
  163. Stage #02 - Get Ready!
  164.  
  165.  
  166. Here are some things you may already know about your computer.. but just 
  167. in case.
  168.  
  169. RAM: Random Access Memory. You can read and write to it.
  170. ROM: Read Only Memory. You can only read from it, i.e.,
  171.      the basic program that the computer runs when it boots.    
  172.  IA: Interface Adapter. These is includes your SID, VIC,     
  173.      PIA.
  174.  
  175. Inside the CPU.
  176.  
  177. I'm sure you've heard a lot about the CPU especially with all the new 
  178. chips out.  Let's see how it works for real.
  179.  
  180. Virtual View Of 65XX chip:
  181.  
  182.          PC(16 bits): Program Counter
  183. Accumulator (8 bits): What type of name is that?!  Consider it 
  184.                       the heart of the cpu, just as the cpu 
  185.                       is the heart of the computer.
  186.  X Register (8 bits): An indexing register, you will learn  
  187.                       how this works soon.
  188.  Y Register (8 bits): Another indexing register, similar
  189.                       in function to the X Register.    
  190.      Status Register: This will be important when we start    
  191.                       doing compares.    
  192.  
  193. An important aspect of the computer is the layout of memory. We will not
  194. go into much detail on this, but we need to cover it. Every location or
  195. address in your computer contains a value of 0 to 255 or a byte. So the
  196. location 49152 might contain the byte 155.  When we read the location
  197. 49152, we 'copy'  this value into the accumulator or another machine
  198. language register. We might want to do this to 'paste' it somewhere
  199. else. Can you think of some reasons we would want to do this? Well, we
  200. might do this in a scroll or moving a stick man across the screen. Other
  201. examples include reading the joystick, accepting input in a word
  202. processing program, and much more!
  203.  
  204. We are almost ready to code, so we will go over two very useful machine 
  205. language instructions. LDA which means 'load accumulator', one of the 
  206. most used instructions. What is this load accumulator? Well, it 'loads' 
  207. a value from memory, like when you load a program. Hmm.. consider it a 
  208. grabber. You can reach in and grab memory from anywhere in your 
  209. computer. You do this with LDA. Look at this:
  210.  
  211. BYTE OF MEMORY=5         ACCUMULATOR=0    - before LDA
  212. LDA Byte OF MEMORY       ACCUMULATOR=5    - after LDA
  213.  
  214. The other instruction we will look at is STA (Store Accumulator To 
  215. Address), like this:
  216.  
  217. BYTE OF MEMORY=5         ACCUMULATOR=25    - before STA
  218. STA Byte Of Memory       Byte Of Memory=25 - after STA
  219.  
  220. If we look at LDA as the copy instruction, then STA is the paste (or 
  221. grab and drop). The accumulator serves as the clip board of sorts. 
  222.  
  223. Time to jam, time to code. First, you will need a machine language 
  224. monitor. I will be using Gnome Kit 64. Since monitors are usually not 
  225. exactly the same, you will have to see what does what in your monitor 
  226. (or better use Gnome Kit 64).
  227.  
  228. .49152 LDA 49159 - Tag this, we may have to change it.
  229. .49155 STA 49160 - Tag this..
  230. .49158 RTS       - Returns us to Basic.                  
  231.  
  232. When you type the first line in, your monitor should give you the .491XX 
  233. on the next line, and of course don't type anything after the - (those 
  234. are remarks which the monitor will not understand). If you get a '?', 
  235. then that usually means your typing something in that is wrong; enter 
  236. the line again. Notice the 'tag this'. We wanted to load in the byte 
  237. right after the end of our code and store it to the next location; so if 
  238. we had guessed where our code would end wrong, we would have had to 
  239. change it. Before you execute this, do a:
  240.  
  241. .49159 B - (we must verify what is at this location)
  242.  
  243. It will give you a list of numbers. The first number will be what's 
  244. stored at 49159; the next number will be 49160. We are moving 49159 to 
  245. 49160, so make sure they are different. If not, then type over the 
  246. number that is there and press return. Now, we will 'run' the code with 
  247. an sys 49152. After doing this, you should have your cursor back with no 
  248. apparent change. Now type .49159 b. Did the location of 49160 change? It 
  249. should have. If it didn't, then check your typing. 
  250.  
  251. Let's try something else as well. We are now going to disassemble what 
  252. we just now coded! Type.. .49152 b. You should get something like:
  253.  
  254. .49152 b 173 7 192 141 8
  255.  
  256. The 173 is the op-code for LDA absolute (we will learn 'immediate', and 
  257. other modes later on). 7 is the low byte of 49152, and 192 is the high 
  258. byte of 49152. Notice how the low byte is first and the high byte is 
  259. second. This is called low byte, high byte order. You should remember 
  260. this, as it's an important feature of the 65xx architecture. Wait, your 
  261. saying how in the world does 7, 192 = 49152 (multiply 192*256 and add 7, 
  262. te da!).
  263.  
  264. Make sure your understanding everything as we are going over it, because 
  265. we are walking now - soon we will be flying. 
  266.  
  267. The task...
  268.  
  269. Suppose we were walking down the street, and someone came up and said "I 
  270. can't type. When I type, I get so excited that I always type the words 
  271. in backwards; here's a million dollars, fix this!". Well, we won't code 
  272. a general application (yet), but we can code a specific example.
  273.  
  274. First, a quick diversion. We need to know how screen memory works. 
  275. Briefly, screen memory starts at 1024 and goes through 2023.  By placing 
  276. any of the screen codes in these locations, we can form letters and, in 
  277. turn, make entire words. It's arranged in a matrix of 40 columns by 25 
  278. rows. Reading any of these locations tells us the character at that 
  279. particular location; writing any of these locations will place the 
  280. character at that location. For reference, 1024 is the first location 
  281. which is located in the upper left hand corner of the screen. This would 
  282. be column 0 and row 0. Column 1 and row 0 would place you at location 
  283. 1025. Row 1, Column 1 would be location 1065. The character placed there 
  284. will be dependent on how the value correlates to the screen codes. 
  285.  
  286. If your still not sure how screen memory works, consult the screen 
  287. location table in any of several popular c64 programming books. Okay, 
  288. now back to our million dollar program.
  289.  
  290. .49152 LDA 1024 - Grab value at 1024
  291. .49155 LDX 1025 - Grab value into 1025
  292. .49158 LDY 1026 - Grab value at 1026
  293. .49161 STA 1026 - Drop value into 1026
  294. .49164 STX 1025 - Drop value into 1025, no change
  295. .49167 STY 1024 - Drop value into 1024
  296. .49170 RTS
  297.  
  298. ;type 'rac' in the upper left hand corner of the screen.
  299.  
  300. When you execute the code, you should get 'car'. Notice the four new 
  301. instructions, LDY, STY, LDX, STX. Can you guess what these do? Well, if 
  302. you said LDY (load memory into y register), STY (store y register to 
  303. memory), LDX (load memory into x register), and STX (store x register to 
  304. memory), then you are precisely right. These four new instructions work 
  305. just as LDA and STA do. In fact, just as there is an accumulator, there 
  306. are X and Y registers. For now, we can consider each of these having the 
  307. same properties, but we will learn the unique properties of each of 
  308. these various registers in the coming Levels.
  309.  
  310. ___ Power-up Time ____
  311.  
  312. Questions //\\//\\//
  313.  
  314. 1.     What instructions can we use to 'grab' or load values  
  315.     from memory into our registers?
  316. 2.     What instructions can we use to 'drop' or store 
  317.     information to memory?
  318. 3.     Write a machine language program to copy your name from 
  319.     somewhere on the screen to another place on the screen. 
  320. 4.     Write a machine language program to read in a word or 
  321.     words and paste them vertically down the screen.
  322.  
  323. ___Stage Boss___
  324.  
  325. Code a machine language program to read in your name (from screen memory 
  326. or otherwise) and copy it to the screen. Try at least four ways. If you 
  327. really feel up to it, do six!
  328.  
  329. Like geometric shapes, circles, boxes, lines, - oops, no more ideas. 
  330. Hint, you may need to look at the layout of screen memory.
  331.  
  332. Stage #02 Completed_
  333.  
  334. ______Things You've Learned_____
  335.  
  336. Well, you have learned a lot in this stage. You worked with the monitor. 
  337. You learned about loading (lda,ldy,ldx) and storing to memory 
  338. (sta,sty,stx). You created a million dollar program and experimented 
  339. with screen memory.  
  340.  
  341. _____Level #01 Completed_____
  342.  
  343. That's right, it's the end for this first issue of Bonkers. We will try 
  344. to release on a very regular basis, hopefully, every week or at least 
  345. every other week. And since we are a current magazine, if you have any 
  346. questions, then you can always drop us an email or check us on IRC, 
  347. channel #c-64. Where can you get the latest issue of Bonkers? Well, from 
  348. any of the staff, or from Bonkers web page at 
  349. http://soho.ios.com/~coolhnd/bonkers/planet.htm.
  350.  
  351. We hope you've enjoyed this issue. I know I've enjoyed writing it. Now 
  352. go code! And grab the next issue of Bonkers; it'll be here before your 
  353. socks start stinking, I promise. 
  354.  
  355.                                            Bonkers Staff
  356.