home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / lib / src / coderel.s65 < prev    next >
Text File  |  1993-01-19  |  5KB  |  208 lines

  1.  
  2.          DPOKE :vre,|RELTAB-1
  3.          sec
  4.                               ; carry must be set for :loop
  5. :loop    ldy   #3             ; copy reloc
  6. :l1      lda   (:vre),y       ; dir. in..
  7.          sta   :mod-1,y
  8.          dey
  9.          bne   :l1
  10.  
  11.          lda   :vre           ; update :L1
  12.          adc   #2             ; pointer to the reloc table (+ Carry)
  13.          sta   :vre           ; INC INC might
  14.          bcc   :_foo          ; be shorter
  15.          inc   :vre+1
  16.          clc
  17.  
  18. :_foo    lda   :siz
  19.          adc   :src
  20.          sta   :siz
  21.          lda   :siz+1
  22.          adc   :src+1
  23.          sta   :siz+1
  24.  
  25.          lda   :mod           ; what to reloc
  26.          bmi   :found         ; EOP reached!!
  27.          beq   :inst          ; instructions ->
  28.          cmp   #2             ; relocatable
  29.          bcs   :data          ; data ->
  30.  
  31. :l2      lda   (:src),y       ; Xfer 1 byte
  32.          sta   (:dst),y       ; slowly...
  33.          lda   #1             ; add one to
  34.          jsr   :chk           ; pts and check
  35.          bcc   :l2            ; ok one more
  36.          bcs   :loop          ; new segment
  37. ;
  38. :inst    lda   (:src),y       ; get instr
  39.          sta   (:dst),y
  40.          and   # $F           ; examine l.sign
  41.          tax                  ; nybble
  42.          lda   :lsbtab,x      ; known val. ?
  43.          bpl   :ez            ; yep ->
  44.          jsr   :lookdeep      ; special case
  45. :ez      tax                  ; save #bytes
  46.          cmp   #2             ; need to reloc
  47.          bcc   :dochk         ; no ->
  48.          beq   :twobyte       ; no ->
  49.  
  50.          iny
  51.          jsr   :xreloc        ; maybe ...
  52.          dey
  53.          jmp   :dochk
  54. :found   jmp   (|MEMLO)
  55.  
  56. :twobyte iny
  57.          lda   (:src),y
  58.          sta   (:dst),y
  59.          dey
  60. :dochk   txa
  61.          jsr   :chk
  62.          bcc   :inst          ; ok one more
  63.          bcs   :loop
  64.  
  65.  
  66. :data    cmp   #3             ; DBYTE ?
  67.          beq   :dbytes
  68.          bcs   :eofmove       ; -able code
  69.  
  70. :ldata   sec
  71.          jsr   :xreloc
  72.          lda   #2
  73.          jsr   :chk
  74.          bcc   :ldata
  75.          bcs   :loop
  76.  
  77. :dbytes  iny
  78.          sec
  79.          lda   (:src),y       ; do the
  80.          sbc   :dif           ; same (no SEC)
  81.          sta   (:dst),y       ; here ofcourse
  82.          dey
  83.          lda   (:src),y
  84.          sbc   :dif+1
  85.          sta   (:dst),y
  86.          lda   #2
  87.          jsr   :chk
  88.          bcc   :dbytes
  89.          jmp   :loop
  90.  
  91. ; ------------------------------
  92. ; And handle the special cases
  93. ; too.
  94. ; ------------------------------
  95. :lookdeep lda (:src),y
  96.          lsr   a
  97.          lsr   a
  98.          lsr   a
  99.          lsr   a
  100.          cpx   #9
  101.          beq   :e1o2
  102. ;
  103.          cmp   #8
  104.          bcc   :e1o2o3
  105.          lda   #2
  106.          rts
  107. ;
  108. :e1o2    and   #1
  109.          adc   #1
  110.          rts
  111. ;
  112. :e1o2o3  tax
  113.          lda   :msbtab,x
  114.          rts
  115. ; -------------------------------
  116. ; This isn't absolutely clear yet
  117. ; -------------------------------
  118. :lsbtab  .byte 128,2,2,2,2,2,2,2
  119.          .byte 1,128,1,3,3,3,3,3
  120. ;
  121. :msbtab  .byte 1,2,3,2,1,2,1,2
  122.          .byte 2,2,2,2,2,2,2,2
  123. ; -------------------------------
  124. ; If we encountered the STOP
  125. ; moving code bit we keep on
  126. ; relocating. but set :SRC ==
  127. ; :DST!. Can do that only ONCE
  128. ; -------------------------------
  129. :eofmove DMOVE  :src,:dst
  130.          jmp   :loop
  131.  
  132. ; -------------------------------------------------------------
  133. ; Actual relocating action. Rite here before your eyes. No I am
  134. ; not in a Speed Demon mood this evening. Therefore another
  135. ; nice memory saving subroutine. Test whether the address is in
  136. ; the relocatable range.
  137. ; Carry must be set.
  138. ; -------------------------------------------------------------
  139. :xreloc  lda (:src),y         ; See them
  140.          sbc   #<|R_START-1   ; the try
  141.          iny                  ; to bring 
  142.          lda   (:src),y       ; the hammer down
  143.          sbc   #>|R_START-1   ; no damn
  144.          bcc   :toolow        ; chains can
  145.  
  146.          dey
  147.          lda   #<|R_END       ; hold me to
  148.          sbc   (:src),y       ; the ground
  149.          lda   #>|R_END
  150.          iny
  151.          sbc   (:src),y
  152.          bcc   :toolow        ; too HI!
  153.  
  154.          dey                  ; one up
  155.          lda   (:src),y       ; do the
  156.          sbc   :dif           ; same (no SEC)
  157.          sta   (:dst),y
  158.          iny
  159.          lda   (:src),y
  160.          sbc   :dif+1
  161.          sta   (:dst),y       ; here ofcourse
  162.          dey
  163.          rts
  164.  
  165. :toolow  lda   (:src),y       ; copy 2
  166.          sta   (:dst),y       ; w/o reloc
  167.          dey                  ; maybe a CHIP
  168.          lda   (:src),y       ; access or
  169.          sta   (:dst),y       ; a call to
  170.          rts                  ; OS
  171.  
  172.          iny
  173.          lda   (:src),y       ; do the
  174.          sbc   :dif+1         ; same (no SEC)
  175.          sta   (:dst),y       ; here ofcourse
  176.          dey
  177.          lda   (:src),y
  178.          sbc   :dif
  179.          sta   (:dst),y
  180.  
  181. ; ------------------------------
  182. ; CHK look whether we're leaving
  183. ; a segment. (e.g. DATA->INSTR)
  184. ; and update pointer according
  185. ; to the value in A
  186. ; ------------------------------
  187. :chk     tax
  188.          clc
  189.          adc   :src
  190.          sta   :src
  191.          bcc   :ok
  192.          inc   :src+1
  193.          clc
  194.  
  195. :ok      txa
  196.          adc   :dst
  197.          sta   :dst
  198.          bcc   :ok2
  199.          inc   :dst+1
  200.  
  201. :ok2     lda   :src+1         ; src+1 < siz+1
  202.          cmp   :siz+1         
  203.          bne   :not_yet       ; yes -> carry clear : continue
  204.          lda   :src           ; src < siz
  205.          cmp   :siz           ; carry clear : yes, continue
  206. :not_yet rts
  207.  
  208.