home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / EXTRA-ST / CPM-80-E / CPM-0.2 / CPM-0 / cpm-0.2 / z80-sources / mylib / scanf.mac < prev    next >
Encoding:
Text File  |  1994-06-06  |  2.8 KB  |  132 lines

  1. ;************************************************************************
  2. ;*        formatiere Strings                    *
  3. ;*        iy ist Adresse der Ausgaberoutine            *
  4. ;*        alle Register werden gerettet                *
  5. ;************************************************************************
  6.  
  7.         .z80            ;
  8.         entry _scanf        ;
  9. cr        equ 13            ;
  10. lf        equ 10            ;
  11.  
  12. _scanf:        ld b,0            ; args for now...
  13. scanf_main:    ld a,(hl)        ; Zeichen
  14.         or a            ; 0 = Ende!
  15.         jr z,exit_scan        ;
  16.         inc hl            ; point to next char
  17.         cp '\'            ; DataLinkEscape?
  18.         jr z,special_char    ;
  19.         cp '%'            ; insert-Byte?
  20.         jr z,insrt_number    ;
  21.         cp '^'            ; ctrl-char?
  22.         jr z,ctrl_char        ;
  23. cmp_this:    ld c,a            ; save akku
  24.         call _iy        ; get char
  25.         cp c            ; equal?
  26.         jr z,scanf_main        ;
  27. ret_bad:    scf            ; not ok
  28. exit_scan:    ret nc            ; if everything in order...
  29. exit_bad:    inc b            ;
  30.         dec b            ;
  31.         ret z            ; no args (yet)
  32.         pop hl            ;
  33. flush:        pop de            ; all bad args down!
  34.         djnz flush        ;
  35.         jp (hl)            ; return this way...
  36.  
  37.  
  38. ctrl_char:    ld a,(hl)        ;
  39.         sub '@'            ;
  40.         cp ' '            ; must be less...
  41.         jr nc,scanf_main    ; then try again
  42.         inc hl            ;
  43.         jr cmp_this        ;
  44.  
  45. special_char:    ld a,(hl)        ;
  46.         cp ' '            ; must be greater
  47.         jr c,scanf_main        ;
  48.         inc hl            ;
  49.         cp 'n'            ; for cr,lf
  50.         jr nz,cmp_this        ;
  51.         call _iy        ;
  52.         cp cr            ; must follow cr,lf
  53.         jr nz,ret_bad        ;
  54.         ld a,lf            ;
  55.         jr cmp_this        ;
  56.  
  57. insrt_number:    ld ix,tab_base        ;
  58.         call _parms##        ; get base & other spec. values
  59.                     ; returns carry, if error
  60.         jr c,scanf_main        ; format error, ignore
  61.  
  62.         jp p,calc_base        ;
  63.         cp 0feh            ;
  64.         jr nz,scanf_main    ;
  65.  
  66. prchar:        call _iy        ; get character
  67.         ld c,0            ;
  68. set_a:        inc hl            ;
  69.         inc b            ;
  70.         pop de            ;
  71.         push hl            ;
  72.         ld l,a            ;
  73.         ld h,c            ;
  74.         ex (sp),hl        ;
  75.         push de            ; arg. on stack
  76.         jp scanf_main        ; auf zu neuen Taten...
  77.  
  78. calc_base:    ld (base),a        ;
  79.         push hl            ;
  80.         push bc            ;
  81.         ld a,(n_digit)        ;
  82.         ld c,a            ;
  83.         ld hl,0            ;
  84. get_num:    ld a,0            ;
  85. base        equ $-1            ;
  86.         call _multa##        ;
  87.         call _iy        ;
  88.         ld b,a            ;
  89.         ld a,(leading)        ;
  90.         sub b            ;
  91.         jr z,below_10        ; akku = 0, all OK
  92.         ld a,'0'        ; disable leading spaces
  93.         ld (leading),a        ;
  94.         ld a,b            ;
  95.         sub '0'            ;
  96.         cp 10            ;
  97.         jr c,below_10        ;
  98.         or ' '            ; tolower
  99.         sub ' '+7        ;
  100. below_10:    ld b,a            ;
  101.         ld a,(base)        ;
  102.         dec a            ;
  103.         cp b            ;
  104.         jr c,ret_bad2        ; bad digit
  105.         ld a,b            ;
  106.         call _adda##        ;
  107.         jr c,ret_bad2        ; overflow
  108.         dec c            ;
  109.         jr nz,get_num        ;
  110.         ld a,(mask_h)        ;
  111.         cp h            ; over 00ffh?
  112.         jr c,ret_bad2        ;
  113.         pop bc            ;
  114.         ld c,h            ;
  115.         ld a,l            ;
  116.         pop hl            ;
  117.         jr set_a        ;
  118. ret_bad2:    pop bc            ;
  119.         pop hl            ;
  120.         jp exit_bad        ; Carry is set!
  121.  
  122. _iy:        jp (iy)            ;
  123.  
  124.         dseg            ;
  125. mask_h:        db 0            ;
  126. n_digit:    db 0ffh            ;
  127. leading:    db ' '            ;
  128. tab_base:                ;
  129.         cseg            ;
  130.         end            ;
  131.  
  132.