# L B9A (/7&%.$/36!-!#454/2 "-!#454/2 #-!#454/2 $-!#454/2 %&'()*+,-./01234 ǝjQ?:*8=9 %# /(>- @ Y0%Χ Θii  ` 01/  Ԏ ) ۈR 0` * > / i / 2 < 0 3 = f '  m  Ji i  i i  m  w 01ʍ/LL   l ll <L`L5 L5 x"˭#̭0έ1|LV 01./@ԩˍ" #͍ԩZ:ԩ XS HHlTppppFppppppppppppppWA4(%#/-0,%4%-!#().%#/$%454/2"90!5,"5..# .%7'%.%2!4)/.3/&47!2%-5,4) "//46%23)/."94()302/'2!-4//+-!.97%%+34/#2!#+4/02//6%7%!2%./4!,,,!:94()302/$5#47!3"2/5'(44/9/57)4(/544(%53%/&!./-.)-/.# -9#/.'2!45,!4)/.34/0!5,/.02/$5#).'!.%8#%,,%.4454/2/.$)&&%2%.44/0)#3/&-!#().%#/$%4(%2%!2%%8!-0,%02/'2!-3')6%.4/$%-/.342!4%7(!4(!3"%%.4!5'(4).!,%33/.4(%3%02/'2!-3-!9"%25.7)4(4(%3)-5,!4/23(/7).'%8!#4,97(!4(!00%.3 (/7)4!&&%#434(%&,!'3!.$4(%2%')34%23%4#0,%!3%02%33#42, !4/#/.4).5%C:HHͥL ͍iiɡ  Щ hhl BwD E;HI VLS BD E,HI VLt PLEASE REMOVE ALL CARTRIDGES ANDREPEAT LOADING PROCEEDURE.THIS PROGRAM REQUIRES 32K OF MEMORYMINIMUM. L L ,/!$).'%22/2L" ̈ɿL0 L" EJK VBD EHIꠈ B VD   ΍0ύ1 xˍ"̍#XL X)Ң L( HvH`0 1ǩ|ȩD RЍЩ./oԩԩ$6%@Ԣ؝D EJKB V{{ 4  ILppB|A K:{{@ {7 L {{{` 4 4 4L$ `é Ġ 4{   4{ 4{ 4 6{{L{${ ' 4 4{ 4{{LF { L {{{AL [L L {' { 5{LF {0L :L {~{ { 4LF { { {{{{L#L {+{LF {LF *{'LF {{LF -{LF {LF ={LF {LF {  5{{L {LLF { {{{{{{  {7L{ 4{ ]L `  % 6{@L {){{ b& ]{{L7 n ]L{{   L 4 4 4 4 5p"q 4 4 4p 4q{ ]  L{{ L {0 G :A`{ș{0{`:{{80m{{L0{87m{{:{{80 m{{L\{87 m{{:{{80m{{L{87m{{:{{80 m{{L{87 m{{`)IL\{ ũ>Ʃ ō{ȱiȱi {L{ 4{` THE COMPLETE MACHINE CODE TUTOR *******************************  BY PAUL BUNN (C) 1984 NEW GENERATION SOFTWAREChoose selection by pressing RETURN. When choice is made press the SPACE BAR. ō{{ 4 4{ 4 4{{ 4 4{ 4 4 4 4 6 {{i{Ш{L LH{8 iŅȱŅĠÍ{ȱÅȱÅ 4{>7 {{ { 4{ L{{i{{LL~{ 4 4{ 4  4{LLåĩ<  6 Åą`PRESS SPACE BAR TO CONTINUE H'H{{ {0m{{{i{{Lm{0 d {L{0 {L{0  {L`m{{{i{`v{wxx ȹ` 4 4 4w q{ 4{ 4v q{ 4{ 4 4 4yxL9 q{ 4{ 4xyL,ȹxL9ȹxL9veyvxxL   6YL R  ҩD BDEJKlxBD>EHI V J L JL C:  Are you sure you want to load a stage of lessons ? (Y/N) BLV  Please position correct cassette (side two, three or four) and then press PLAY on cassette recorder followed by RETURN on keyboard to start. Do NOT press system reset whilst trying to load. LOADING ERROR. Please rewind cassette and then press PLAY followed by RETURN on keyboard to reload.{{{{{{ o7Gv#w { 4{i 4{ 4 6{{{ٽ{  {{L ;L <{ L v<{#v{{{{L1 @L vivwiw` 4 4 4" 5 { 4 6L v#w{AL{ v{#v{{{{L1 @Lv#w{#LsLNv{?v{{{{{$${{ b{LMLY @L{{ {LM{{{{L1 {{,G ( L]Lȹ{XPYLN{$,ȹ{{ {L7ͅv$w{ L.{{ bL{$)ȹ{{ {:v$w{ L.{{ bL 3v%w{ Lv$w{ L{$'{{ b{*;v$w{ L.ȹ{{ {L|v#w{ L|vP{Ev{{{{2{{{{{{{{hhL1 @L`{{{{hhL1L3{m{{{{m{i{L 4 4 4" 5 Ll{{`{` 4` 4 4 4{ { ~ 4 4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{ 4{ 4`RESULTING ADDRESS $z&88+&18 89 "(88+"(88+"(88+&18 89'_88+"(88+'v8|88&18 8=8'_8 8+"(88+"(88+&18 89'_88+"(88+*;;+ &18 89"(88+"(88+"(88+&18 89 '_88+ "(88+p(999&18 89"(88+"(88+"(88+&18 89'_88+"(88+&):++ `(799+ "(88+"(88+(P98+`(799+(/9)98"(88+'}9|8r9`(79=8+(/9)9 8"(88+(P98+`(799+(/9)98"(88+*;;+ `(799+"(88+"(88+"(88+`(799+ (/9)98 "(88+y(999`(799+"(88+"(88+"(88+`(799+(/9)98"(88+"(88+(9 89 "(88+"(88+"(88+(9 89(9"98"(88+(v8 88(9 8=8(9"9 8"(88+(.:++ (9 89(9"98"(88+*;f;+ (9 89"(88+"(88+"(88+(9 89 (9"98 "(88+C)999(9 89"(88+"(88+"(88+(9 89(9"98"(88+(+;++L)998 "(88+"(88+"(88+L)998p)/9"98"(88+(}9 8r9L)99:p)/9"9 8"(88+"(88+L)998p)/9"98"(88++;;+ L)998"(88+"(88+"(88+L)998 p)/9"98 "(88+)999L)998"(88+"(88+"(88+L)998p)/9"98"(88+"(88+4(9 89 "(88+"(88+X(9%894(9 89F(989"(88+)[:%8+"(88+)98 :"(88+X(9%894(9 89F(989"(88+*;9Z; 4(9 89"(88+"(88+X(9%89 4(9 89 F(989 "(88+)9%8 :4(9 89)989"(88+"(88+4(9 89"(88+"(88+N(f:%8=8*(f: 8J8 <(f:8=8"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+~*9 8B;*(f: 8=8*9 8{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+*;9P; *(f: 8J8"(88+"(88+N(f:%8J8 *(f: 8J8 <(f:8J8 "(88+*9:9*(f: 8J8*9l:{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+b*:%8=8p*: 8J8 "(88+"(88+b*:%8J8p*: 8J8 *[:8+"(88+):%8+p*: 8=8*[:8+"(88+b*:%8J8p*: 8J8 *[:8+"(88+*;:+ p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+ "(88+*9:9p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+"(88+G*:8=8#*:98 "(88+"(88+G*:8J8#*:98*:8+"(88+*:8+#*:9:):++"(88+G*:8J8#*:98*:8+"(88+*;:+ #*:98"(88+"(88+"(88+#*:98 *:8+ "(88+)9:9#*:98"(88+"(88+"(88+#*:98*:8+"(88+{HHH{Lk5{ɜ{L6ɝ{L6 LC5{L=5 {{L=5 L5γ{{ 5L=5{L5a8 {) { 5L+5i(ǥiȩ{L=5{{({L5hhh`ǩ|ȩȥ{{|L=5 {{α{L=5ǩ|ȭ{δ{i(ǥiL5α{{ 5L=5{HHH{ L4{L5` { i0ХǍ{ȍ{{{|8{ {{{L5 `HH u6{ { {{L@6ILH6hh{`BD{EHI V䭴{ɛ {L@6{{{ `{`{`{`{) {(L5ɇ`H{ai {h{`HH=7ЍЩ @h  hhLbL47HU7T ԍЍh@H{ ԍЍh@L_7{{{` 4 4 4( 4{ 4 4{ 4 4{{ܩ 4 4 4ȩ 4 `셽7 %  6L END OF PROGRAM - PRESS A KEYaccumulator X register Y register Logical OR with number with memory locationArithmetic shift left Push processor status register onto stack memory location Break - will stop executionNot a valid instruction op-code PRESS RETURN KEY TO CONTINUEclear carry flag right left Rotate Logical AND accumulator Test BITs of accumulator against from stackPull Logical shift Exclusive OR set with memory locationAdd with interrupt disable Transfer Store at memory locationto stack pointerto accumulatorJump to subroutineJump - Load program counter with new addressDecrement Load stack pointer to X registeroverflow No operationIncrement decimal mode number Subtract with Compare not equal to zero (Z=0)equal to zero (Z=1)Branch if positive (N=0)minus (N=1)Return from subroutineto Y registerset (C=1)clear (C=0)overflow flag clear (V=0)overflow flag set (V=1){{{L; 4{ 4{ 4 6` ;{{{{{ꭀ{ L<{{{{{ u<{݀{{{{L-<{{{{L;`R< { 4" 4 6hhL ERROR - DUPLICATE LABEL FOUND " 4{ 4{ 4 6` 4 4{ 4 6 LA>{ L={BJL={ N{ BA$(A#0L=L=: { ,0L=:L<` { L={) ( L={)( {${), 0 :L=L={),0GA:Ls=hhLN{ ,0:AGL=L=L=ʅ= LL>THERE SHOULD BE A SPACE BETWEEN THE MNEMONIC AND OPERAND. THE HIGHLIGHTED LINE INDICATES WHICH LINE HAS AN ERROR.x>  4 4{ 4 4 4 4 4 6hhL A LABEL SHOULD ONLY BE UPTO SIX LETTERS LONG. THE HIGHLIGHTED LINE INDICATES THELINE WHICH HAS A LABEL TOO LONG.?G?h?????@1@S@r@@@@A Instructions on using this program (A Lesson 1 - The registers G Lesson 2 - Memory locations H Lesson 3 - Load and store >M Exercise 1 for lesson 3 R Lesson 4 - What's a flag ? X Lesson 5 - Addition S Exercise 1 for lesson 5 W Exercise 2 for lesson 5 RX Lesson 6 - Subtraction Z Exercise 1 for lesson 6 ] Exercise 2 for lesson 6 ] Lesson 7 - Increments and decrements 4^ Exercise 1 for Lesson 7 a LOAD NEXT STAGE OF LESSONS ~INSTRUCTIONS When in edit mode, press STARTto assemble program OPTIONto erase program andSELECTto return to the main menu. When running the program, press SELECTto switch between bottom and side display OPTIONto switch between HEX and DEC modes escapeto return to edit mode and theRETURN KEYto run each lineINSTRUCTIONS The demonstration programs will run automatically. If you like you can edit the program by pressing the 'escape' key. To run the program just press the key marked START. When the program has finished press the SELECT key to return to the main menu. If you wish you can press the OPTION key to erase the program, so that you can type in your own program. When the program is running, the line highlighted is the line about to be run, with a descriptionof that line displayed near the bottom of the screen. By pressing the RETURN KEY you can make the computer run that line, and the information on the screen will be updated.INSTRUCTIONS When you are typing in a program you can use the cursor keys (without having to press CONTROL) to edit the program. If the program line does not have a label, first press the SPACE BAR. Once you have typed in the program, do not forget to press the key marked START to assemble the program. If the computer reports an error, then press any key, and you will be returned to edit mode. Then correct your error, and press START to assemble the program again. THE REGISTERS The registers are like little pigeon holes that can hold a number between 0 and 255. On the 6502 processor (that's the chip inside every ATARI computer) there are 3 registers called the accumulator, the X register and the Y register. These are usually referred to as the A,X and Y registers. The accumulator is the most commonly used register and the other two registers are used for special purposes.  MEMORY LOCATIONS Programs and data are stored inside the computer in MEMORY. A memory location is like a register in the fact that each memory location can hold a number between 0 and 255. There are 65536 (64K) memory locations on the ATARI computer of which can be RAM or ROM. RAM stands for Random Access Memory, and means that you can change the value of the memory location to what you want. ROM stands for Read Only Memory and you can only read its contents and writing to it has no effect. ROM is set up at the factory andis the same each time you turn the computer on. Unlike ROM, RAM will lose its contents if you turn the computer off, and then back on. MEMORY LOCATIONS A machine code program just affects memory locations. Even sound effects and high resolution graphics are controlled by changing the contents of memory locations. With machine code this can be done VERY quickly and the result of this is that arcade games and fast moving graphics are possible. A machine code program itself is stored in memory .A group of one to three memory locations make up an instruction which is executed by the computer. LOAD AND STORE These are the simplest of all machine code instructions and they just affect registers and memory locations. A load is where the contents of a register is changed to a new value. The instructions LDX, LDY and LDA are examples of load instructions. These will load the X, Y and A registers with a new value. The instructions STX, STY and STA are all examples of store instructions. A store instruction is where the value of a register is stored at a memory location, usually for later retrieval. In the example program note that there is a BRK instruction. This instruction is at the end of the program, and is used to stop execution. LOAD AND STORE When running your own programs you may not store or load from a memory location whose address is greater than 1023 decimal. All these addresses are RAM, and may have their contents changed by a program. You are limited to this range of memorylocations by this program, not by the computer. This is so that your program will NEVER crash. When writing your own programs without this program, then there IS a chance that the computer willcrash. It is up to you then to make surethat this does not happen. MEMORY LOCATIONS Note that in the program the hash character (#) is used for a special purpose. On the 6502, usually this character is used to represent an actualnumber. So LDA #23 will load the accumulator with 23, not the contents of memory location 23. LDA #40 STA 1 STA 1000 LDX #5 LDY #9 STX 500 STY 600 LDA 500 LDA 1000 LDA 600 LDA 1 BRKADDITION This is simply achieved in machine code with the instruction ADC, this is a mnemonic meaning ADd with Carry. The carry flag is a flag which is altered depending on the result of the addition. As you know, a memory locationand a register can only hold a number between 0 and 255, the carry flag is used to indicate too large a result. For example, 240+240=480 right? Wrong. If you add 240 to 240, the result would be 224, and the carry flag will be set. The carry flag indicates that you shouldadd 256 to the result. Thus, 256+224 gives 480, the correct result.ADDITION The instruction ADC also adds the valueof the carry flag to the result, so thatif the carry is set and 0 is added to 0 then the result would be one. Therefore for true results, it is necessary to CLear the Carry flag, by the instructionCLC. There are two demonstration programs to show ADC in use. There is also an instruction to SEt theCarry flag, SEC. This is used in the second demonstration program to show theeffect of ADC when the carry is set. CLC LDA #240 ADC #240 STA 200 CLC LDA #0 ADC 200 CLC ADC #1 BRK SEC LDA #0 ADC #0 BRKWHAT'S A FLAG ? A flag is like a little indicator, like a warning light to show low fuel on a car for example. With a flag however, the status of the flag is shown by a one or a zero. A one means that the flag is 'set' and a zero means that the flag is 'clear'. One flag is called the 'Zero' flag. After an instruction, this flag could be affected depending on the instruction. If the result produced is zero, then this flag will be set. There are other flags which will be discussed later. When the program is running all the flags are displayed either on the left side or the bottom of the screen.SUBTRACTION This is achieved on the 6502 in a similar way to addition, except that theprecautionary practice is to set the carry flag instead of clearing it beforea subtraction. The instruction is calledSBC, which stands for SuBtract with Carry. The carry is used to indicate a 'borrow'. If you subtract a number from a smaller number then the carry flag will be cleared. If the carry is clear when you perform the SBC then the result will be one less than it should be. Two example programs are given to demonstrate SBC in use. SEC LDA #120 SBC #50 STA 33 SEC LDA #80 SBC 33 SEC LDA #20 SBC #30 CLC LDA #3 SBC #0 BRK SEC LDA #4 SBC #1 SBC #1 SBC #1 SBC #1 SBC #1 BRKINCREMENTS AND DECREMENTS The above words mean the same as increase and decrease. However, on the 6502, increments and decrements have a special meaning in machine code, and that is to increase or decrease by one. There are quite a few of these instructions which affect registers and memory locations. An increment will add one to the contents of a memory location and store the new result back into the memory location. The procedure is similar for registers and decrements. If you try and decrement 0 the result will be 255, and incrementing 255 will produce a result of 0.INCREMENTS AND DECREMENTS The following increments and decrementsare available to 6502 machine code: INCINCREMENT MEMORY LOCATION DECDECREMENT MEMORY LOCATION INXINCREMENT X - REGISTER DEXDECREMENT X - REGISTER INYINCREMENT Y - REGISTER DEYDECREMENT Y - REGISTER An example program is given to show increments and decrements in use. LDA #4 STA 230 LDX #212 LDY #34 INC 230 DEY INX DEX INY DEC 230 DEC 230 LDA 230 DEC 230 LDA 230 BRKɀLGŨ  GL G `Ɉ"% RNLM 0L W @` kɈ` Z 0` ZC` WΆ`}<L`L5 L5 x"˭#̭0έ1|LV 01./@ԩˍ" #͍ԩZ:ԩ XS HHlTppppFppppppppppppppWA4(%#/-0,%4%-!#().%#/$%454/2"90!5,"5..# .%7'%.%2!4)/.3/&47!2%-5,4) "//46%23)/."94()302/'2!-4//+-!.97%%+34/#2!#+4/02//6%7%!2%./4!,,,!:94()302/$5#47!3"2/5'(44/9/57)4(/544(%53%/&!./-.)-/.# -9#/.'2!45,!4)/.34/0!5,/.02/$5#).'!.%8#%,,%.4454/2/.$)&&%2%.44/0)#3/&-!#().%#/$%4(%2%!2%%8!-0,%02/'2!-3')6%.4/$%-/.342!4%7(!4(!3"%%.4!5'(4).!,%33/.4(%3%02/'2!-3-!9"%25.7)4(4(%3)-5,!4/23(/7).'%8!#4,97(!4(!00%.3 (/7)4!&&%#434(%&,!'3!.$4(%2%')34%23%4#0,%!3%02%33#42, !4/#/.4).5%C:HHͥL ͍iiɡ  Щ hhl BwD E;HI VLS BD E,HI VLt PLEASE REMOVE ALL CARTRIDGES ANDREPEAT LOADING PROCEEDURE.THIS PROGRAM REQUIRES 32K OF MEMORYMINIMUM. L L ,/!$).'%22/2L" ̈ɿL0 L" EJK VBD EHIꠈ B VD   ΍0ύ1 xˍ"̍#XL X)Ң L( HvH`0 1ǩ|ȩD RЍЩ./oԩԩ$6%@Ԣ؝D EJKB V{{ 4  ILppB|A K:{{@ {7 L {{{` 4 4 4L$ `é Ġ 4{   4{ 4{ 4 6{{L{${ ' 4 4{ 4{{LF { L {{{AL [L L {' { 5{LF {0L :L {~{ { 4LF { { {{{{L#L {+{LF {LF *{'LF {{LF -{LF {LF ={LF {LF {  5{{L {LLF { {{{{{{  {7L{ 4{ ]L `  % 6{@L {){{ b& ]{{L7 n ]L{{   L 4 4 4 4 5p"q 4 4 4p 4q{ ]  L{{ L {0 G :A`{ș{0{`:{{80m{{L0{87m{{:{{80 m{{L\{87 m{{:{{80m{{L{87m{{:{{80 m{{L{87 m{{`)IL\{ ũ>Ʃ ō{ȱiȱi {L{ 4{` THE COMPLETE MACHINE CODE TUTOR *******************************  BY PAUL BUNN (C) 1984 NEW GENERATION SOFTWAREChoose selection by pressing RETURN. When choice is made press the SPACE BAR. ō{{ 4 4{ 4 4{{ 4 4{ 4 4 4 4 6 {{i{Ш{L LH{8 iŅȱŅĠÍ{ȱÅȱÅ 4{>7 {{ { 4{ L{{i{{LL~{ 4 4{ 4  4{LLåĩ<  6 Åą`PRESS SPACE BAR TO CONTINUE H'H{{ {0m{{{i{{Lm{0 d {L{0 {L{0  {L`m{{{i{`v{wxx ȹ` 4 4 4w q{ 4{ 4v q{ 4{ 4 4 4yxL9 q{ 4{ 4xyL,ȹxL9ȹxL9veyvxxL   6YL R  ҩD BDEJKlxBD>EHI V J L JL C:  Are you sure you want to load a stage of lessons ? (Y/N) BLV  Please position correct cassette (side two, three or four) and then press PLAY on cassette recorder followed by RETURN on keyboard to start. Do NOT press system reset whilst trying to load. LOADING ERROR. Please rewind cassette and then press PLAY followed by RETURN on keyboard to reload.{{{{{{ o7Gv#w { 4{i 4{ 4 6{{{ٽ{  {{L ;L <{ L v<{#v{{{{L1 @L vivwiw` 4 4 4" 5 { 4 6L v#w{AL{ v{#v{{{{L1 @Lv#w{#LsLNv{?v{{{{{$${{ b{LMLY @L{{ {LM{{{{L1 {{,G ( L]Lȹ{XPYLN{$,ȹ{{ {L7ͅv$w{ L.{{ bL{$)ȹ{{ {:v$w{ L.{{ bL 3v%w{ Lv$w{ L{$'{{ b{*;v$w{ L.ȹ{{ {L|v#w{ L|vP{Ev{{{{2{{{{{{{{hhL1 @L`{{{{hhL1L3{m{{{{m{i{L 4 4 4" 5 Ll{{`{` 4` 4 4 4{ { ~ 4 4 4{ { ~ 4! 4 4{ { ~ 4 4 4{{*i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(88+"(88+"(88+&18 89'_88+"(88+'v8|88&18 8=8'_8 8+"(88+"(88+&18 89'_88+"(88+*;;+ &18 89"(88+"(88+"(88+&18 89 '_88+ "(88+p(999&18 89"(88+"(88+"(88+&18 89'_88+"(88+&):++ `(799+ "(88+"(88+(P98+`(799+(/9)98"(88+'}9|8r9`(79=8+(/9)9 8"(88+(P98+`(799+(/9)98"(88+*;;+ `(799+"(88+"(88+"(88+`(799+ (/9)98 "(88+y(999`(799+"(88+"(88+"(88+`(799+(/9)98"(88+"(88+(9 89 "(88+"(88+"(88+(9 89(9"98"(88+(v8 88(9 8=8(9"9 8"(88+(.:++ (9 89(9"98"(88+*;f;+ (9 89"(88+"(88+"(88+(9 89 (9"98 "(88+C)999(9 89"(88+"(88+"(88+(9 89(9"98"(88+(+;++L)998 "(88+"(88+"(88+L)998p)/9"98"(88+(}9 8r9L)99:p)/9"9 8"(88+"(88+L)998p)/9"98"(88++;;+ L)998"(88+"(88+"(88+L)998 p)/9"98 "(88+)999L)998"(88+"(88+"(88+L)998p)/9"98"(88+"(88+4(9 89 "(88+"(88+X(9%894(9 89F(989"(88+)[:%8+"(88+)98 :"(88+X(9%894(9 89F(989"(88+*;9Z; 4(9 89"(88+"(88+X(9%89 4(9 89 F(989 "(88+)9%8 :4(9 89)989"(88+"(88+4(9 89"(88+"(88+N(f:%8=8*(f: 8J8 <(f:8=8"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+~*9 8B;*(f: 8=8*9 8{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+*;9P; *(f: 8J8"(88+"(88+N(f:%8J8 *(f: 8J8 <(f:8J8 "(88+*9:9*(f: 8J8*9l:{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+b*:%8=8p*: 8J8 "(88+"(88+b*:%8J8p*: 8J8 *[:8+"(88+):%8+p*: 8=8*[:8+"(88+b*:%8J8p*: 8J8 *[:8+"(88+*;:+ p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+ "(88+*9:9p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+"(88+G*:8=8#*:98 "(88+"(88+G*:8J8#*:98*:8+"(88+*:8+#*:9:):++"(88+G*:8J8#*:98*:8+"(88+*;:+ #*:98"(88+"(88+"(88+#*:98 *:8+ "(88+)9:9#*:98"(88+"(88+"(88+#*:98*:8+"(88+{HHH{Lk5{ɜ{L6ɝ{L6 LC5{L=5 {{L=5 L5γ{{ 5L=5{L5a8 {) { 5L+5i(ǥiȩ{L=5{{({L5hhh`ǩ|ȩȥ{{|L=5 {{α{L=5ǩ|ȭ{δ{i(ǥiL5α{{ 5L=5{HHH{ L4{L5` { i0ХǍ{ȍ{{{|8{ {{{L5 `HH u6{ { {{L@6ILH6hh{`BD{EHI V䭴{ɛ {L@6{{{ `{`{`{`{) {(L5ɇ`H{ai {h{`HH=7ЍЩ @h  hhLbL47HU7T ԍЍh@H{ ԍЍh@L_7{{{` 4 4 4( 4{ 4 4{ 4 4{{ܩ 4 4 4ȩ 4 `셽7 %  6L END OF PROGRAM - PRESS A KEYaccumulator X register Y register Logical OR with number with memory locationArithmetic shift left Push processor status register onto stack memory location Break - will stop executionNot a valid instruction op-code PRESS RETURN KEY TO CONTINUEclear carry flag right left Rotate Logical AND accumulator Test BITs of accumulator against from stackPull Logical shift Exclusive OR set with memory locationAdd with interrupt disable Transfer Store at memory locationto stack pointerto accumulatorJump to subroutineJump - Load program counter with new addressDecrement Load stack pointer to X registeroverflow No operationIncrement decimal mode number Subtract with Compare not equal to zero (Z=0)equal to zero (Z=1)Branch if positive (N=0)minus (N=1)Return from subroutineto Y registerset (C=1)clear (C=0)overflow flag clear (V=0)overflow flag set (V=1){{{L; 4{ 4{ 4 6` ;{{{{{ꭀ{ L<{{{{{ u<{݀{{{{L-<{{{{L;`R< { 4" 4 6hhL ERROR - DUPLICATE LABEL FOUND " 4{ 4{ 4 6` 4 4{ 4 6 LA>{ L={BJL={ N{ BA$(A#0L=L=: { ,0L=:L<` { L={) ( L={)( {${), 0 :L=L={),0GA:Ls=hhLN{ ,0:AGL=L=L=ʅ= LL>THERE SHOULD BE A SPACE BETWEEN THE MNEMONIC AND OPERAND. THE HIGHLIGHTED LINE INDICATES WHICH LINE HAS AN ERROR.x>  4 4{ 4 4 4 4 4 6hhL A LABEL SHOULD ONLY BE UPTO SIX LETTERS LONG. THE HIGHLIGHTED LINE INDICATES THELINE WHICH HAS A LABEL TOO LONG. ?3?U?x????@1@T@{@@ Lesson 8 - Transfers @ Exercise 1 for lesson 8 uB Lesson 9 - Binary notation B Lesson 10 - Shifts and rotates \M Exercise 1 for lesson 10 DV Lesson 11 - Two more flags W Exercise 1 for lesson 11 [ Lesson 12 - Logical instructions S\ Exercise 1 for lesson 12 g Lesson 13 - Indexed addressing Oh Exercise 1 for lesson 13 o LOAD NEXT STAGE OF LESSONS\TRANSFERS There are quite a few transfer instructions on the 6502. A transfer is the copying of one value of a register into another register. The following transfers are available TAX-TRANSFER ACCUMULATOR TO X-REGISTER TXA-TRANSFER X-REGISTER TO ACCUMULATOR TAY-TRANSFER ACCUMULATOR TO Y-REGISTER TYA-TRANSFER Y-REGISTER TO ACCUMULATOR There are also two other transfer instructions that will be discussed later. LDA #104 TXA LDX #40 TXA CLC ADC #20 TAX TAY LDY #3 TYA BRK BINARY NOTATION Decimal is a system of using 10 different numbers in each number column.This system is called BASE 10. However, this is not the system used by a computer, which uses BASE 2. This systemuses only two different numbers for eachnumber column, one and zero. Because of this, the COLUMN HEADINGS will be different. The column headings for decimal are: 1000 100 10 1 BINARY NOTATION So the decimal number 2398 is actually calculated thus: 2 times 1000 = 2000 3 times 100 = 300 9 times 10 = 90 8 times 1 = + 8 ---- Giving a result of 2398 BINARY NOTATION In binary the column headings are like this: 128 64 32 16 8 4 2 1 Here is an example of converting binary to decimal: 1 128 0 +0 0 +0 So the binary number 1 +16 10010111 would be 0 +0 151 (128+16+4+2+1) 1 +4 in decimal 1 +2 1 +1 --- 151 BINARY NOTATION Each binary digit (1 or 0) is called a BIT. The previous example was an eightbit number. The bits are numbered from right to left as BIT 0 to BIT 7. All the registers and memory locations on the 6502 are eight bits in length. So the maximum value they can hold is 11111111 binary, or 255 decimal (128+64+32+16+8+4+2+1). A group of eight bits is commonly called a BYTE. In the way binary is used above a byte could only be used to represent a positive number. However, by using a technique called TWO'S COMPLEMENT negative numbers can also be stored. BINARY NOTATION When using two's complement to represent negative numbers, bit 7 (the far left most bit of a byte) represents the sign. On the 6502 a one is used for a negative number and a zero for a positive number. To change the sign of anumber is quite simple. First you have to reverse every digit so that a one becomes a zero and a zero becomes a one.Then you have to add one for the final result. BINARY NOTATION 00011010 would be: reverse 00011010 to give 11100101 and then add one +00000001 -------- 11100110 Therefore the negative equivalent of 00011010 is 11100110. BINARY NOTATION Using two's complement you can use addition to simulate subtraction. Take the example of 43-1. The negative of positive one is 11111111. And 43 in binary is 00101011. Therefore, adding them together gives : 00101011 + 11111111 -------- (1)00101010 Note that there is a carry, which must be ignored if using two's complement. Sothe result of the addition is 00101010 which is 42, the correct answer. SHIFTS AND ROTATES Shifts and rotates are instructions that directly relate to changing of binary numbers. Each instruction will now be described. Note that the instruction may be followed by a number representing a memory location, or the letter A representing Accumulator. The function of the instruction will then be carried out on the memory location or the accumulator.ROTATE RIGHT ROR *** This instruction performs what is knownas a nine bit rotation. All the bits in the binary number are rotated one place to the right. The carry holds the data contained in bit 0. The old data of the carry flag is then stored in bit 7. The following example should make things clearer: BEFORE: carry number 1 01011010 AFTER: 0 10101101ROTATE LEFT ROL *** This instruction is also known as a nine bit rotation. It is the same as theROR instruction except the bits are rotated left. The following example should make this clear: BEFORE: number carry 01111101 1 AFTER: 11111011 0 LOGICAL SHIFT RIGHT LSR *** This instruction is similar to the ROR instruction, except that it is a eight bit rotation (a shift). A zero enters the left end, bit 7, and shifts all the other numbers one place to the right. The digit that falls off the right end goes into the carry. The LSR instructionhas the effect of dividing a number by two, with the carry holding a result representing the half digit. Here is an example demonstrating LSR: BEFORE: carry number x 11100100 AFTER: 0 01110010 ARITHMETIC SHIFT LEFT ASL *** This instruction is similar to LSR except that the bits are shifted to the left. This has the effect of multiplyingthe number by two. If the carry is set after the instruction, this indicates too large a result, and that 256 should be added to the result to get the correct answer. With ASL a zero enters the far right end, bit 0, and shifts all the other bits one place to the left. What was in bit 7 is placed in the carry flag. BEFORE: carry number x 10000110 AFTER: 1 00001100 LDA #1 ROL A ROL A STA 100 ROL A SEC ASL 100 LDA 100 ROR A LDA #128 ASL A CLC LDA #2 LSR A ROR A BRK TWO MORE FLAGS THE NEGATIVE FLAG This flag always reflects the status ofbit 7 after an instruction. If bit 7 is set after an instruction then this flag will be set. If bit 7 is cleared after an instruction then this flag will be cleared. THE OVERFLOW FLAG This flag is set if the negative flag has been set or cleared by an arithmeticoperation, whose result then has the wrong sign. TWO MORE FLAGS If you add two large numbers together then there is a chance of the overflow flag being set. Take this example:- 01000011 (+67) +01000000 (+64) --------- 10000011 (-125)  TWO MORE FLAGS In the example the two positive numbers added together have produced a negative result. If this happens then the overflow flag will become set. This flag will become set if:- Adding large positive numbers Adding large negative numbers Subtracting a large positive number froma large negative number Subtracting a large negative number froma large positive number. Note that the instruction CLV stands for CLear oVerflow flag. There is a demonstration program showing when the overflow and negative flags are set. SEC LDA #130 SBC #20 CLC LDA #65 CLV ADC #67 SEC CLV SBC #120 CLV CLC ADC #1 BRK LOGICAL INSTRUCTIONS Logical instructions are based on the mathematical work of George Boole. Each logic instruction relies on what is known as a 'truth table'. The two inputsare shown at the top, with the relating result at the bottom. All logical instructions are performed between the accumulator and a memory location or an immediate number. Each pair of bits are done separately by the 6502. Each logical instruction available to the 6502 will now be described. LOGICAL INSTRUCTIONS AND *** With the AND instruction, each pair of bits of the accumulator and memory location or number are compared. If theyare BOTH ones, then the output will be one, otherwise the output will be zero. Here is the truth table for AND:- Input 1: 0 0 1 1 Input 2: 0 1 0 1 ------------- Results: 0 0 0 1 An example: 10101101 ANDed with 11110001 gives: 10100001 LOGICAL INSTRUCTIONS The AND instruction is useful when you want to 'mask off' certain bits of a binary number. For example, say bits 7 to 4 are unimportant, then you could usethe AND instruction to erase the bits that are not wanted. The following example shows this: 11011010 - Original number 00001111 - The 'mask' byte 00001010 - The result The result now has the upper four bits missing. Note that in the example programs you will have to convert from decimal to binary for the results to make sense. LOGICAL INSTRUCTIONS ORA *** With the instruction ORA, which stands for OR Accumulator, each pair of bits are compared, if ONE or BOTH the bits are set then the result will be a one. Here is the truth table for ORA: Input 1: 0 0 1 1 Input 2: 0 1 0 1 ------------- Results: 0 1 1 1 LOGICAL INSTRUCTIONS Here is an example using ORA: 10101011 - Number to be ORed with: 00001111 - this number gives 10101111 - this result. The ORA instruction is useful if you want to make sure certain bits are a oneas in the previous example. LOGICAL INSTRUCTIONS EOR *** This instruction is similar to the ORA instruction, except that if both inputs are ones, then the result will be zero. The truth table for EOR is: Input 1: 0 0 1 1 Input 2: 0 1 0 1 ------------- Results: 0 1 1 0 EOR actually stands for Exclusive OR. One of its uses is for reversing selected bits. Each bit that you want reversed has to be in the number or the memory location. LOGICAL INSTRUCTIONS Here is an example of using the EOR instruction to reverse every bit. If youwere to add one to the result you would have the opposite sign of the number. Ifit had been positive it would now be negative and vice versa. 01101110 - number to be Exclusive ORed 11111111 - with this number to give: 10010001 - this number. LDA #1 ORA #2 ORA #4 ORA #4 AND #3 AND #2 AND #0 LDA #255 STA 1000 LDA #190 EOR 1000 EOR #255 BRK INDEXED ADDRESSING So far only three addressing modes havebeen used, IMMEDIATE, ABSOLUTE and IMPLIED. The implied form of addressing is the easiest to understand, it is justa three letter mnemonic, like TXA. The addressing mode determines where the 6502 will fetch its data from, for the instruction about to be executed. With the immediate form of addressing, the 6502 uses the actual number specified. So if the instruction was LDA #20, the 6502 would load the accumulator with thenumber 20, not the memory location. INDEXED ADDRESSING With absolute addressing the number given after the instruction is a memory location. With the instruction LDA 20 the 6502 will load the accumulator with the number contained in the memory location number 20, which could be any number between 0 and 255, which need notbe the actual number 20. INDEXED ADDRESSING Indexed addressing is similar to absolute addressing in the fact that the6502 fetches the data from a memory location. The X and Y registers are sometimes called index registers, because of their use with this form of addressing. Two examples of using indexed addressing are about to be given, with an explanation showing how they are used, and how they work. INDEXED ADDRESSING One example of using indexed addressingis LDA 710,X and another example is ORA 654,Y. In the first example the contents of the X register are added to 710 to give a memory location address. So, if the X register holds 25 when the program is running then the 6502 will load the accumulator with the contents of memory location 735 (710+25). Similarly, with the second example the Y register is added to 654 to give a memory location address. If the Y register contains 216 when the program is running then the accumulator will be ORed against memory location 870 (654+216). An example program shows indexed addressing in use. LDA #6 STA 605 CLC ADC #2 STA 606 ADC #2 STA 607 LDX #0 TXA LDA 605,X INX ADC 605,X INX ADC 605,X BRK) `h Ն Ť ɡ}àL3 6 6 6 RLȩ נ נ נ<L`L5 L5 x"˭#̭0έ1|LV 01./@ԩˍ" #͍ԩZ:ԩ XS HHlTppppFppppppppppppppWA4(%#/-0,%4%-!#().%#/$%454/2"90!5,"5..# .%7'%.%2!4)/.3/&47!2%-5,4) "//46%23)/."94()302/'2!-4//+-!.97%%+34/#2!#+4/02//6%7%!2%./4!,,,!:94()302/$5#47!3"2/5'(44/9/57)4(/544(%53%/&!./-.)-/.# -9#/.'2!45,!4)/.34/0!5,/.02/$5#).'!.%8#%,,%.4454/2/.$)&&%2%.44/0)#3/&-!#().%#/$%4(%2%!2%%8!-0,%02/'2!-3')6%.4/$%-/.342!4%7(!4(!3"%%.4!5'(4).!,%33/.4(%3%02/'2!-3-!9"%25.7)4(4(%3)-5,!4/23(/7).'%8!#4,97(!4(!00%.3 (/7)4!&&%#434(%&,!'3!.$4(%2%')34%23%4#0,%!3%02%33#42, !4/#/.4).5%C:HHͥL ͍iiɡ  Щ hhl BwD E;HI VLS BD E,HI VLt PLEASE REMOVE ALL CARTRIDGES ANDREPEAT LOADING PROCEEDURE.THIS PROGRAM REQUIRES 32K OF MEMORYMINIMUM. L L ,/!$).'%22/2L" ̈ɿL0 L" EJK VBD EHIꠈ B VD   ΍0ύ1 xˍ"̍#XL X)Ң L( HvH`0 1ǩ|ȩD RЍЩ./oԩԩ$6%@Ԣ؝D EJKB V{{ 4  ILppB|A K:{{@ {7 L {{{` 4 4 4L$ `é Ġ 4{   4{ 4{ 4 6{{L{${ ' 4 4{ 4{{LF { L {{{AL [L L {' { 5{LF {0L :L {~{ { 4LF { { {{{{L#L {+{LF {LF *{'LF {{LF -{LF {LF ={LF {LF {  5{{L {LLF { {{{{{{  {7L{ 4{ ]L `  % 6{@L {){{ b& ]{{L7 n ]L{{   L 4 4 4 4 5p"q 4 4 4p 4q{ ]  L{{ L {0 G :A`{ș{0{`:{{80m{{L0{87m{{:{{80 m{{L\{87 m{{:{{80m{{L{87m{{:{{80 m{{L{87 m{{`)IL\{ ũ>Ʃ ō{ȱiȱi {L{ 4{` THE COMPLETE MACHINE CODE TUTOR *******************************  BY PAUL BUNN (C) 1984 NEW GENERATION SOFTWAREChoose selection by pressing RETURN. When choice is made press the SPACE BAR. ō{{ 4 4{ 4 4{{ 4 4{ 4 4 4 4 6 {{i{Ш{L LH{8 iŅȱŅĠÍ{ȱÅȱÅ 4{>7 {{ { 4{ L{{i{{LL~{ 4 4{ 4  4{LLåĩ<  6 Åą`PRESS SPACE BAR TO CONTINUE H'H{{ {0m{{{i{{Lm{0 d {L{0 {L{0  {L`m{{{i{`v{wxx ȹ` 4 4 4w q{ 4{ 4v q{ 4{ 4 4 4yxL9 q{ 4{ 4xyL,ȹxL9ȹxL9veyvxxL   6YL R  ҩD BDEJKlxBD>EHI V J L JL C:  Are you sure you want to load a stage of lessons ? (Y/N) BLV  Please position correct cassette (side two, three or four) and then press PLAY on cassette recorder followed by RETURN on keyboard to start. Do NOT press system reset whilst trying to load. LOADING ERROR. Please rewind cassette and then press PLAY followed by RETURN on keyboard to reload.{{{{{{ o7Gv#w { 4{i 4{ 4 6{{{ٽ{  {{L ;L <{ L v<{#v{{{{L1 @L vivwiw` 4 4 4" 5 { 4 6L v#w{AL{ v{#v{{{{L1 @Lv#w{#LsLNv{?v{{{{{$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{ {L+{L+L({{ {{i{ {L({){`{) H({) {{q{h) {{`{)H({) {jh) {{`{L){{{)M{h) {{`{{L){{`{ {` 4 4 4# 4 6hhL  `{ {`{L){L){{{L){L){) H({) {{{h) {{`{) {{8h) {{`{) {{LR*{) {{LR*{{L){{L){{L){){`{{{L){){`{)L*`{){{`{)@`{)`{)`{)`{)`{)@` + 4L + 6hhL ERROR - ADDRESS NOT FOUND {`v+ 48 q{ 4 q{ 4{ 4`RESULTING ADDRESS $z&88+&18 89 "(88+"(88+"(88+&18 89'_88+"(88+'v8|88&18 8=8'_8 8+"(88+"(88+&18 89'_88+"(88+*;;+ &18 89"(88+"(88+"(88+&18 89 '_88+ "(88+p(999&18 89"(88+"(88+"(88+&18 89'_88+"(88+&):++ `(799+ "(88+"(88+(P98+`(799+(/9)98"(88+'}9|8r9`(79=8+(/9)9 8"(88+(P98+`(799+(/9)98"(88+*;;+ `(799+"(88+"(88+"(88+`(799+ (/9)98 "(88+y(999`(799+"(88+"(88+"(88+`(799+(/9)98"(88+"(88+(9 89 "(88+"(88+"(88+(9 89(9"98"(88+(v8 88(9 8=8(9"9 8"(88+(.:++ (9 89(9"98"(88+*;f;+ (9 89"(88+"(88+"(88+(9 89 (9"98 "(88+C)999(9 89"(88+"(88+"(88+(9 89(9"98"(88+(+;++L)998 "(88+"(88+"(88+L)998p)/9"98"(88+(}9 8r9L)99:p)/9"9 8"(88+"(88+L)998p)/9"98"(88++;;+ L)998"(88+"(88+"(88+L)998 p)/9"98 "(88+)999L)998"(88+"(88+"(88+L)998p)/9"98"(88+"(88+4(9 89 "(88+"(88+X(9%894(9 89F(989"(88+)[:%8+"(88+)98 :"(88+X(9%894(9 89F(989"(88+*;9Z; 4(9 89"(88+"(88+X(9%89 4(9 89 F(989 "(88+)9%8 :4(9 89)989"(88+"(88+4(9 89"(88+"(88+N(f:%8=8*(f: 8J8 <(f:8=8"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+~*9 8B;*(f: 8=8*9 8{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+*;9P; *(f: 8J8"(88+"(88+N(f:%8J8 *(f: 8J8 <(f:8J8 "(88+*9:9*(f: 8J8*9l:{:"(88+N(f:%8J8*(f: 8J8<(f:8J8"(88+b*:%8=8p*: 8J8 "(88+"(88+b*:%8J8p*: 8J8 *[:8+"(88+):%8+p*: 8=8*[:8+"(88+b*:%8J8p*: 8J8 *[:8+"(88+*;:+ p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+ "(88+*9:9p*: 8J8"(88+"(88+"(88+p*: 8J8 *[:8+"(88+G*:8=8#*:98 "(88+"(88+G*:8J8#*:98*:8+"(88+*:8+#*:9:):++"(88+G*:8J8#*:98*:8+"(88+*;:+ #*:98"(88+"(88+"(88+#*:98 *:8+ "(88+)9:9#*:98"(88+"(88+"(88+#*:98*:8+"(88+{HHH{Lk5{ɜ{L6ɝ{L6 LC5{L=5 {{L=5 L5γ{{ 5L=5{L5a8 {) { 5L+5i(ǥiȩ{L=5{{({L5hhh`ǩ|ȩȥ{{|L=5 {{α{L=5ǩ|ȭ{δ{i(ǥiL5α{{ 5L=5{HHH{ L4{L5` { i0ХǍ{ȍ{{{|8{ {{{L5 `HH u6{ { {{L@6ILH6hh{`BD{EHI V䭴{ɛ {L@6{{{ `{`{`{`{) {(L5ɇ`H{ai {h{`HH=7ЍЩ @h  hhLbL47HU7T ԍЍh@H{ ԍЍh@L_7{{{` 4 4 4( 4{ 4 4{ 4 4{{ܩ 4 4 4ȩ 4 `셽7 %  6L END OF PROGRAM - PRESS A KEYaccumulator X register Y register Logical OR with number with memory locationArithmetic shift left Push processor status register onto stack memory location Break - will stop executionNot a valid instruction op-code PRESS RETURN KEY TO CONTINUEclear carry flag right left Rotate Logical AND accumulator Test BITs of accumulator against from stackPull Logical shift Exclusive OR set with memory locationAdd with interrupt disable Transfer Store at memory locationto stack pointerto accumulatorJump to subroutineJump - Load program counter with new addressDecrement Load stack pointer to X registeroverflow No operationIncrement decimal mode number Subtract with Compare not equal to zero (Z=0)equal to zero (Z=1)Branch if positive (N=0)minus (N=1)Return from subroutineto Y registerset (C=1)clear (C=0)overflow flag clear (V=0)overflow flag set (V=1){{{L; 4{ 4{ 4 6` ;{{{{{ꭀ{ L<{{{{{ u<{݀{{{{L-<{{{{L;`R< { 4" 4 6hhL ERROR - DUPLICATE LABEL FOUND " 4{ 4{ 4 6` 4 4{ 4 6 LA>{ L={BJL={ N{ BA$(A#0L=L=: { ,0L=:L<` { L={) ( L={)( {${), 0 :L=L={),0GA:Ls=hhLN{ ,0:AGL=L=L=ʅ= LL>THERE SHOULD BE A SPACE BETWEEN THE MNEMONIC AND OPERAND. THE HIGHLIGHTED LINE INDICATES WHICH LINE HAS AN ERROR.x>  4 4{ 4 4 4 4 4 6hhL A LABEL SHOULD ONLY BE UPTO SIX LETTERS LONG. THE HIGHLIGHTED LINE INDICATES THELINE WHICH HAS A LABEL TOO LONG.?C?f?????@/@R@{@@@@ A Lesson 14 - Branches and jumps .A Exercise 1 for lesson 14 RG Exercise 2 for lesson 14 G Exercise 3 for lesson 14 H Lesson 15 - Compares H Exercise 1 for lesson 15 P Exercise 2 for lesson 15 Q Lesson 16 - Subroutines WR Exercise 1 for lesson 16 %T Lesson 17 - Hexadecimal notation T Exercise 1 for lesson 17 e Exercise 2 for lesson 17 f Lesson 18 - Binary Coded Decimal eg Exercise 1 for lesson 18 k LOAD NEXT STAGE OF LESSONS\ BRANCHES AND JUMPS Jump instructions are like the GOTO statement in BASIC, and the branch instructions are like IF..THEN..GOTO statements in BASIC. Previously you have been pressing the space bar before typing in a program line. You have been skipping the LABEL FIELD each time you have done so. If youwant to use a jump or a branch to a linedo NOT press the space bar at the start of the line, but type in a label of up to 6 characters long and then press the space bar. A label can then be used by a program for jumps and branches. Each jump and branch will now be described. BRANCHES AND JUMPS JMP- JUMP (UNCONDITIONAL) BEQ- BRANCH IF ZERO FLAG SET BNE- BRANCH IF ZERO FLAG CLEAR BCS- BRANCH IF CARRY FLAG SET BCC- BRANCH IF CARRY FLAG CLEAR BVS- BRANCH IF OVERFLOW FLAG SET BVC- BRANCH IF OVERFLOW FLAG CLEAR BMI- BRANCH IF NEGATIVE FLAG SET BPL- BRANCH IF NEGATIVE FLAG CLEARTHE DEMONSTRATION PROGRAMS EXERCISE 1 This is a simple program to use the X register to count down from 10 to 0. BEQ is used to detect when X reaches 0, and JMP is used to jump round a loop until X equals 0. EXERCISE 2 This program will divide 58 by 5. The X register holds the answer, and at the end the Y register holds the remainder. EXERCISE 3 This program will multiply the number contained in the X register at the start(in this case 5) by the number in the Y register at the start (in this case 6) You can easily change the program to multiply any two numbers (as long as theanswer does not exceed 255). LDX #10 LOOP DEX BEQ END JMP LOOP END BRK LDA #58 SEC AGAIN SBC #5 BMI FINISH INX JMP AGAIN FINISH ADC #5 TAY BRK LDX #5 LDY #6 LDA #0 STA 20 LINE1 TYA CLC ADC 20 STA 20 DEX BNE LINE1 JMP DUMMY DUMMY2 JMP END DUMMY JMP DUMMY2 END BRKCOMPARES So far branches have only been used for detecting whether flags are set or clear. This is, in essence, all that branches can do, but with compares, theycan do a lot more. A compare is really a subtraction that affects the flags andleaves the accumulator in its original condition. The negative, overflow, zero and carry flags are all affected after a compare, how they are affected will bediscussed shortly.COMPARES As previously mentioned, a compare is really a subtraction, leaving the accumulator intact. Therefore, a comparewill affect the flags in exactly the same way. With a compare however, it is not necessary to set the carry flag beforehand, as it is with a subtraction. You may perform a compare between any of the three registers and a memory location or an immediate number. The instruction CMP is used to compare the accumulator, and the instructions CPX and CPY are used to compare the X and Y registers respectively.COMPARES The zero flag will become set if the compare should prove both numbers equal,otherwise it will be cleared. The carry flag will be set if the number specified with the compare is less than or equal to the register used by the compare. The carry flag willbe cleared otherwise. The negative flag will be set if the subtraction (compare) produces a negative result, otherwise it will be cleared. The overflow flag will be set or cleared depending on the subtraction using the rules for two's complement as previously discussed. COMPARES There are two example programs showing the compare instructions when working. Look particularly at the flags,and how they are affected by the compareinstructions. Try changing the programs yourself, to see what happens. A SUMMARY OF INSTRUCTIONS: CMPCOMPARE ACCUMULATOR WITH NUMBER OR MEMORY LOCATION CPXCOMPARE X-REGISTER WITH NUMBER OR MEMORY LOCATION CPYCOMPARE Y-REGISTER WITH NUMBER OR MEMORY LOCATIONSTART LDX #50 LOOP1 DEX CPX #40 BEQ END1 JMP LOOP1 END1 DEY CPY #254 BNE START CLV CLC LDA #156 CMP #100 BVS ALWAYS NEVER BRK ALWAYS CMP #1 BVC NEVERPROG2 LDX #230 STX 300 LDY #0 LAB1 LDA #230 CMP 290,Y BEQ FOUND INY JMP LAB1 FOUND CLC LDX #4 LAB2 CPX #2 BCC END1 DEX JMP LAB2 END1 BRKSUBROUTINES The instruction JSR (followed by a label name) is effectively the same as the GOSUB command from BASIC. When theJSRinstruction is executed, controlis passed to the line with that label on it. When the subroutine has finished,the instructionRTS(ReTurn from Subroutine) should be executed. This instruction will return to the line after the JSR instruction. There is an example program showing the instructions JSR and RTS in use. LDA #0 LDX #5 LOOP JSR ADD8 DEX BNE LOOP BRK ADD8 CLC ADC #8 RTS THIS PROGRAM WILL MULTIPLY FIVE BY EIGHT.HEXADECIMAL Like binary and decimal, hexadecimal is a number base. Where binary is base 2and decimal is base 10, hexadecimal is base 16. A number base relates to the number of different digits used in each of the column headings. With decimal this is ten digits, the numbers 0 to 9. Hexadecimal also uses these digits, but because it uses sixteen different digitsfor each column heading, six more characters are used to represent the number digits 10,11,12,13,14 and 15. The standard way to represent these is to use A,B,C,D,E and F respectively.HEXADECIMAL So in actual fact, the hexadecimal number 12 15 11 4 could easily be misunderstood as 12 15 1 14 for example. This is why the letters A to F are used to represent the numbers 10 to 15. Using letters the above number would be CFB4. The column headings for hexadecimal are: 4096 256 16 1HEXADECIMAL This means that CFB4 in decimal is calculated thus: C = 12 * 4096 = 49152 F = 15 * 256 = 3840 B = 11 * 16 = 176 4 = 4 * 1 = + 4 ----- CFB4 is therefore 53172 in decimal.HEXADECIMAL As you have seen, a hexadecimal number bears hardly any resemblance to a decimal number. However, a binary numberbears a direct resemblance. Each group of four bits of a binary number (called a nybble) represents one hexadecimal digit. Here is the table for conversion: DECIMAL BINARY HEX DECIMAL BINARY HEX 0 0000 0 8 1000 8 1 0001 1 9 1001 9 2 0010 2 10 1010 A 3 0011 3 11 1011 B 4 0100 4 12 1100 C 5 0101 5 13 1101 D 6 0110 6 14 1110 E 7 0111 7 15 1111 F HEXADECIMAL Using the given table, it is easy to convert from binary to hexadecimal (hex)or vice versa. Also, using hex makes two's complement easier to interpret. By pressing the red function key f3 youcan switch between hexadecimal and decimal modes. This affects whether the values of the A, X and Y registers are displayed in hex or decimal. When in hex mode the '$' character is used to show a hex number. You can also use this character in a program to specify a hex number as well.HEXADECIMAL By now you must be well aware that a memory location and a register can only hold a number between 0 and 255, which is an eight bit binary number. In hex this is a number between $0 and $FF. However, you can use two consecutive memory locations to give 16 bits which can represent one number. This two byte storage has the capability to represent 65536 (256*256) different numbers. When using two memory locations to store one number, the memory location with the lowest address stores what is known as the Least Significant Byte (LSB), the next sequential byte stores what is known as the Most Significant Byte (MSB). Therefore, if the LSB holds $A1 and the MSB holds $4E then the resulting number will be $4EA1.HEXADECIMAL When in hex mode, when a load or store is performed on a memory location, the computer will print 'RESULTING ADDRESS:'followed by a hexadecimal number. This is the actual address of a memory location the computer uses for the instruction, even for indexed addressingand the like. There are two example programs using hexadecimal numbers, to help demonstratewhat has been discussed. Remember to press function key f3 to put the displayin hexadecimal mode. The first program is a 16 bit multiplication program that will multiply the value of the X and Y registers together. The result is storedat consecutive memory locations in LSB MSB format. The second program is a simple 16 bit subtraction program.A PAGE A page is a natural follow on from the definition of a hexadecimal number. A page is a commonly used term to describeAREAS of memory. It is simply the MSB of a memory address. For example, memorylocation $1EA is described as being in page 1. Any memory location between addresses 0 and 255 will therefore be in page zero. Because you can only use memory locations with addresses between 0 and 1024 (0 and 400 hex) this means you can only use 4 pages with this simulator. On a normal 6502 system without this simulator you have 256 pages of RAM and ROM (addresses 0 to $FFFF). LDX #$E9 LDY #$A LDA #0 STA 30 STA 31 LOOP TXA CLC ADC 30 STA 30 LDA 31 ADC #0 STA 31 DEY BNE LOOP LDX 30 LDY 31 SEC LDA #$2D SBC #$E2 STA 450 LDA #$3A SBC #$38 STA 451 LDX 450 LDY 451 BRKBINARY CODED DECIMAL (BCD) This is a system where decimal numbers are represented using hexadecimal numbers. For example, normally the hex number $25 is 2*16+5 (37) in decimal. However, using BCD this hex number wouldactually represent the DECIMAL number 25. When using BCD, results will be meaningless if looking at the results in decimal or hex without knowing you are using BCD. To operate BCD the 6502 instruction SED, SEt Decimal mode, will set the decimal mode flag. To CLearthe Decimal mode flag, use the instruction CLD. Once decimal mode is activated all ADC and SBC instructions will be carried out in BCD.BINARY CODED DECIMAL (BCD) If you try to add using BCD, and the result is greater than hexadecimal 99 then the carry flag will be set. If you perform a subtraction and the result is less than or equal to 0, then the carry flag will be cleared. There is an example program using BCD which should clarify what has been discussed. Remember to press function key f3 to enter hex mode, otherwise the results will be meaningless. SED LDA #$80 ADD1 CLC ADC #1 BCC ADD1 LDA #$95 TAKE5 SEC SBC #5 BCS TAKE5 CLD LDA #242 NORMAL CLC ADC #1 CMP #$1B BNE NORMAL BRKe <L`L5 L5 x"˭#̭0έ1|LV 01./@ԩˍ" #͍ԩZ:ԩ XS HHlTppppFppppppppppppppWA4(%#/-0,%4%-!#().%#/$%454/2"90!5,"5..# .%7'%.%2!4)/.3/&47!2%-5,4) "//46%23)/."94()302/'2!-4//+-!.97%%+34/#2!#+4/02//6%7%!2%./4!,,,!:94()302/$5#47!3"2/5'(44/9/57)4(/544(%53%/&!./-.)-/.# -9#/.'2!45,!4)/.34/0!5,/.02/$5#).'!.%8#%,,%.4454/2/.$)&&%2%.44/0)#3/&-!#().%#/$%4(%2%!2%%8!-0,%02/'2!-3')6%.4/$%-/.342!4%7(!4(!3"%%.4!5'(4).!,%33/.4(%3%02/'2!-3-!9"%25.7)4(4(%3)-5,!4/23(/7).'%8!#4,97(!4(!00%.3 (/7)4!&&%#434(%&,!'3!.$4(%2%')34%23%4#0,%!3%02%33#42, !4/#/.4).5%C:HHͥL ͍iiɡ  Щ hhl BwD E;HI VLS BD E,HI VLt PLEASE REMOVE ALL CARTRIDGES ANDREPEAT LOADING PROCEEDURE.THIS PROGRAM REQUIRES 32K OF MEMORYMINIMUM. L L ,/!$).'%22/2L" ̈ɿL0 L" EJK VBD EHIꠈ B VD   ΍0ύ1 xˍ"̍#XL X)Ң L( HvH`0 1ǩ|ȩD RЍЩ./oԩԩ$6%@Ԣ؝D EJKB V{{ 4  ILppB|A K:{{@ {7 L {{{` 4 4 4L$ `é Ġ 4{   4{ 4{ 4 6{{L{${ ' 4 4{ 4{{LF { L {{{AL [L L {' { 5{LF {0L :L {~{ { 4LF { { {{{{L#L {+{LF {LF *{'LF {{LF -{LF {LF ={LF {LF {  5{{L {LLF { {{{{{{  {7L{ 4{ ]L `  % 6{@L {){{ b& ]{{L7 n ]L{{   L 4 4 4 4 5p"q 4 4 4p 4q{ ]  L{{ L {0 G :A`{ș{0{`:{{80m{{L0{87m{{:{{80 m{{L\{87 m{{:{{80m{{L{87m{{:{{80 m{{L{87 m{{`)IL\{ ũ>Ʃ ō{ȱiȱi {L{ 4{` THE COMPLETE MACHINE CODE TUTOR *******************************  BY PAUL BUNN (C) 1984 NEW GENERATION SOFTWAREChoose selection by pressing RETURN. When choice is made press the SPACE BAR. ō{{ 4 4{ 4 4{{ 4 4{ 4 4 4 4 6 {{i{Ш{L LH{8 iŅȱŅĠÍ{ȱÅȱÅ 4{>7 {{ { 4{ L{{i{{LL~{ 4 4{ 4  4{LLåĩ<  6 Åą`PRESS SPACE BAR TO CONTINUE H'H{{ {0m{{{i{{Lm{0 d {L{0 {L{0  {L`m{{{i{`v{wxx ȹ` 4 4 4w q{ 4{ 4v q{ 4{ 4 4 4yxL9 q{ 4{ 4xyL,ȹxL9ȹxL9veyvxxL   6YL R  ҩD BDEJKlxBD>EHI V J L JL C:  Are you sure you want to load a stage of lessons ? (Y/N) BLV  Please position correct cassette (side two, three or four) and then press PLAY on cassette recorder followed by RETURN on keyboard to start. Do NOT press system reset whilst trying to load. LOADING ERROR. Please rewind cassette and then press PLAY followed by RETURN on keyboard to reload.{{{{{{ o7Gv#w { 4{i 4{ 4 6{{{ٽ{  {{L ;L <{ L v<{#v{{{{L1 @L vivwiw` 4 4 4" 5 { 4 6L v#w{AL{ v{#v{{{{L1 @Lv#w{#LsLNv{?v{{{{{$${{ b{LMLY @L{{ {LM{{{{L1 {{,G ( L]Lȹ{XPYLN{$,ȹ{{ {L7ͅv$w{ L.{{ bL{$)ȹ{{ {:v$w{ L.{{ bL 3v%w{ Lv$w{ L{$'{{ b{*;v$w{ L.ȹ{{ {L|v#w{ L|vP{Ev{{{{2{{{{{{{{hhL1 @L`{{{{hhL1L3{m{{{{m{i{L 4 4 4" 5 Ll{{`{` 4` 4 4 4{ { ~ 4 4 4{ { ~ 4! 4 4{ { ~ 4 4 4{{*i0 4 {jji0 4 {jjjj0i 4 {**i0 4 {ji0 4 {jjji0 4{`{L 4 4 4{  ~ 4 4 4{  ~ 4 4 4{  ~ 4 4 4{)i0 4 4 4 4,{1 4L0 4 4 4 4{jjji0 4 4 4 4{jji0 4 4 4 4{*i0 4 4 4 4{**i0 4`{ q${{{{{`{JJJJ i7{Li0{{) i7{`i0{`{{LY0{{{{8d{{id8 {{i 8{{` 4 4 4 4`{ 4 4 4 4L> 4 4 4 4L^ 4 4{ 4 4 4 4` 4 4{ 4 4 4 4`{ 4{ 4{L4{(LN{$L{{ L{{ b{ );, LN{XLN{)LNROR~SBCSTAADCyAND9CMPEORYLDALDXORASBCSTAADCuAND5ASLCMPDECEORUINCLDALDYLSRVORAROL6RORvSBCSTASTYLDXSTXADCaAND!CMPEORALDAORASBCSTAADCqAND1CMPEORQLDAORASBCSTAJMPLJSR BCCBCSBEQBMI0BNEBPLBVCPBVSp 4 4 4P{ 4{{`+ % 4 4 4{{{{i iL%ȱȱȱ e&ȱȱ e&ȱȱ e&ȱ{ &){ L) ?+`l{ 5{` %hhL {{L)` ׅ{`{{{i` {½{`{®{{{` &m{i` &m{L&+®{{{ȱi­{m{i` %®{{m{{ȱi­{` {{m{i` {{m{Lk' {{{{L+` {{{i{{q{L'`{)l{{{{h) {{`{{ {`{{ {`{{ {`{{ {L) 6hhL {L){`{L){`{L){`{-{{L){){`{ {`J{)L{h) {{`M{{L){)H(*{)L{h) {{`{{,{{) {h) {{`8{`{{ i{{ {L+{L+L({{ {{i{ {L({){`{) H({) {{q{h) {{`{)H({) {jh) {{`{L){{{)M{h) {{`{{L){{`{ {` 4 4 4# 4 6hhL  `{ {`{L){L){{{L){L){) H({) {{{h) {{`{) {{8h) {{`{) {{LR*{) {{LR*{{L){{L){{L){){`{{{L){){`{)L*`{){{`{)@`{)`{)`{)`{)`{)@` + 4L + 6hhL ERROR - ADDRESS NOT FOUND {`v+ 48 q{ 4 q{ 4{ 4`RESULTING ADDRESS $z&88+&18 89 "(88+"(88+"(88+&18 89'_88+"(88+'v8|88&18 8=8'_8 8+"(88+"(88+&18 89'_88+"(88+*;;+ &18 89"(88+"(88+"(88+&18 89 '_88+ "(88+p(999&18 89"(88+"(88+"(88+&18 89'_88+"(88+&):++ `(799+ "(88+"(88+(P98+`(799+(/9)98"(88+'}9|8r9`(79=8+(/9)9 8"(88+(P98+`(799+(/9)98"(88+*;;+ `(799+"(88+"(88+"(88+`(799+ (/9)98 "(88+y(999`(799+"(88+"(88+"(88+`(799+(/9)98"(88+"(88+(9 89 "(88+"(88+"(88+(9 89(9"98"(88+(v8 88(9 8=8(9"9 8"(88+(.:++ (9 89(9"98"(88+*;f;+ (9 89"(88+"(88+"(88+(9 89 (9"98 "(88+C)999(9 89"(88+"(88+"(88+(9 89(9"98"(88+(+;++L)998 "(88+"(88+"(88+L)998p)/9"98"(88+(}9 8r9L)99:p)/9"9 8"(88+"(88+L)998p)/9"98"(88++;;+ L)998"(88+"(88+"(88+L)998 p)/9"98 "(88+)999L)998"(88+"(88+"(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ROGRAM - PRESS A KEYaccumulator X register Y register Logical OR with number with memory locationArithmetic shift left Push processor status register onto stack memory location Break - will stop executionNot a valid instruction op-code PRESS RETURN KEY TO CONTINUEclear carry flag right left Rotate Logical AND accumulator Test BITs of accumulator against from stackPull Logical shift Exclusive OR set with memory locationAdd with interrupt disable Transfer Store at memory locationto stack pointerto accumulatorJump to subroutineJump - Load program counter with new addressDecrement Load stack pointer to X registeroverflow No operationIncrement decimal mode number Subtract with Compare not equal to zero (Z=0)equal to zero (Z=1)Branch if positive (N=0)minus (N=1)Return from subroutineto Y registerset (C=1)clear (C=0)overflow flag clear (V=0)overflow flag set (V=1){{{L; 4{ 4{ 4 6` ;{{{{{ꭀ{ L<{{{{{ u<{݀{{{{L-<{{{{L;`R< { 4" 4 6hhL ERROR - DUPLICATE LABEL FOUND " 4{ 4{ 4 6` 4 4{ 4 6 LA>{ L={BJL={ N{ BA$(A#0L=L=: { ,0L=:L<` { L={) ( L={)( {${), 0 :L=L={),0GA:Ls=hhLN{ ,0:AGL=L=L=ʅ= LL>THERE SHOULD BE A SPACE BETWEEN THE MNEMONIC AND OPERAND. THE HIGHLIGHTED LINE INDICATES WHICH LINE HAS AN ERROR.x>  4 4{ 4 4 4 4 4 6hhL A LABEL SHOULD ONLY BE UPTO SIX LETTERS LONG. THE HIGHLIGHTED LINE INDICATES THELINE WHICH HAS A LABEL TOO LONG. ?4?W?????@8@[@z@@ Lesson 19 - The stack @ Exercise 1 for lesson 19 &O Lesson 20 - More addressing modes O Exercise 1 for lesson 20 ` Exercise 2 for lesson 20 ` Lesson 21 - More on the stack a Exercise 1 for lesson 21 h Lesson 22 - The BIT instruction i Exercise 1 for lesson 22 l Lesson 23 - Interrupts m Lesson 24 - What now ? s LOAD A DIFFERENT STAGE OF LESSONS \THE STACK The stack is like a pile of plates stacked one on top of the other that can be upto 256 plates high. You can add plates to the top or remove plates from the top. If you want a plate near the bottom, you must start from the top taking off all the plates, throwing themaway until you get to the plate that youwant.THE STACK The stack is one page of memory that resides at page 1 ($100 - $1FF). There is a STACK POINTER (SP) which always points to the top of the stack. It is a one byte value and is used in a similar way to indexed addressing where the stack pointer is added to $100 to give an address that points to a data item. Each time you PUSH an item (put anitem on the stack) or PULL an item (remove a data item from the stack), thestack pointer is updated. It starts off as $FF, pointing to $1FF, and then when data items are added, it decreases, and when items are pulled from the stack it increases. Just like a memory location the stack pointer will wrap round if too many items are pulled or pushed.THE STACK All programs used with this simulator are stored in memory locations $7B00 onwards. It takes between one and three bytes (called op-code bytes if used for execution of a program) to make up an instruction. You can see the addresses and op-code bytes for each instruction displayed if you want, by pressing the yellow key marked 'SELECT' when the program is running. To revert to the normal mode press 'SELECT' again.THE STACK The stack is particularily useful in the temporary storage of data. The 6502 uses it itself. How it does so will be discussed a little later on. To keep track of which instruction is being executed a PROGRAM COUNTER (PC) isused. This makes sure that instructions are executed in the right order and it copes with jumps, branches and subroutines. It is a 16 bit number contained inside the 6502.THE STACK After each instruction the program counter is updated to point to the next instruction to be executed.Normally this means that the program counter is increased by the number of bytes in an instruction. With jumps it is loaded with a complete new value. With branches (relative jumps) there is an offset byte which is added to the program counter. It can also be a negative number if bit 7 is set (using two's complement). Therefore a branch can only change the program counter by 127 forwards or 128 backwards. This is alimitation when writing a long program but should not affect you when writing programs with this simulator.THE STACK One of the ways the 6502 uses the stackis to ensure that JSR instructions work properly, and that when a RTS instruction is executed, control is passed to the instruction immediately following the JSR instruction. This is done by pushing the contents of the program counter onto the stack. This is always the address of the memory location where the op-code for the JSR instruction is plus two. Then, when the RTS instruction is executed, this 16 bit number is then pulled off from the stack and one is added to it, and this value is put into the program counter. This has the effect of the nextinstruction executed being the one immediately after the originating JSR instruction.THE STACK Here is a list of instructions that are associated with the stack PHA- PUSH ACCUMULATOR ONTO STACK PLA- PULL ACCUMULATOR FROM STACK TSX- TRANSFER VALUE OF STACK POINTER TO X REGISTER TXS- TRANSFER VALUE OF X REGISTER TO STACK POINTER A demonstration program is given to showthe effect of these and the JSR and RTS instructions on the stack. TSX JSR NORET RET LDA #$7B PHA LDA #$12 PHA TSX RTS NORET PLA TSX PLA TSX JMP RET END TSX BRK MORE ADDRESSING MODES So far the following addressing modes have been used: IMPLIED These instructions are just three letters on their own, performing a simple task, like TXS for example. They only require one byte per instruction. ABSOLUTE These are simple instructions that affect memory locations whoose address is greater than 255 (page 1 or above). Three bytes are used per instruction. MORE ADDRESSING MODES ZERO PAGE This mode is exactly the same as the absolute mode, except that only two bytes are used per instruction. This is because the MSB is assumed to be 0 as all memory locations using this form of addressing are on page 0. IMMEDIATE This mode also requires two bytes for each instruction. Immediate is where an actual number is given instead of a memory location address. The '#' symbol is used to specify this mode of addressing with assemblers (including this one). MORE ADDRESSING MODES ABSOLUTE,Y This is the same as absolute addressing except that the contents of the Y register are added to the specified address to give a resulting address of a memory location. ABSOLUTE,X This is the same as ABSOLUTE,Y addressing except the X register is added instead of the Y register. MORE ADDRESSING MODES ZERO PAGE,Y This is the same as ABSOLUTE,Y except that a zero page address is given and the instruction only takes two bytes as opposed to three needed by ABSOLUTE,Y addressing. ZERO PAGE,X This is the same as ZERO PAGE,Y form of addressing except the X register is used instead of the Y register. This mode also only requires two bytes.POST INDEXED ADDRESSING This mode of addressing is very useful indeed. An example of using this mode of addressing is STA (78),Y. When this instruction is executed, the 6502 will look up locations 78 and 79 in zeropage. Location 78 will form the LSB of the address and location 79 will form the MSB of the address. Therefore if location 78 were to hold hex 13 and location 79 were to hold hex 2F when the program was running then the resulting address of the memory locationwould be hex 2F13. After this address has been calculated the Y register is added to it to form the final address. So if the Y register held 5 when the 6502 tried to execute the above instruction, the 6502 would store the accumulator at memory location hex 2F18.PRE-INDEXED INDIRECT ADDRESSING This is a very rarely used form of addressing. It will be described here for completeness. The instruction LDA (45,X) will be used as an example to explain this form of addressing. Whatever is contained in the X register at the time of execution is added to thezero page address in the instruction, inthis case 45. So if X contained 20 when the program is running, the final zero page address is 65. This location then forms the LSB of the final address and location 66 forms the MSB of the final address. Therefore if X contained 5 when the program was running and location 65 contained hex E2 and location 66 contained hex A1 then the accumulator will be loaded with the contents of memory location A1E2 in the above example. MORE ADDRESSING MODES INDIRECT This form of addressing is NOT supported by the simulator. However, it would be supported by a normal assembler. There is only one instructionthat uses indirect addressing and that is JMP (xxxx). This will take the contents of memory location xxxx and that will form the LSB of the JMP address. Then the contents of memory location xxxx+1 forms the MSB of the JMP address. The program counter is thenloaded with the JMP address causing the next instruction to be executed, theinstruction held in the memory location pointed to by xxxx. MORE ADDRESSING MODES Consider the following program: LDA #9 STA 320 LDA #2 STA 321 JMP (320) Although this program will not work on this simulator, it would be assembled and run on a normal assembler and 6502. This program would have the effect of loading the program counter with hex 209. The next instruction to be executed would be at memory location 209. LDA #2 STA 32 LDA #2 STA 33 AGAIN LDY #1 LOOP TYA LOOP2 STA (32),Y INY CPY #10 BNE LOOP INC 33 LDA 33 CMP #3 BEQ AGAIN BRK LDA #$F1 STA 140 LDA #3 STA 141 STA 149 STA 150 LDX #10 TXA STA (130,X) LDA #40 LDX #19 STA (130,X) LDX #10 EOR (130,X) BRK MORE ON THE STACK The flags are held in one byte in the 6502 and are arranged thus: BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0 N V - B D I Z C N - NEGATIVE FLAG V - OVERFLOW FLAG - - NOT USED (EXPANSION BIT) B - BREAK FLAG (NOT USED BY SIMULATOR) D - DECIMAL MODE FLAG I - INTERRUPT DISABLE FLAG Z - ZERO FLAG C - CARRY FLAG MORE ON THE STACK Therefore, if you think of all the flagsas a single 8 bit binary number, that is one byte, each bit represents one number from the binary column headings. For example, the overflow flag gives the number 64. MORE ON THE STACK The flags are stored in this way and are kept track of by the 6502 processor and stored in what is known as the PROCESSOR STATUS REGISTER (PSR). As you know, most instructions affect flags, but only two instructions affect the processor status register as a whole. These instructions are PHP and PLP, which will PUSH and PULL the processor status register to and from the stack. If you wanted to set, say three flags at once, you could use PHP and PLP to do so. However, there are no transfer instructions to transfer between the accumulator and PSR, so other instructions have to be used in conjunction with these instructions for them to be effective. MORE ON THE STACK An example program demonstrating how the processor status register is used in conjunction with the stack is given to help demonstrate its uses. One of the other uses for these instructions is the temporary saving of the flags. You can push the value of thePSR onto the stack by using PHP, execute instructions that normally affect the flags. Then by using PLP you can restore the flags to their original values. Remember to pull and push the right number of values, otherwise the stack will become filled up and 'dirty'. SEC TSX PHP PLA ORA #192 PHA PLP LDA #$FF PHA PLP LDA #1 LOOP PHA PLP ASL A BCC LOOP BRK THE BIT INSTRUCTION This instruction is used to check that one or more BITs of a memory location are set. The memory location is either in the addressing modes absolute or zeropage. The bits to be tested are in the accumulator, and the memory location to be tested against the accumulator is supplied with the BIT instruction in the form BIT xxxx where xxxx is a singlememory location. After the instruction the zero flag will be set if none of thebits specified by the accumulator are set. The accumulator is left unchanged after the instruction. The contents of bit 6 of the memory location are placed in the overflow flag, and bit 7 is placed in the negative flag. An example program is given to show BIT in use. LDA #$30 STA 6 LDA #0 SEC ROTATE ROL A BIT 6 BEQ ROTATE LDA #$C0 STA $3F0 LDA #0 BIT $3F0 LDA #$80 STA 560 LDA #0 BIT 560 BRKINTERRUPTS An interrupt is something that occurs after a fixed amount of time or after a certain event, like pressing BREAK forexample. When an interrupt occurs the 6502 looks up what is known as an INTERRUPT VECTOR which is an indirect jump to a small program that handles theinterrupt. This interrupt might be to read the keyboard for example. When the interrupt has finished what it is doing the program will resume as normal to where it left off before the interrupt occurred.INTERRUPTS With the simulator, you can not write your own interrupt routines. However, interrupts are occurring all the time, to check if you press BREAK or press a key. There are two types of interrupts these are: MASKABLE INTERRUPTS These are interrupts that can be changed to point to your own routine. By using the instruction SEI (SEt Interrupt disable flag) you can mask out maskable interrupts altogether, so that maskable interrupts do not occur until a CLI (CLear Interrupt disable flag) instruction is executed.NON - MASKABLE INTERRUPTS This type of interrupt is not affected by the SEI and CLI instructions. Depending on the computer, these interrupts can still have their vectors changed. All interrupts end in an RTI (ReTurn from Interrupt) instruction. Usually an interrupt routine has to save the registers it uses, by pushing them onto the stack. When an interrupt occurs the processor status register and the returnaddress are pushed onto the stack. When an RTI instruction is executed the flagsand return address are pulled from the stack and the program will continue as normal.WHAT NOW ? You have now finished the course of lessons to teach you 6502 machine code. The lessons have been designed to teach machine code so that it will be the samefor each 6502 based machine. However, to make the most of the machine, it is advisable to use what is called an OPERATING SYSTEM. This is contained in ROM and given with the computer fitted as standard. An operating system is a machine code program that lets you use advanced features of a machine like graphics and sound. It also handles routines for reading the keyboard and using cassettes and disc drives, as wellas many other functions.WHAT NOW ? As this program only simulates the running of a 6502 program, you cannot use this program in conjunction with the operating system. Therefore, it is time to use a more powerful but less user friendly assembler. Note that machine code programs run veryquickly and will not wait for you to press the return key like this one does!With a machine code program not written with this assembler there is a chance that the program could 'crash'. Therefore it is up to you to make sure you use 'safe' areas of memory. If you are ever unsure of a instruction you can always use this program to check how it works before using it in your own program.ASROLSRO% .DEƽ<><.O.AN.NOԡ.RE .REF.DEF.NOT .AND .OR ,X