P720 CEEFAX 720 Wed 30 Dec 21:05/32 |B2201212|a17T/SWR09|i14TEXT|m10|s÷n1÷eM astering Sideways ROM & RAM - Module 09 Numerical argpldnts  ---- I n how to use the MOS to read  rguments by defining new Osbyte and Oswo rd routines. In I you one way of reading n—ldrical argplfn ts and  T here is no one correct way of reading nu merical arguldnts and, as  ramming problems, there are as many solu tions as there  The method I will demonstrate is quite useful for sideways ram programs because it uses the minimum amount of  ry and restores that memory after use. The method demonstrated in this module will read a hexadecimal  range from &00 to &FFFF and convert the argwlfnt into  The routine includes range and erro|c
P720 CEEFAX 720 Wed 30 Dec 21:01/50 |B2202212x—17T/SWR09|h14 DXT|m336D|s÷n1÷ er  s The binary representation of  ment is stored in two bytes of zero page memory ready to be  the routine. These two bytes can be rest ored   hen you design yoqr SWR programs you sho uld anticip—te how they I the instructions for using your program tell the user  e hexadecimal argpldnt one-two-three, yo ur  23 and 0123 as all the sald,  g—ldnt. Your program should not insist o n using leading  ) prefix if a hexadecimal number is expe cted  enerating an arror. I idea to get your SWR program runnhng wit h a *  and use Osrdch to read it byte after  e. That style of programming works b|c
P720 CEEFAX 720 Wed 30 Dec 21:34/07 er  s. The binary representation of  ment is stored in two bytes of zero page memory ready to be  the routine. These two bytes can be rest ored   hen you design your SWR programs you sho uld anticipate how they I the instructions for using your program tell the user  e hexadecimal argrlfnt one-two-three, yo sr  23 and 0123 as all the same,  gument. Your program should not insist o n using leading  ) prefix if a hexadecimal number is expe cted  enerating an error. I idea to get your SWR program running wit h a *  and use Osrdch to read it byte after  e. That style of programming works b|c
P720 CEEFAX 720 Wed 30 Dec 21:20/21 |B2203212|a17T/SWR09|i14TEXT|m36D8|s÷n1÷ eut it looks very unprofessional  ting a program which allows the user to edit the number with D uite complicated. The argrlfnt(s) should follow the  ple, you have written a program which  ints the contents of a memory location a nd you want to use it to  nts of location &900 your SWR interprete r should accept  igure 9.1 to call the routine and print the  PEEKR P EEK &0900 R R FV commands and arguments  ----------------------- U ASCII characters can be used to represe nt a two byte T bble represented by the first ASCII  acter depends on how many characters are used to represent the F le, the A in &AB represents the most|c
P720 CEEFAX 720 Wed 30 Dec 21:00/42 |B2204212|a17T/SWR09|i14TEXT|m3A43|s÷n1÷ e significant  icant byte but the A in &ABC represents the  significant byte. IAS CII characters fpom right to left, inste ad of thd  e first byte you read will always be the  ignificant byte. If you read  ers from left to right you either have t o know in advance  e used so that, if necessary, you can pa d out  ou have to type the leading zeros to  ablish the place value of each character in the argpldnt. TASCI I representation of a —exadecimal argume nt into  least significant nybble of tha least ignificant byte unless you use an alabor ate conversion algorithm or  s. This presents a problem because t|c
P720 CEEFAX 720 Wed 30 Dec 21:21/44 e significant  icant byte but the A in &ABC represents the  significant byte. IAS CII characters from right to left, inste ad of the  e first byte you read will always be the  ignificant byte. If you read  ers from left to right you either have t o know in advance  e used so that, if necessary, you can pa d out  ou have to type the leading zeros to  ablish the place value of each character in the argrlfnt. TASCI I representation of a hexadecimal argrlf nt into  least significant nybble of the least ignificant byte unless you use an elabor ate conversion algorithm or  s. This presents a problem because t|c
P720 CEEFAX 720 Wed 30 Dec 21:20/00 ehe Gsread routine used to read the ASCI I characters from the argslfnt reads fpo m left to  T em you can read each nybble of the  ent, convert it into binary and push it on the stack until yoq  ion character. Then pull each nybble off the stack  rder, as if they had been read from righ t  I number of nybbles read from the argumen t  les off the stack and convert  e, or pair of nybbles, into a byte. This technique is   CLC\ inate with space, return or " JS R &FFC2 \ initialise argpldnt with G sinit LDX\ sult = &0000 STX\ st sig. byte of result STX \ most sig. byte of result 
P720 CEEFAX 720 Wed 30 Dec 21:01/04 |B2205212|a17T SWR09|i14TEXT|m3DAE|s÷n1÷ ehe Gsread routine used ASCI I characters from thd arg—ldnt reads fpo m left to T em you can read each nybble of the  ent, convert it into binary and push it on the stack until you  ion character. Then pull each nybble off the stack  rder, as if they had been read from righ t  I number of nybbles read from the arg—lfn t  les off the stack and convdrt  e, or pair of nybbles, into a byte. This technique is   CLC\ inate with space, return or " JS R &FFC2 \ initialise argument with G sinit LDX\ sult = &0000 STX\ st sig. byte of result STX \ most sig. byte of result 
P720 CEEFAX 720 Wed 30 Dec 21:00/30 ÷e DEX \ X will count numbe r of nybbles - 1  JSRF FC5 \ read character from arguldnt w ith Gsread BCS\ h if termination character CMP ASC("&") \ is it "&"? BEQ p \ ignore "&" JSR\ convert nybble into binary PHA \ push nybble on stack  INX \ count nybble in X regis ter BPL\ ext character  CPX \ more than 4 characters? BC S error \ branch if too many  PLA \ pull LS nybble of LS by te STA\LS yte DEX\ only nybble? BMI\ nch if it is PLA\ l MS nybble of LS byte ASLA \ shift ASLA\ t ASLA\ 
P720 CEEFAX 720 Wed 30 Dec 21:00/54 |B2207212|a17T/SWR09|i14TEXT|m41483|s÷n1 ÷e ASL A \ bits ORA 0 \ OR with &70 STA \ and store LS byte DEX \ was that the last nybble?  BMI finish \ branch if it was  PLA \ pull LS nybble of MS byte STA\MS byte DEX\ he last nybble? BMI\ branch if it was PLA\ pull MS nybble of MS byte ASLA \ shift ASLA\ left ASLA\  ASL A \ bits ORA \ OR with &71 STA \ and store MS byte  F .2 Converting from ASCII to binary  ------------------------------------- A interpreter will pass control to  el ".found". The carry flag should be cl eared before 
P720 CEEFAX 720 Wed 30 Dec 21:30/00 |B2208212|a17T/SWR09|i14TEXT|m417ED|s÷n1 ÷ent so that either a second quotation m ark, a  e read as the termination character  he argument. IX ster is loaded with zero and then the  ntent of X is stored in the two consecut ive memory locations to be  result. The X register will be used to c ount the number ASCII from the argpldnt minus one X is decre mented XFF racters have been read. Thc argument  read one character at a time, ignoring a ny leading ampersand. Each  converted from an ASCII character into a binary nybble  The X register is incremented with every NT routine will keep reading until  ination character is found. A ding the termination character the numbe r stored in the X 
P720 CEEFAX 720 Wed 30 Dec 21:11/40 |B2209212|a17T/SWR09|i14TEXT|m41B57|s÷n1 ÷e between 0 and 3 inclusive. If it is o utside that  ters have been included in the argelfnt or I her case a branch to an error  ll be made. W ave been read from the argelfnt the rout ine  f the stack. The X register is  ed and tested after each nybble has been processed to see if  first one read from the argrlfnt. T ere must be at least one nybble to pull off the stack and not more T e first nybble pulled off the stack can be stored in the  d for the least significant byte. If a s econd  bits shifted left four times and OR ith the first nybble to form the least s ignificant byte. A third  tored in the memory location reserve|c
P720 CEEFAX 720 Wed 30 Dec 21:11/05 |B220A212|a17T/SWR09|i14$FXT|m41EC1|s÷n1 ÷ed for the most  forth nybble should have all its bits sh ifted OR third nybble to form the most  t byte. T from the two consecutive zero page  y locations. No other part of user memor y or SWR has been used by  . The error routine and the ASCII charac ter to binary  ne are only refered to by name in figure 9.2  found in the program PEEK. T am PEEK uses the conversion routine outl ined in figure 9.2  command *PEEK N To sse the p rogram  into SwR and press thg Break key. Y an use the new command to print the hexa decimal value of the  y byte of the I/O processor's memory. Fo r example, PEEKPEEK
P720 CEEFAX 720 Wed 30 Dec 21:31/22 |B220B212|a17T/SWR09|i14TEXT|m4222B|s÷n1 ÷ed *PEEK "900" are all equivalent and w ill all  in the I/O processor's memory. T ogram uses a one-command interpreter (li nes 300-570) similar  the earlier modules of the course. It s ses the  e argument and store the binary  on in two zero page bytes (lines 580-105 0). The read and  n modified slightly to store the two zer o page  k before making the conversion (lines  0-640). The two bytes can then be restor ed before returning to the MOS 00-1130). T used with post-indexed indirect  ing to load the accumulator with the byt e specified by the  1070). The accumulator content is printe d using  0-1340. After printing the result th|c
P720 CEEFAX 720 Wed 30 Dec 21:10/51 |B220C212|a17T/SWR09|i14TEXT|m42595|s÷n1 ÷ee  nes 1100-1130)N The registers  the stack by the interpreter are pulled off to balance the  0) The accumulator is reset to zero (li ne 1170)  he request has been recognised, and  rol is returned to the MOS (line 1180). TASCII tine is in lines 1350 to 1480. A s converting the ASCII character into a binary nybble the  for valid characters If an invalid cha racter  rror routine (lines 1490-1660)  o the one introduced in Module 8. B ause errors can be detected at any stage of the conversion,  age bytes when an error is found would a dd extra  ine All the information you need to  tore the zero page bytes from the er|c
P720 CEEFAX 720 Wed 30 Dec 21:12/11 |B220D212|a17T/SWR09|i14TEXT|m428FF|s÷n1 ÷eror routine is available in the X ster. If you need the exercise you could modify the error  zero page bytes before printing the err or I o balance the stack as well  ill be reset by BASIC when control passe s to the BRK   10 REM: PEEK MODE HIMEMC 00 DIM HI MEM   0 F FFC  100 gsread=&FFC5 FFE  0 osnewl=&FFE7 FFF F OR pass = 0 TO 2 STEP 2 PHIMEM 160 [ OPT pass BRK 180 BRK BRK  0 JMP service+diff  OPT FNequb(&82) OPTFN b((copyright+diff) MOD 256)  BRK  OPTFN qus("PEEK")  
P720 CEEFAX 720 Wed 30 Dec 21:33/39 |B220E212|a17T/SWR09|i14TEXT|m42C69|s÷n1 ÷e BRK OPTFN C) Gordon Horsington 1987")  BRK  CMP 4 BEQ  RTS   PHA TXA  PHA TYA PHA LDXFF  420 INX LDA +diff,X BEQ  LDA (comvec),Y INY 470 CMP £ASC(b.")  BEQ found ANDDF  CMP title+diff,X B EQ comloop PLA  TAY PLA T AX PLA RTS 580 nfound CLC  JSR gsinit LDA PHA LDA ress+1 PHA L DX £0 STX 
P720 CEEFAX 720 Wed 30 Dec 21:20/00 |B220F212|a17T/SWR09|i14TDXT|m42FD3|s÷n1 ÷e0 STX address+1  DEX  JSR ead BCS  CMP £ASC("&") BEQ op JSR  PHA \ Push hex nybbles INX BPL loop  CPX 800 BCS error PL A \ LS nybble low byte  STA address DEX  40 BMI finish 850 PLA \ MS nybble low byte  ASL A ASLA  ASL A ASLA  ORA address STA ss \ LS byte DEX  BMI finish PLA \ LS nybble high byte  STA address+1 DEX  BMI finish PLA \ MS nybble high byte 
P720 CEEFAX 720 Wed 30 Dec 21:24/20 ÷e ASL A 1000 ASL A  ASL A ASLA  ORA address+1 STA address+1 \ MS byte   LDY £0 LDA ss),Y JSR  90 JSR osnewl PLA 1110 STA address+1  PLA STA  PLA PLA  PLA LDA  RTS  PHA  210 LSR A LSRA  230 LSR A LSRA 1 250 JSR nybble+diff  PLA  AND F SED CLC  310 ADC £&90 ADC &40 CLD JMP osasci  CMP £ASC("9")+1 BCS  380 CMP £ASC("0") 
P720 CEEFAX 720 Wed 30 Dec 21:30/44 |B2211212|a17T/SWR09|i14TFXT|m436A7|s÷n1 ÷e BMI error ANDF  410 RTS   SBC £&37 CMPA  0 BMI error CMP 10 BCS  RTS  LDA rormsg+diff) MOD 256 STA ddress LDA ) DIV 256 STA  540 LDY £&FF   560 INY LDA ss),Y STAY  BPL errorloop JMP errstack   BRK BRK OPT FNequs("Out of range") BRK OPTFNFF  stbyte ] NEXT INPUTS ave filename = "filename$ IF ame$="" END SAVE $+" "+STR$|÷(HIMEM)+" "+STR$|÷(las  tbyte)+" FFFF8000 FFFF8000" X
P720 CEEFAX 720 Wed 30 Dec 21:21/10 |B2212212|a17T/SWR09|i14 DXT|m43A0F|s÷n1 ÷e%=save MOD 256 YDIV 1750 *OPT1,2 CALL OPT 1,0 END DEFFN  0 ?P%=byte PP   30 DEFFNequw(word) PMOD 6 PDIV PP 1870 =pass DEFFN  0 !P%=double PP  1920 DEFFNequs(string$) P $ PPLEN  c
P720 CEEFAX 720 Wed 30 Dec 21:00/00 ÷e%=save MOD 256 YDIV 1750 *OPT1,2 CALL OPT 1,0 END DEFFN  0 ?P%=byte PP   30 DEFFNequw word) PMOD 6 P ?1=word DIV 256 PP 1870 =pass DEFFN  0 !P%=double PP  1920 DEFFNequs(string$) P $ PPLEN  c