home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / INC_MAG / INC_1_1.MSA / TEXTS_SMALLMC.TXT < prev    next >
Encoding:
Text File  |  1989-12-15  |  6.3 KB  |  183 lines

  1.  
  2. Small Mcode.
  3.  
  4. When even the smallest Ataris have al least 500 kb of memory there is
  5. normally no reason to save memory by making programs small, there is
  6. hovever one thing were space is cramped and you have be economical
  7. about your code, it's when you want to make a complete program that
  8. fits into a 512 bytes bootsektor!
  9. There are two ways of looking at the lenght of a program, dynamic or
  10. static. The dynamic lenght is how many instructions are executed when
  11. the program is executed, the smaller dynamic lenght, the faster
  12. program. Static lenght is how much memory the code occupies.
  13. There are several ways of reducing codesize, either reprogram using
  14. fewer instructions, or make instructions themselves smaller.
  15. Instructions can be of three different sizes 2, 4 and 6 bytes.
  16.  
  17. First a list on the size of different instructions, all these have
  18. the same effect and only differs in size!
  19.  
  20.  
  21. Machine Code    Mnemonics                Comment
  22.  
  23. DFFC00000002    add.l     #2,sp          This adjust the stackpointer
  24. 4FEF0002        lea       2(sp),sp       Only works upto 32k.
  25. 548F            addq.l    #2,sp          Only works upto 8.
  26.  
  27.  
  28. 203C00000013    move.l    #19,d0         Inits a dbra loop.
  29. 303C0013        move.w    #19,d0         only word is needed.
  30. 7013            moveq.l   #19,d0         only upto 128.
  31.  
  32.  
  33. 227C0000044E    move.l    #$44e,a1       Move from an adress
  34. 2278044E        move.l    $44e.w,a1      Only works in low memory
  35.  
  36. 227900000074    move.l    hej,a1         move from fixed adress.
  37. 227A004A        move.l    hej(pc),a1     relative is smaller
  38.  
  39.  
  40. 4EF900000074    jmp       hej            Jumps.
  41. 60000038        bra       hej            Branch is always smaller.
  42. 6034            bra.s     hej            only short distance.
  43.  
  44.  
  45. There are also some more tricks to make code smaller, Below are two
  46. examples that sets medium resolution.
  47.  
  48. 3F3C0000        move.w    #0,-(sp)            setscreen 28 bytes
  49. 2F3CFFFFFFFF    move.l    #-1,-(sp)
  50. 2F3CFFFFFFFF    move.l    #-1,-(sp)
  51. 3F3C0005        move.w    #5,-(sp)
  52. 4E4E            trap      #14
  53. DFFC0000000C    add.l     #12,sp
  54.  
  55. 2F3CFFFF0000    move.l    #$ffff0000,-(sp)    setscreen 24 bytes.
  56. 2F3CFFFFFFFF    move.l    #-1,-(sp)
  57. 2F3C0005FFFF    move.l    #$0005ffff,-(sp)
  58. 4E4E            trap      #14
  59. 4FEF000C        lea       12(sp),sp
  60.  
  61. By moveing more data at a time to the stack, code can be reduced in
  62. size.
  63.  
  64. Here is a source list of this disks bootcode. The original program was
  65. 176 bytes long, after considering the above info I got it down to 100
  66. bytes.
  67.  
  68.  
  69. start
  70.         lea     tend+30(pc),a5
  71.         move.l  a5,a6
  72.  
  73. * These two lines are the only initialization needed, where to find
  74. * the scrolltext, as you can see I set end of text marker and text-
  75. * pointer to the same value, starting scrolling at the end of the text
  76. * tend+30 means 30 spaces added after the scrolltext.
  77. * (It's 30 ascii 0 really but they both look the same so it doesn't 
  78. * matter)
  79.  
  80. key     moveq.l #3,d7
  81. shut_up bsr.s   shift
  82.         bsr.s   shift
  83.         move.l  #$25000b,-(sp)
  84.         trap    #14
  85.         addq.l  #2,sp
  86.         dbra    d7,shut_up
  87.  
  88. * When I move #$25000b to the stack and later adjust SP with 2 I
  89. * will exit the loop with four words of 000b on the stack, I need
  90. * these later on.
  91.  
  92.         cmp.l   a5,a6
  93.         bne.s   cont
  94.         lea     text(pc),a6
  95. * I check and see if I've reached the end of the scrolltext!
  96. * This will be true when starting the code so I reset a6 to start
  97. * of text.
  98.  
  99. cont    dc.w    $a000           fontbase
  100.         move.l  4(a1),a1
  101.         move.l  76(a1),a1
  102. * The original did this at the beginning and stored the result in a
  103. * data register, moveing it here saved a few bytes.
  104.  
  105.         move.b  (a6)+,d6
  106.         add.l   d6,a1
  107.         moveq.l #7,d0
  108. new_char
  109.         move.b  (a1),(a4)
  110.         addq.l  #2,a4
  111.         lea     256(a1),a1
  112.         dbra    d0,new_char
  113. * nothing very fancy here, only a4 that got it's value in the 
  114. * shift subroutine.
  115.  
  116. vsync   trap    #1
  117.         addq.l  #8,sp
  118.         tst.b   d0
  119.         beq.s   key
  120. * When I get here, the stack will contain 4 word of $000b, so I use
  121. * those and adjust SP accordingly.
  122.  
  123.  
  124. shift   move.l  $44e.w,a1               find screen
  125.         lea     3354(a1),a1     2-red,4-green,8-blue!
  126. * gets screenbase and adds a number so I can scroll bottom to top.
  127.  
  128.  
  129.         move.l  a5,a4
  130.         moveq.w #7,d0
  131. rows    lsl.w   -(a4)
  132.         moveq.w #19,d1
  133. left    roxl.w  (a1)
  134.         subq.l  #8,a1
  135.         dbra    d1,left
  136.         dbra    d0,rows
  137. * This is where I get the a4 value I use later on.
  138.  
  139.         rts     Return with RTS!
  140.  
  141. text    dc.b    "This is the bootscroller in this mag. "
  142.         dc.b    "This code is now 100 bytes long "
  143.         dc.b    "and I don't think I can make it any smaller.   "
  144.         dc.b    "Bootscrollers are nice, you don't have to write "
  145.         dc.b    "long scrolltexts!!!  Only 383 bytes of chars!! "
  146.         dc.b    "Greetings to Bluestar.  Thats all folks!     "
  147.         even
  148. tend    dc.b    ""
  149.  
  150. If you know how to make this smaller or If you already have a smaller
  151. scroller, please let me know. I think this is the smallest possible
  152. all char scroller on the ST.
  153.  
  154. Hints and tips:
  155.  
  156. Use the info above, if you only need to add a number between 1 and 8
  157. use addq, between 1 and 32000 use lea.
  158.  
  159. Using PC offset is double effective when compiling to PRG files, it's
  160. smaller in itself and it doesn't require an entry in the relocate
  161. table.
  162.  
  163. You can, when having several traps after each other, adjust the
  164. stackpointer only after the last one.
  165.  
  166. Try not to use any memory locations to store data if possible, moving
  167. between two register takes only 2 bytes of memory and diskspace.
  168.  
  169. Remember that speed is not that important, if you need the physbase DO
  170. NOT find it before the program starts and store it somewhere, but get
  171. it only when you need it. That goes for all initialization, if you can
  172. get it within the code, do so. It will save you the instructions to
  173. store it first and recall it later.
  174.  
  175. Search for instructions that appear two times or more where you put
  176. values in data or adress registers, perhaps you can alter the code so
  177. that only the first initialisation is needed.
  178.  
  179. Most important, admit defeat, when you've tweaked and fiddled with 
  180. your code so it's only half the original size it might be impossible 
  181. to compress it further.
  182.  
  183.