home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / enterprs / c128 / text / examples.arc / FIND.A < prev    next >
Encoding:
Text File  |  1989-12-01  |  9.0 KB  |  236 lines

  1. ; find.asm
  2. ;----------------------------------
  3. ; Command:   find/text/,range
  4. ;----------------------------------
  5.  
  6.                                   
  7. poker       = $0016               
  8. z61         = $0061               
  9. runmod      = $007f               
  10. txtptr      = $003d               
  11. ierror      = $0300               
  12. chrget      = $0380               
  13. chrgot      = $0386               
  14.                                  
  15. int04       = $1704               
  16. int05       = $1705               
  17. int0e       = $170e               
  18. stk0        = $1be7                 ; CS-DOS 'main' saves stack pointer here
  19. tx          = $1bfe                 ; Text/Basic mode flag
  20.  
  21. linget      = $50a0
  22. fndlin      = $5064               
  23.  
  24. chrout      = $ffd2
  25. stop        = $ffe1               
  26.                                   
  27. star        = $0b00               
  28.             .wor star             
  29.             * = star              
  30.  
  31.             jmp find
  32.             dw Date
  33.                       
  34. find        lda ierror            ;save ierror vector
  35.             sta svie              
  36.             lda ierror+1          
  37.             sta svie+1            
  38.             lda #<chng            ;and make a new one for now
  39.             sta ierror            
  40.             lda #>chng            
  41.             sta ierror+1          
  42.             ldx stk0              ;recall stack at "lookup"
  43.             txs                   
  44.             lda #$40              
  45.             sta $ff00             
  46.             rts                   ;and return to main
  47.                                   
  48. svie        .wor 0                
  49.                                   
  50. ;-------------------------------
  51. ; adjust for text or basic mode
  52. ;-------------------------------
  53.                                   
  54. adj         bit tx                ;basic mode?
  55.             bmi adj0              ;no text. un-crunch the line
  56.             rts                   
  57.                                   
  58. adj0        ldx #0                ;copy command line back to input buffer
  59.             jsr int04             
  60. adj1        sta $0200,x           
  61.             inx                   
  62.             jsr int05             
  63.             cmp #0                
  64.             bne adj1              
  65.             sta $0200,x           
  66.             rts                   
  67.                                   
  68. ;-------------------
  69. ; ierror jumps here
  70. ;-------------------
  71.                                   
  72. chng        lda #0                ;bank 15
  73.             sta $ff00             
  74.             sta runmod            
  75.             lda svie              ;restore old error vector
  76.             sta ierror            
  77.             lda svie+1            
  78.             sta ierror+1          
  79.             jsr adj               ;adjust buf if text mode
  80.             lda #<$01ff           ;setup chrget
  81.             sta txtptr            
  82.             lda #>$01ff           
  83.             sta txtptr+1          
  84. fc          jsr chrget            ;1st non-alpha char is the delimiter
  85.             cmp #"a"              
  86.             bcc gotdel            ;got it
  87.             cmp #"z"+1            
  88.             bcc fc                
  89. gotdel      sta delim             ;save it
  90.             ldx #0                
  91.             jsr gettxt            ;get replace text
  92.             jsr chrget            ;move past closing delimiter
  93.             beq ch1               ;no range
  94.             cmp #","              
  95.             beq ch11              
  96.             jmp snerr             
  97.                                   
  98. ch11        jsr chrget            
  99. ch1         jsr range             ;get range
  100.             jsr b15               
  101.             lda #13               
  102.             jsr chrout            
  103.             jmp za32a             
  104.                                   
  105. za31f       iny                   ;bump past eol marker (0)
  106.             tya                   
  107.             clc                   
  108.             adc z61               
  109.             sta z61               
  110.             bcc za32a             
  111.             inc z61+1             
  112. za32a       jsr peek              ;get link high
  113.             beq fini              ;end of text...quit
  114.             jsr stop              ;stop key?
  115.             beq fini              ;yep
  116.             jsr za431             ;test if line lower than max
  117.             bcs za337             ;it is. test it
  118. fini        jmp int0e             ;otherwise all done
  119.                                   
  120. za337       sty offset            ;.y=0 (offset into text portion of line)
  121. za339       inc offset            
  122.             ldy offset            ;offset into current line being tested
  123.             ldx strend            ;offset into buf for search string
  124.             lda strend+1          ;length of search string
  125.             sta sofs              
  126. za343       jsr pk                ;get from line
  127.             beq za31f             ;end of line. text not found
  128.             cmp $0200,x           ;same as search string?
  129.             bne za339             ;no. move up through source line
  130.             inx                   
  131.             iny                   
  132.             dec sofs              
  133.             bne za343             ;still more chars to check
  134.             dey                   ;found text. save offset to end+1
  135.             sty delim             
  136.             sty zb5               
  137.             ldx thslin            
  138.             lda thslin+1          
  139.             ldy #0                
  140.             jsr $5125             
  141.             jsr b15               
  142.             lda #13               
  143.             jsr chrout            
  144.             ldy zb5               
  145.             jmp za337             
  146.                                   
  147. ;-------------------------------------
  148. ; get search or replace text from buf
  149. ;-------------------------------------
  150.                                   
  151. gettxt      ldy txtptr            
  152.             iny                   
  153.             tya                   
  154.             sta strend            ;offset to start of text
  155.             lda #0                
  156.             sta strend+1          ;initialize length to zero
  157. gt0         lda $0200,y           
  158.             beq mbs               
  159.             cmp delim             
  160.             beq gtx               
  161.             inc strend+1          ;bump length
  162.             iny                   
  163.             bne gt0               
  164. gtx         sty txtptr            ;done
  165.             rts                   
  166.                                   
  167. mbs         sty txtptr            
  168.             pla                   
  169.             pla                   
  170.             jsr chrgot            
  171.             jmp ch1               
  172.                                   
  173. ;----------------
  174. ; get line range
  175. ;----------------
  176.                                   
  177. range       bcc r0                ;chrget got a number
  178.             beq r0                
  179.             cmp #"-"              
  180.             beq r0                
  181. snerr       ldx #11               
  182.             jmp (ierror)          
  183.                                   
  184. r0          jsr linget            ;get first line number
  185.             jsr fndlin            ;find where it is in text area (z61)
  186.             jsr chrgot            ;is it start-end?
  187.             beq r1                ;no. just start
  188.             cmp #"-"              
  189.             bne snerr             
  190.             jsr chrget            
  191.             jsr linget            
  192.             bne snerr             
  193. r1          lda poker             
  194.             ora poker+1           
  195.             bne r3                
  196.             lda #$ff              
  197.             sta poker             
  198.             sta poker+1           
  199. r3          jmp b15               
  200.                                   
  201. za431       jsr peek              
  202.             sta thslin            
  203.             jsr peek              
  204.             sta thslin+1          
  205.             sec                   
  206.             lda poker             
  207.             sbc thslin            
  208.             lda poker+1           
  209.             sbc thslin+1          
  210.             rts                   
  211.                                   
  212. ;----------------
  213. ; peek text area
  214. ;----------------
  215.                                   
  216. peek        inc z61               
  217.             bne pk0               
  218.             inc z61+1             
  219. pk0         ldy #0                
  220. pk          sta $ff01             
  221.             lda (z61),y           
  222. b15         pha                   
  223.             lda #$00              
  224.             sta $ff00             
  225.             pla                   
  226.             rts                   
  227.                                   
  228. strend      *=*+2                 ;offset,length for search & replace text
  229. thslin      *=*+2                 ;temp. store current line number
  230. offset      *=*+1                 ;current offset into line
  231. sofs        *=*+1                 ;copy of source string offset into buf
  232. delim       *=*+1                 
  233. zb5         *=*+1                 
  234.                                   
  235.             .end                  
  236.