home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 6 / Commodore_Free_Issue_06_2007_Commodore_Computer_Club.d64 / t.Hexfiles < prev    next >
Text File  |  2023-02-26  |  6KB  |  213 lines

  1. u
  2.         THE HEX FILES - PART 1
  3.         Written by Jason Kelk
  4.  
  5.  
  6. A few people out there have expressed
  7. an interest in learning machine code.
  8. And after all, if you want to write
  9. successful games, demos or utilities
  10. it is the best language to learn,
  11. albeit a bit unfriendly. The main
  12. problem with machine code is its
  13. simplicity. There, I bet that confused
  14. you a bit eh? What I mean is that
  15. almost anything can be done in a
  16. single BASIC command can take a bit
  17. more work in machine language but at a
  18. greatly increased speed.
  19.  
  20. Right, that's the intro over with so
  21. lets start looking at some commands.
  22. The first one I've decided to explain
  23. is RTS. RTS is short for ReTurn from
  24. Subroutine and its job in life is just
  25. like the BASIC return command.
  26.  
  27. So why am I explaining it first? Well,
  28. if you just use it without subroutines
  29. it also acts like the end command and
  30. for the purposes of this course we
  31. will be using RTS to finish our code.
  32. So why does it have such a short name?
  33. That's due to the fact that all
  34. assembly language commands are only
  35. three letters long! Now, if you were
  36. to just put the command RTS into an
  37. assembler and try to run it all that
  38. would happen is the "READY." message
  39. would come up. Dull, huh?
  40.  
  41. For the next bit I need to explain a
  42. bit about the workings of the C64;
  43. imagine in your mind for a moment a
  44. long line of little boxes and that
  45. each box has a number on it from 0 to
  46. 65,535. A good example is box number
  47. 1,024 which controls the character at
  48. the top left of the screen.
  49.  
  50. If you type POKE1024,1 and press
  51. return the letter A appears at the top
  52. left of the C64's display over
  53. whatever happened to be there. All of
  54. these boxes can hold a number from 0
  55. to 255 (a byte). In the same way, box
  56. 53,280 controls the border colour of
  57. the screen so for example POKE53280,4
  58. will put colour four into the border
  59. making it go purple.
  60.  
  61. But to do this in machine code is a
  62. little more complex. First off, all
  63. numbers are stored in hex, which is
  64. base 16. We all count in base 10
  65. (mainly due to that being the number
  66. of fingers most of us have to count
  67. with) but base 16 is a little more
  68. tricky. You count to 9 as normal, but
  69. instead of saying 10 you use the
  70. letter A.
  71.  
  72. Similarly you would use B to represent
  73. 11, C for 12 and so on until F (which
  74. is 15) when you would finally use 10
  75. (pronounced "one zero" or "one oh").
  76. But in hex 10 is 16 which would be
  77. incredibly confusing so from here on
  78. any hex number will have a dollar sign
  79. in front of it to say which base its
  80. in, for example $64.
  81.  
  82. So why do we have to use hex? The
  83. benefits will become apparent later on
  84. but as it's a good habit to think in
  85. hex we will start now to get everybody
  86. used to the idea and move on to our
  87. next two commands, which are Load
  88. Decimal Accumulator, or LDA for short,
  89. and STore Accumulator, STA to its
  90. friends. LDA is machine code's
  91. equivalent of a cross between the LET
  92. and PEEK commands, so LDA #$04 is the
  93. same as LET A=4. But LDA can also be
  94. used for reading the contents of those
  95. little memory boxes we discussed
  96. earlier, so if we were to use LDA
  97. $C000
  98.  
  99. we would be putting whatever was in
  100. location $C000 (box 49,152) into A.
  101. The use of the # tells our C64 that we
  102. are giving it a direct number to put
  103. into A and without the # the C64 will
  104. read what is in box 4 in the memory
  105. instead.STA is the reverse, it can put
  106. whatever is in A back into a memory
  107. box. So STA will put whatever A
  108. contains into box $0400 (or 1,024,
  109. which is the top left of the screen
  110. remember?). A good example would be:ii
  111.  
  112. lda #$04
  113. sta $d020
  114. rts
  115.  
  116. Basically this is the same as the
  117. "POKE53280,4" command we saw earlier
  118. and shows you what I meant by the
  119. simplicity. One simple BASIC command
  120. takes two in machine code for this
  121. particular job and each step has to be
  122. followed. Now an example of the second
  123. version of LDA:
  124.  
  125. lda $d021
  126. sta $d020
  127. rts
  128.  
  129. Now this is slightly different. The
  130. first command reads from 53,281 (the
  131. screen colour, which is normally dark
  132. blue) and then the second puts that
  133. colour into 53,280 (the border colour
  134. again) so basically this will turn the
  135. border the same colour as the screen!
  136. So this is the same as
  137. POKE53280,PEEK(53281).
  138.  
  139. BASIC programmers will be wondering
  140. why we are always using A and not
  141. another letter for the variable. The
  142. reason is that A is not just a
  143. variable in this context, its the
  144. Accumulator. But we do have two other
  145. letters available and they are X and
  146. Y, known technically as the X and Y
  147. registers. Both can be used in a
  148. similar way to A in that:
  149.  
  150. ldx #$04
  151. stx $d020
  152. rts
  153.  
  154. Will do the same thing as the first
  155. example and replacing LDX with LDY and
  156. STX with STY will also work. The X and
  157. Y do have a couple of different
  158. features which will be covered in
  159. detail later, but one incredibly
  160. useful thing they can do is add or
  161. subtract 1 from their contents in a
  162. flash! This trick is done with the INX
  163. and DEX commands for the X register
  164. and INY and DEY for the Y. So how do
  165. we use them? Time for another example
  166. methinks:
  167.  
  168. ldx #$04
  169. dex
  170. stx $d020
  171. rts
  172.  
  173. This looks similar to the previous
  174. example, but the result of running it
  175. would be to make the border turn cyan!
  176. What actually happens is that first X
  177. is given the number 4 to look after.
  178. Then we tell it to go down by 1 with
  179. the DEX command leaving it with 3.
  180. Finally X is told to put it's number
  181. into the border colour but because it
  182. only holds a 3 now the colour is
  183. different. I bet you can't guess which
  184. colour 3 makes!
  185.  
  186. INX will have the reverse effect to
  187. DEX so replacing one with the other in
  188. the above example will cause X to end
  189. up holding a 5 so this time the border
  190. would be dark green. The Y register is
  191. exactly the same, so replacing all of
  192. the references to X with Y in the
  193. example will still work and produce
  194. the exact same result.That's all for
  195. this first instalment, but if you want
  196. to head on to the next part I'll show
  197. you what to do with an accumulator,
  198. two registers and an old washing up
  199. liquid bottle. If you have any
  200. questions about this article or
  201. machine code, email me and I'll do the
  202. business with the shooters. Erm, try
  203. to help
  204.  
  205. Continues Next Issue
  206.  
  207. Written by Jason Kelk
  208. (Published with Permission)
  209. http://www.oldschool-gaming.com/
  210.  
  211.  
  212. ...end...
  213.  
  214.