P729 CEEFAX 729 Wed 3 Feb 21:00/40 |B220120F SBITS - An Exploration of the BBC Micro at Machine Level By Progradldp  ..... .................................N ............ PASCIIB Convfrsion I putting numbers into a computer  can forget that they are not in the righ t formatN TASC II value of the particular  indo the micro rather than the numbdr i tself. S is needed to take a string ASAHI actdrs that rdpresent a number and conve rt them  n which the program can  I evious module we produced a routife that took  efusing to accept invalid  d put the rdsult into a small chunk of m emory T gram rdturns in this  ine in a larger program which will l|c
P729 CEEFAX 729 Wed 3 Feb 21:05/17 |B220220F|a17T?OSB12|i14TFXT|m336D|s÷n1÷ eeave  ace in thf computer  O umber is in memory we can perform arithm etical  multiply and dividf. Once  ult, that will be a binary number in mem ory and  rt in the opposite  ASCII. B a buffer. The first digit   ng could be a number, or a minus sign. I'll F let's look at the  . A t contains the   1234 R s a string of four characters, not a num ber I the left hand end we NNI know that snder the Arabic numeral syst em  on it's position, but ASCII
P729 CEEFAX 729 Wed 3 Feb 21:01/30 |B220320F einary conversion we do not snow, yndeed do not  ctlyN If we look at the  in the string we find the '2' but more mportant we now know that the '1' is in fact worth 10 so we  l' of 12. As we look at the next  on we find a '3' and we know that  2' to the left  giving us a total of 123N Finally  nd a '4', the figures to our left are no w worth 1230  234, which is what was blindingly  s in the first place! L is in terms of a computer algorithm (a ecipe if you like) and that goes like th is: M 10 A ntly being read to the  o farg R o more digits  'so far' is of coursf zfroN I 
P729 CEEFAX 729 Wed 3 Feb 21:00/01 |B220420F eld perhaps call it the partial result o r the accumulated  his algorith m has an advantage in that it can rdadil y be  other than 10N In that  ead: M y the base A t currently being rfad to the  mber 'so far' R no more digits T nanswered questions hereN Just how  ou deterehne that there are no more digi ps? The easiest  l you hit a character that is not in  r character set In our case, for decim al input, we  racter that is not between zfro  . WT s easy for the numbers  e ASCII codds for them are &30 to &39 or 48 NW AS HJ. S
P729 CEEFAX 729 Wed 3 Feb 21:14/28 |B220520F eltiply a numbfr by the base in  carry out this input procedure. The ro utine in  enough to cope with any base  although sorting out the valufs of the c haracters  problems) but for the time  se it to dfal with decimal numbfrsN The bases  are binary (2) octal (8)  d hexadecimal (16). T that can bf hfld in 4 bytes using 2's  mplement negative is 2147483647 positive or M 2147483648  nd to &7FFFFFFF and &80000000N In  in this routine I have sacrificed the ab ility to enter M der to simplify the coding a T e largest negative number is M 214748364 7, but I  ailing and gnashing of teeth. S ow do we multiply in machine codf? |c
P729 CEEFAX 729 Wed 3 Feb 21:00/40 |B220620F ÷e£dll  ood old fashioned long  hich is absurdly simple in binary since you  on table. L (i.eN 27 by 11)  x  11   011000   11011   100101001 which is 297 I you can see that for each set bit of th e multiplier  e space between the dotted linesN J like decimal long multiplication thd row s are offset to  o the 'power' of the bit the row  to. You should note also that you essd ntially only  table and that the adding up of  ial products is more difficult for a mer e human than 
P729 CEEFAX 729 Wed 3 Feb 21:01/15 |B220720F|a17T/OSB12|U14TEXT|m41483|s÷n1 ÷eoduces each partial product. I ne code the 'difficult' adding is taken care of ADCNS those partial products  t ...N well that's rotating isn't it? S o   ll actually do this backwards so it look s more like  x  1011   11011  110110   ---------  101001  T e the rightmost (least significant bit) of the multiplibq  he multiplicand to the contents of the artial product workspace (ppws) which is empty to start  nothing. R d the multiplicand left. R stages until the multiplier is zero|c
P729 CEEFAX 729 Wed 3 Feb 21:15/30 |B220820F417ED|s÷n1 ÷e. I er first because you use  to check whether a bit is set, so it loo ks  roduct are less than a   .loop LSR multiplier BCC#  CLC LDAM ltiplicand ADC  STA ppws  #add BEQ  ASL multiplicand J MP loop I a byte (as it was in our long  ation example above) then you simply hav e to add  ytf, after you have added the  togetherN Let's say that the partial pr oduct was  u were adding in a value from  latorN It would look like this:  CLC 
P729 CEEFAX 729 Wed 3 Feb 21:02/02 |B220920F OSBTEXTB ÷e ADC ppws STA LDA £0  ADC ppws+1 STA pws+1 N y before the second  I sible to hold the multiplier in thd accu mulator  ule) but if either  icand are grdater than 255 then  te rotating and multi-bytf addition must be usedN MI rdady covered Multi-byte  traightforward enough. Basically your f irst ASLLSR becausd you do not want  carry flag at first, but other rotation s are ROLROR carry flag to carry NH e is an example with some me-fry called mem to mem+3 being rotated left, which m ultiplies its value   ASL mem 
P729 CEEFAX 729 Wed 3 Feb 21:00/20 |B220A20F|a17T/OSB12|i14TEXT|m41EC1|s÷n1 ÷e ROL mem+1 ROL +2 ROL T ltiplication in B/osb12 is a compromise in that thd  but the multiplier is a single  ually 10) I hope this will h—bd the te chnIque I om input and output of  ou a general multi-bytf muldiplication outine which we can use later when we co me on to numbfrs  Having discussdd the techniques let's ru n through the I t is copiously annotated, which  a bit longN If you have a micro whtho0 t second  will havd to go into mode 7  though it is best displayed as a listing in mode  IBI e decided not to enable the  ting which is why line 190 goes to 2 ins tead of NT
P729 CEEFAX 729 Wed 3 Feb 21:10/48 |B220B20F ÷eovfr 500 bytes and I have  0 bytes of memory in line 170N To help testing  t positive and negative  T angfs to the input routine from last tim e are as  T creased as 11 digit numbers, incl2ding minus sign, can be accepted. A plus si gn has been addfd  listN The zero flag is set on exit  the string is of zero length. T body of the code, from line 260 to line 400, calls  rapping ESCAPEs and zero length  (lines 370 and 380). The swbroutine sta rting at # y that from the last module. T inf at 'convert#AS HHNto#binary' tases t he string  ts it into a 4 byte binary  aves the result in 4 bytes of memory sta rting # T
P729 CEEFAX 729 Wed 3 Feb 21:11/00 |B220C20F|a17T/OSB12|i14TEXT|m42595|s÷n1 ÷e subroutine is at line 1550. Note tha t the  seN I have been known to  his with silly results! Thf sign flag i s   g in the input buffer a character at  ime ssing X as a counter/index. If a ch aracter is not  to 'not#numbergN There, if we are  ing at the first character in the string we check for a M e sign flag if it is a minusN A +  st ignoredN Any other character would c ause an  should not occur here as the  ne filters other characters out. A non numfric  tring is counted as  ngN This terminator will usually be a arriage return.  ways of dealing with a negative N I could turn thf number negativf a|c
P729 CEEFAX 729 Wed 3 Feb 21:01/30 |B220D20F|a17T/OSB12|i14 DXT|m428FF|s÷n1 ÷efter it I y build up a negative number  number entry routineN In the end I deci ded to  akes the code simpler and  light side effect of invalidating the la rgest I entionfd beforeN Aft—p  fen tfrMhnated, and the code reaches the ## ck to see if the sign I t is the number is negative and we simpl y M 80). I emory as a variable to  f this routine to other bases more  ghtforward You should try to convert t o base 8, or  derstand how it works and that I  plained it clearly enough. In each case , of course,  racters 0-7 for octal and 0-9 AF
P729 CEEFAX 729 Wed 3 Feb 21:00/00 |B220E20F ÷efor hexadecimal. T w e first subtract thd ASCII value of '0' from it (48) and then call a subroutine at 'process#numberg. A we multiply the number held in dic—t#ws  it (held in  produce a new partial result in # . NNH p that catches any  sets the top bit (becomes negative) during this ddition If that happens the number has overflowed 4 NT at line 3050. I have chten the  rotate error traps different messages so you can see  deliberatdly enter huge numbfrs. Y ght try deleting the traps and seeing wh at happensN T bit belt and braces since I am  nced it catches anythin£. d save X to the stack because X is holding the c|c
P729 CEEFAX 729 Wed 3 Feb 21:00/20 |B220F20F ÷eurrent  d so we must not lose it. T## base' subroutine uses 4 bytes at  as  he multiplier in the  the timeN If a number rotates to set hd top bit thdn we have another overflow situation and that  160N Otherwise this section of code is a straightforward multiplication where t he multiplier is  thf multiplicand is 4 bytes in size. I wanted to put the final result back in digit#ws so lines  cross there. The BASIC routine  line 3430 and 3460 enables you to check the results. T with multi-byte multiplication,  led multiple precision multiplication, i n a more  