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 / printf.mac < prev    next >
Encoding:
Text File  |  1994-06-06  |  3.9 KB  |  197 lines

  1. ;************************************************************************
  2. ;*                                    *
  3. ;*        formatiere Strings                    *
  4. ;*        IY ist Adresse der Ausgaberoutine            *
  5. ;*        alle Register werden gerettet                *
  6. ;*        B holds number of used arguments            *
  7. ;*                                    *
  8. ;************************************************************************
  9.  
  10.         .z80            ;
  11.         entry _printf        ;
  12.  
  13. cr        equ 13            ;
  14. lf        equ 10            ;
  15.  
  16. getarg        macro            ;
  17.           inc hl        ;
  18.           inc b            ;
  19.           ex (sp),hl        ;
  20.           pop de        ;
  21.           ex (sp),hl        ;
  22.           push de        ;
  23.         endm            ;
  24.  
  25. _printf:    ld b,0            ;
  26. printf_main:    ld a,(hl)        ; Zeichen
  27.         or a            ; 0 = Ende!
  28.         ret z            ;
  29.         inc hl            ; point to next char
  30.         cp '\'            ; DataLinkEscape?
  31.         jr z,special_char    ;
  32.         cp '%'            ; insert-Byte?
  33.         jr z,insrt_number    ;
  34.         cp '^'            ; ctrl-char?
  35.         jr z,ctrl_char        ;
  36. out_this:    call _iy        ; output Akku
  37.         jr printf_main        ;
  38.  
  39. ctrl_char:    ld a,(hl)        ;
  40.         sub '@'            ;
  41.         cp ' '            ; must be less...
  42.         jr nc,printf_main    ; then try again
  43.         inc hl            ;
  44.         jr out_this        ;
  45.  
  46. special_char:    ld a,(hl)        ;
  47.         cp ' '            ; must be greater
  48.         jr c,printf_main    ;
  49.         inc hl            ;
  50.         cp 'n'            ; for cr,lf
  51.         jr nz,out_this        ;
  52.         ld a,cr            ;
  53.         call _iy        ;
  54.         ld a,lf            ;
  55.         jr out_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,printf_main    ; printout: ignore error
  61.  
  62.         jp p,calc_base        ;
  63.         cp 0ffh            ; 's' = string
  64.         jr z,prstring        ;
  65.         cp 0feh            ;
  66.         jr nz,printf_main    ;
  67.  
  68. prchar:        getarg            ;
  69.         ld a,(n_digit)        ;
  70.         ld c,1            ;
  71.         cp 0ffh            ;
  72.         jr z,many_c        ;
  73.         ld c,a            ;
  74. many_c:        ld a,l            ;
  75.         call _iy        ;
  76.         dec c            ;
  77.         jr nz,many_c        ;
  78. ende_str:    pop hl            ;
  79.         jp printf_main        ;
  80.  
  81. prstring:    getarg            ;
  82. loop_str:    ld a,(n_digit)        ;
  83.         cp 0ffh            ;
  84.         jr nz,n_str        ;
  85. long_str:    ld a,(hl)        ;
  86.         or a            ;
  87.         jr z,ende_str        ;
  88.         call _iy        ;
  89.         inc hl            ;
  90.         jr long_str        ;
  91.  
  92. n_str:        ld c,a            ;
  93. out_str:    ld a,(hl)        ;
  94.         or a            ;
  95.         jr z,n_ende        ;
  96.         call _iy        ;
  97.         inc hl            ;
  98.         dec c            ;
  99.         jr nz,out_str        ;
  100.         jr ende_str        ;
  101. n_ende:        ld l,' '        ;
  102.         jr many_c        ;
  103.  
  104. calc_base:    push hl            ;
  105.         ld c,1            ;
  106.         ld l,a            ;
  107.         ld h,0            ;
  108. calc_loop:    ld (ix+0),l        ;
  109.         inc ix            ;
  110.         ld (ix+0),h        ;
  111.         inc ix            ;
  112.         inc c            ;
  113.         push bc            ;
  114.         ld c,a            ;
  115.         call _multa##        ;
  116.         ld a,c            ;
  117.         pop bc            ;
  118.         jr nc,calc_loop        ;
  119.         pop hl            ;
  120.         ld a,c            ;
  121.         ld (noch_digit),a    ;
  122.  
  123.         getarg            ;
  124.  
  125.         ld a,(mask_h)        ;
  126.         and h            ; do not use top Byte
  127.         ld h,a            ;
  128.         push bc            ; Unterprogramm, druckt HL dezimal aus
  129.         ld a,(noch_digit)    ; Anzahl der Stellen
  130.         dec a            ;
  131.         ld b,a            ;
  132. one_digit:    push de            ;
  133.         dec ix            ;
  134.         ld d,(ix+0)        ;
  135.         dec ix            ;
  136.         ld e,(ix+0)        ;
  137.         ld a,-1            ;
  138. einen_mehr:    or a            ;
  139.         sbc hl,de        ;
  140.         inc a            ;
  141.         jr nc,einen_mehr    ;
  142.         add hl,de        ; letzten ung}ltig machen
  143.         pop de            ;
  144.         call nib_out        ; Ausgabe des Zeichens
  145.         djnz one_digit        ; n{chste Stelle ausrechnen
  146.         pop bc            ;
  147.         ld a,l            ; Einer
  148.         call nib_out        ;
  149.         pop hl            ;
  150.         jp printf_main        ;
  151.  
  152. nib_out:    cp 10            ;
  153.         jr c,digit        ;
  154.         add a,' '+7        ; Char offset
  155. digit:        add a,'0'        ;
  156.         ld c,a            ;
  157.         ld a,(noch_digit)    ;
  158.         dec a            ;
  159.         ld (noch_digit),a    ;
  160.         ld a,c            ;
  161.         jr z,_iy        ; letzte Ziffer immer ganz raus
  162.         ld a,(n_digit)        ;
  163.         cp 0ffh            ;
  164.         jr nz,no_long        ;
  165.         ld a,c            ;
  166.         cp '0'            ; keine L{ngenangabe, unterschlage f}hrende Nullen
  167.         ret z            ;
  168.         call _iy        ;
  169.         ld a,(noch_digit)    ;
  170.         ld (n_digit),a        ;
  171.         ld a,'0'        ;
  172.         ld (leading),a        ; sind n{mlich nicht mehr leading...
  173.         ret            ;
  174. no_long:    dec a            ;
  175.         sub 0            ;
  176. noch_digit    equ $-1            ;
  177.         ret c            ; wenn zu lang...
  178.         ld a,c            ;
  179.         cp '0'            ;
  180.         jr nz,no_zero        ;
  181.         ld a,(leading)        ;
  182.         jr _iy            ;
  183. no_zero:    ld a,'0'        ;
  184.         ld (leading),a        ;
  185.         ld a,c            ;
  186. _iy:        jp (iy)            ;
  187.  
  188.         dseg            ;
  189. mask_h:        db 0            ;
  190. n_digit:    db 0ffh            ;
  191. leading:    db ' '            ;
  192. tab_base:    dw 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15;f}r base 2 reicht es
  193.         cseg            ;
  194.  
  195.         end            ;
  196.  
  197.