home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / msdos / decus / RB128 / intsin.txt < prev    next >
Text File  |  1995-06-04  |  8KB  |  285 lines

  1. / A second demonstration interrupt driven program.
  2. /
  3. / This program has to read characters from keyboard,
  4. /  non-digit characters will get discarded, sequences of digits are 
  5. / interpreted as representing (unsigned) decimal numbers --- a number conversion
  6. / is performed, the value read is stored in an array and added into a sum
  7. /
  8. / the program terminates when the sum exceeds one thousand.
  9. /
  10. / the program has to monitor the clock while doing other work
  11. /
  12. / the characters read must be stored in a buffer, they get fetched from there
  13. / when needed by the processing routines. 
  14. / Since the multiply by shift-and-add is slow, the buffer tends to fill up
  15. / when digits are being processed; but, its quickly emptied of any non-digit
  16. / characters
  17. *0
  18. / save program counter on interrupt
  19.         0
  20.         jmp i pints
  21. pints,  inthnd
  22. accsav, 0
  23. lnksav, 0
  24. /
  25. *20
  26. ticks,  0               / count of clock ticks
  27. intsrd, intary          / array where integer values read are stored
  28. intcnt, 0               / count of integers read
  29. total,  0               / sum of integers read
  30. inbuff, chrbuf          / array that constitutes circular input buffer
  31. pptr,   0               / " pointers " into buffer for producer and
  32. cptr,   0               / consumer
  33. nchar,  0               / character
  34. done,   0               / boolean flag. set when want to terminate
  35. /
  36. / the following variables belong to the multiplication routines
  37. /
  38. *160
  39. mplr,   0               / multiplier
  40. mpand,  0               / multiplicand
  41. prod,   0               / single length product
  42. temp,   0               / work space used by multiply routine
  43. divsr,  0               / divisor
  44. divdn,  0               / dividend
  45. quot,   0               / quotient
  46. rem,    0               / remainder
  47. kd12,   14              / 12 decimal, the number of bits
  48. kd10,   12              / 10 decimal
  49. kd100,  144             / 100 decimal
  50. kd1000, 1750            / 1000 decimal
  51. cntr,   0               / used when looping as a counter
  52. *200
  53. start,  cla cll
  54.         dca intcnt      / zero out all variables
  55.         dca total
  56.         dca done
  57.         dca ticks
  58.         dca cptr
  59.         dca pptr
  60.         clkt            / start clock
  61.         ion             / enable interrupts
  62. loop,   jms i pnxtch    / get a character
  63.         jms isdig    / check if a digit
  64.         skp             / if it wasn't, discard the character
  65.         jms readnm      / if it was, start the new number
  66.         cla cll
  67.         tad total       / check for termination
  68.         cia
  69.         tad kd1000
  70.         sma cla
  71.         jmp loop
  72. /       have read enough
  73.         iof
  74.         hlt
  75. pnxtch, nxtch
  76. /
  77. /       readnm:
  78. /               entered when have read first digit
  79. /               of a number
  80. /               complete reading characters and combining them
  81. /               in to form a number
  82. readnm, 0
  83.         cla cll
  84.         dca number
  85. lrdnm,  cla cll         / convert character to numeric
  86.         tad czero
  87.         cia
  88.         tad nchar
  89.         dca n
  90. /       now need to multiply current number by 10 decimal
  91.         tad number
  92.         dca mpand
  93.         tad kd10
  94.         dca mplr
  95.         jms i pmult
  96. /       assume that no chance of exceeding numbers allowed!
  97.         tad prod
  98.         tad n
  99.         dca number
  100. /       get next character
  101.         jms i pnxtch
  102. /       check if digit
  103.         jms isdig
  104.         skp             / if it wasn't then quit
  105.         jmp lrdnm       / if it was a digit, continue building number
  106. /       number is finished,
  107. /       store it away
  108. /       have to index into an array
  109.         tad intsrd  / base address
  110.         tad intcnt  / + index value
  111.         dca point   / = required address
  112.         tad number
  113.         dca i point
  114. /       increment count
  115.         isz intcnt
  116.         nop
  117. /       add number into total
  118.         tad number
  119.         tad total
  120.         dca total
  121.         jmp i readnm
  122. number, 0
  123. n,      0
  124. point,  0
  125. pmult,  mult
  126. /
  127. /       isdig:
  128. /               check on character in "char"
  129. /               is it a digit, (i.e. >= '0', <='9'),
  130. /               if so return its numeric value in acc
  131. /                       and MODIFY RETURN address
  132. /
  133. /       so, will be returning to address immediately
  134. /               following call if its not a digit (and acc will be zero)
  135. /           but returning to address subsequent to that if it
  136. /               is a digit
  137. /
  138. isdig,  0
  139.         cla cll
  140.         tad czero
  141.         cia
  142.         tad nchar
  143.         spa cla
  144.         jmp i isdig             / its less than zero character
  145.         tad nchar
  146.         cia
  147.         tad cnine
  148.         spa cla
  149.         jmp i isdig
  150. /       its a digit as in correct range of character values
  151.         isz isdig
  152.         nop
  153.         jmp i isdig
  154. czero,   60              / value representing '0'
  155. cnine,   71              / value representing '9'
  156. / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  157. *400
  158. /       Here is the interrupt handler
  159. inthnd,   dca accsav
  160.         rar
  161.         dca lnksav
  162. /       saves completed, now skip chain
  163.         clksf
  164.         skp
  165.         jmp clksrv
  166.         ksf
  167.         skp
  168.         jmp keysrv
  169.         hlt     / unknown interrupt
  170. /       here, have return from interrupt
  171. xit,    cla cll
  172. /       restore registers
  173.         tad lnksav
  174.         ral
  175.         tad accsav
  176. /       re-enable interrupts
  177.         ion
  178. /       do return
  179.         jmp i 0
  180. /
  181. /       clock service routine
  182. clksrv, clkcf   / clear clock flag
  183.         isz ticks
  184.         nop
  185.         jmp xit
  186. /       keyboard service routine
  187. keysrv, krb
  188.         dca temp1
  189. /       need to index into array
  190.         tad inbuff      / base address
  191.         tad pptr        / + index
  192.         dca temp2       / = required address
  193.         tad temp1
  194.         dca i temp2     / character stored in "inbuff[pptr]"
  195.         tad pptr
  196.         iac
  197. /       here we cheat a little, use a masking operation
  198. /       to force pptr to stay in range 0-177 octal
  199.         and m177
  200.         dca pptr
  201.         jmp xit
  202. m177,   0177
  203. temp1,  0
  204. temp2,  0
  205. / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  206. *600
  207. nxtch,  0
  208. /       wait till there are characters
  209. nxtl,   cla cll
  210.         tad cptr
  211.         cia
  212.         tad pptr
  213.         sna cla
  214.         jmp nxtl        / if pointers are equal then no characters yet
  215. /       need to index into array
  216.         tad inbuff      / base address
  217.         tad cptr        / + index
  218.         dca ntemp2      / = required address
  219.         tad i ntemp2
  220.         dca nchar
  221.         tad cptr
  222.         iac
  223. /       here we cheat a little, use a masking operation
  224. /       to force pptr to stay in range 0-177 octal
  225.         and nm177
  226.         dca cptr
  227.         jmp i nxtch
  228. nm177,   0177
  229. ntemp2,  0
  230. /       Here we have a "shift and add" multiply routine.
  231. /
  232. /       Several simplifying assumptions:
  233. /       1) its intended only for positive numbers (so not
  234. /               going to worry about two's complement notation
  235. /               for integers).
  236. /       2) its intended only for small numbers (the product
  237. /               of two 12-bit numbers can require 24-bits,
  238. /               that would mean simulating a double length
  239. /               register which is practical but tiresome,
  240. /               so assume that will only be working with
  241. /               small numbers  and that product will be
  242. /               represented in 12-bits; no attempt made
  243. /               to detect overflow).
  244. /       3) note the crude way of passing the arguments (multiplier
  245. /               and multiplicand) via page 0 locations.
  246. mult,   0
  247.         cla cll
  248. /       first, set up a loop that will take us through the
  249. /       12 (decimal) bits of the words to be multiplied.
  250.         tad kd12
  251.         cia
  252.         dca cntr
  253.         tad mpand
  254.         dca temp
  255. /       zero the product
  256.         dca prod
  257. lmult,  cla cll
  258. /       isolate next bit of multiplier
  259.         tad mplr
  260.         rar
  261.         dca mplr
  262. /       test if a 1, for then need to add in another
  263. /       partial product
  264.         snl
  265.         jmp mult1       / that bit was a zero, so ignore
  266.         tad temp
  267.         tad prod
  268.         dca prod
  269. /       to mult1, now  shift temp left as considering next power
  270. /       of 2
  271. mult1,  cla cll
  272.         tad temp
  273.         ral
  274.         dca temp
  275. /       check if have finished the loop
  276.         isz cntr
  277.         jmp lmult
  278. emult,  jmp i mult
  279. /
  280. *1000
  281. chrbuf, 0
  282. *1200
  283. intary, 0
  284. $
  285.