Skyhigh 09 ch12 Life, the Universe, and Everything
From C64 Diskmag Wiki
life, the universe, and everything. (-it's by the mega strange dude : RAZ) welcome to this chapter on the subject of the second most important thing in the universe. MACHINE CODING. do you feel relaxed ?. good. now lean back and enjoy the first part of the long voyage through the magic land of the mnemonics. EXACTLY WHAT IS THIS STRANGE MACHINE CODE ?. deep inside your beloved c64 the 6510 CPU chip is to be found. this chip the very heart of the computer. the CPU is responsible of executing the code written in the computer. if a command tells the computer to set the value of location $d020 to $00, the CPU examines the commend(s) and then tells other chips (the VIC II, the CIA, the RAM and the rest) what to be done. (c.p.u. = Central Processing Unit) the ONLY thing the CPU understands is numbers between 0-255 ($00-$ff in HEX). this is the true face of machine code : a number - or a serie of numbers. the numbers represent a piece of CODE and/or numbers used by codes. the diffe- rent codes are yet again represented by the so called mnemonics used in assemb- lers. the point is that the assembler trans- scripts the mnemonics into the numbers they represent when the code is ASSEMB- LED. before i go on with the description of the mnemonics i'll better give you a SMALL introduction to the HEX-decimal system of numbers: the set of numbers we normally use are based upon the number 10. that means we have got 10 different numbers (0-9). every number can be reduced to numbers multiplied by a potent of 10. for example : the number 2365 can be written :2*10^3 +3*10^2 +6*10^1 +5*10^0 (remember : 10^0 = 1). ^=arrow up. (this charset hasn't got it) the reason for telling this is very simple : the HEX-system is completely like the DECIMAL-system, but it's based upon the number 16. however, since we haven't got 16 numbers we use the numbers 0-9 & the letters a-f. that means a=10, f=15 ect. in order to know the difference be- tween hex and decimal, a dollar-sign ($) is ALWAYS put in front of a hex number. eg. $4000 , $e544 , $fce2, $ffff the same way as in the decimal system the numbers can be written as numbers multiplied by potents of the basenumber. eg. $39f1 = 3*16^3+9*16^2+15*16^1+1*16^0 = 14833 (decimal) $10 = 1*16^1+0*16^0 = 16 the computer itself understands no- thing but 0 and 1. every 'cell' in the memory can be cleared either by (0) or set (1). a cell like this is named a BIT. 8-bits combine into a BYTE. 256 (2^8) possible numbers can be held in a byte. by using the hex-system every- thing gets a lot easier.. lets have a closer look at the byte. THE BYTE. the byte is built of 8 binary numbers. it is capable of holding the numbers : %00000000 - %11111111. (the per cent-sign (%) is used to indi- cate that the number is binary) in hex that is: $00 - $ff and in decimal : 0 - 255 the idea of using the hex system is that it's based on the number 16. 16 IS 2^4. this means that each 'part' of the hexnumber $ff / $ff is represent- ^ ^ -ing 4 bits. %00001111 = $0f %11110000 = $f0 %01010101 = $55 %11110101 = $f5 %00000101 = $05 %01011111 = $5f WHY THE HECK IS THIS STUPID JERK TALKING ABOUT NUMBERS. I DEMAND SOME MC CODE NOW YOU CRY!!!! the problem is that it's important to understand the true nature of hex/binary numbers in order to program mc. but okay. i'll now give you some exam- ples of machine code. the commodore 64 CPU understands 56 COMMANDS. most of these commands can be used in several ways. THE REGISTERS. the 6510 has got 6 internal registers. PC: this is the only 16-bit register. (that means it can hold the values: $0000 - $ffff. this is also the number of memory locations in the c64. each memory location holds 8 bits = 1 byte) P.C. = Program Counter. the PC holds a pointer to the loca- tion from which the CPU is reading. e.g. pc = $45fe means that the CPU is executing the code at location $45fe. A: the ACCUMULATOR. this is a 8-bit register (#$00-#$ff). this register is the ONLY one which can be added, subtracted and rotated (read more about this later/next issue). the accumulator can be stored into the memory as well as the X and Y registers. the accumulator can also be loaded by a value in the memory. X: like the accumulator, but can't be used for adding ect. this register can be used for addressing the memory in a special way. (read about it later/next issue) Y: like the x-register. STATUS : the status-register holds six (6) FLAGS namely : n,z,c,i,d,v everytime the CPU manipulates one the the 3 registers or the memory, these flags are effected. (a closer descrip- tion follows next issue) STACK-POINTER : the CPU is capable of storing temporary data in the so called stack. the SP holds the pointer (8-bit) to the first free position in the stack. the stack is placed in the area: $0100-$0200. TWO SIMPLE MNEMONICS: lets say that you want to set the border colour to black. in basic it would look like this : poke 53280,0 (53280 = $d020 . 0 = $00) in order to do this in MC we'll need the mnemonics: LDA & STA. LDA stands for : LoaD Accumulator with memory. the LDA has got 8 different addressing- modes. the one we'll need here is the simplest: we want to set the accumulator to the value $00. the 8 modes has each got an OP-CODE. an op-code is an 8-bit number which represents an order. luckyly the ASSEMBLER (eg the famous turbo-assembler) knows the op-code, and the right one is used when the code is assembled. we use the code : LDA #$00 this code loads the accumulator with the VALUE $00. the number-sign (#) in- dicates that the number is a value and not a memory location. when the code is assembled it's transscripted into two bytes : $a9 and $00. $A9 is the OP- CODE, and it tells the CPU that the next byte is to be loaded into the accumu- lator. so far we've loaded A with the value #$00. now we want it to be stored into the memory location $d020. the STA (STore Accumulator in memory) has got 7 addressing modes. we'll use the one know as 'absolute' STA $D020 when the code is assembled this will be transscripted into : $8d ,$20 ,$d0. the first code ($8D) is the op-code for STA absolute. it tells the cpu that the value in the accumulator shall be stored in the memory location described in the next TWO bytes. PLEASE NOTICE: the pointer to $d020 is a 16-bit pointer also know as a WORD. a word is build of 2 BYTES. the LOWER byte is ALWAYS writ- ten first and the HIGH byte is second. but hey! what if we want to store the value into the address $00ff what will happen ?. STA $FF will be transscripted into : $85, $ff. this op-code ($85) tells the cpu to store the accumulator in the ZERO-page. the ZERO-page is the first 256 numbers in the memory ($0000-$00ff). the next BYTE is used as a pointer. the ZERO-page is UTTERLY IMPORTANT. some special orders do only work in this area. ENDING OUR PROGRAM. the operation system of the commodore takes care of loading, saving, writing on the screen, BASIC, scanning the key- board ect. WITHOUT THE OPERATION SYSTEM THE C64 WOULD BE 'DEAD'. the operation system is of course written in mc. it's a program. this program is stored into some special chips namely the ROM. (Read Only Memory) which means it can't be cleared. when we start our mc program the opera- tion system is stopped. when our program is finished we need to RETURN to the operation system. in the end of our program we put the order: RTS (ReTurn from Subroutine) when our program is started via a 'sys' the operation system executes it as a SUBROUTINE (like GOSUB in basic). when it meets the order RTS it continues the system-routines. (like RETURN in basic). our small program should look like this LDA #$00 STA $D020 RTS i would LOVE to write more but biz kid told me to keep this chapter small (b'coz he'll also need space for an in- terview) and it's allready taking up 55 blocks = apx. 14 000 bytes = $36b0. okay see you next time... -RAZ 18/12 1993. ps: if you want to read about ADVANCED (game)-coding read my articles in : REVEALED. next issue is to be released 10/01 94. actually there should have been an in- terview with woise of maniax.. i decided to put it in the boardnumber- chapter instead. so better load that chapter to check out the words on : woise of maniax