home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / smallc / part3 / 8080 / crun.asm < prev    next >
Encoding:
Assembly Source File  |  1986-11-30  |  4.2 KB  |  353 lines

  1. ;
  2. ;*****************************************************
  3. ;                             *
  4. ;    runtime    library    for small C compiler         *
  5. ;                             *
  6. ;    c.s - runtime routine for basic C code         *
  7. ;                             *
  8. ;        Ron Cain                 *
  9. ;                             *
  10. ;*****************************************************
  11. ;
  12.     cseg
  13. ;
  14.     public    ?gchar,?gint,?pchar,?pint
  15.     public    ?sxt
  16.     public    ?or,?and,?xor
  17.     public    ?eq,?ne,?gt,?le,?ge,?lt,?uge,?ult,?ugt,?ule
  18.     public    ?asr,?asl
  19.     public    ?sub,?neg,?com,?lneg,?bool,?mul,?div
  20.     public    ?case,brkend,Xstktop
  21.     public    etext
  22.     public    edata
  23. ;
  24. ; fetch char from (HL) and sign extend into HL
  25. ?gchar:    mov    a,m
  26. ?sxt:    mov    l,a
  27.     rlc
  28.     sbb    a
  29.     mov    h,a
  30.     ret
  31. ; fetch int from (HL)
  32. ?gint:    mov    a,m
  33.     inx    h
  34.     mov    h,m
  35.     mov    l,a
  36.     ret
  37. ; store char from HL into (DE)
  38. ?pchar: mov    a,l
  39.     stax    d
  40.     ret
  41. ; store int from HL into (DE)
  42. ?pint:    mov    a,l
  43.     stax    d
  44.     inx    d
  45.     mov    a,h
  46.     stax    d
  47.     ret
  48. ; "or" HL and DE into HL
  49. ?or:    mov    a,l
  50.     ora    e
  51.     mov    l,a
  52.     mov    a,h
  53.     ora    d
  54.     mov    h,a
  55.     ret
  56. ; "xor" HL and DE into HL
  57. ?xor:    mov    a,l
  58.     xra    e
  59.     mov    l,a
  60.     mov    a,h
  61.     xra    d
  62.     mov    h,a
  63.     ret
  64. ; "and" HL and DE into HL
  65. ?and:    mov    a,l
  66.     ana    e
  67.     mov    l,a
  68.     mov    a,h
  69.     ana    d
  70.     mov    h,a
  71.     ret
  72. ;
  73. ;......logical operations: HL set to 0 (false) or 1 (true)
  74. ;
  75. ; DE == HL
  76. ?eq:    call    ?cmp
  77.     rz
  78.     dcx    h
  79.     ret
  80. ; DE != HL
  81. ?ne:    call    ?cmp
  82.     rnz
  83.     dcx    h
  84.     ret
  85. ; DE > HL [signed]
  86. ?gt:    xchg
  87.     call    ?cmp
  88.     rc
  89.     dcx    h
  90.     ret
  91. ; DE <= HL [signed]
  92. ?le:    call    ?cmp
  93.     rz
  94.     rc
  95.     dcx    h
  96.     ret
  97. ; DE >= HL [signed]
  98. ?ge:    call    ?cmp
  99.     rnc
  100.     dcx    h
  101.     ret
  102. ; DE < HL [signed]
  103. ?lt:    call    ?cmp
  104.     rc
  105.     dcx    h
  106.     ret
  107. ; DE >= HL [unsigned]
  108. ?uge:    call    ?ucmp
  109.     rnc
  110.     dcx    h
  111.     ret
  112. ; DE < HL [unsigned]
  113. ?ult:    call    ?ucmp
  114.     rc
  115.     dcx    h
  116.     ret
  117. ; DE > HL [unsigned]
  118. ?ugt:    xchg
  119.     call    ?ucmp
  120.     rc
  121.     dcx    h
  122.     ret
  123. ; DE <= HL [unsigned]
  124. ?ule:    call    ?ucmp
  125.     rz
  126.     rc
  127.     dcx    h
  128.     ret
  129. ; signed compare of DE and HL
  130. ;   carry is sign of difference [set => DE < HL]
  131. ;   zero is zero/non-zero
  132. ?cmp:    mov    a,e
  133.     sub    l
  134.     mov    e,a
  135.     mov    a,d
  136.     sbb    h
  137.     lxi    h,1        ;preset true
  138.     jm    ?cmp1
  139.     ora    e        ;resets carry
  140.     ret
  141. ?cmp1:    ora    e
  142.     stc
  143.     ret
  144. ; unsigned compare of DE and HL
  145. ;   carry is sign of difference [set => DE < HL]
  146. ;   zero is zero/non-zero
  147. ?ucmp:    mov    a,d
  148.     cmp    h
  149.     jnz    ?ucmp1
  150.     mov    a,e
  151.     cmp    l
  152. ?ucmp1:    lxi    h,1        ;preset true
  153.     ret
  154. ; shift DE right arithmetically by HL, move to HL
  155. ?asr:    xchg
  156. ?asr1:    dcr    e
  157.     rm
  158.     mov    a,h
  159.     ral
  160.     mov    a,h
  161.     rar
  162.     mov    h,a
  163.     mov    a,l
  164.     rar
  165.     mov    l,a
  166.     jmp    ?asr1
  167. ; shift DE left arithmetically by HL, move to HL
  168. ?asl:    xchg
  169. ?asl1:    dcr    e
  170.     rm
  171.     dad    h
  172.     jmp    ?asl1
  173. ; HL = DE - HL
  174. ?sub:    mov    a,e
  175.     sub    l
  176.     mov    l,a
  177.     mov    a,d
  178.     sbb    h
  179.     mov    h,a
  180.     ret
  181. ; HL = -HL
  182. ?neg:    call    ?com
  183.     inx    h
  184.     ret
  185. ; HL = ~HL
  186. ?com:    mov    a,h
  187.     cma
  188.     mov    h,a
  189.     mov    a,l
  190.     cma
  191.     mov    l,a
  192.     ret
  193. ; HL = !HL
  194. ?lneg:    mov    a,h
  195.     ora    l
  196.     jz    ?lneg1
  197.     lxi    h,0
  198.     ret
  199. ?lneg1:    inx    h
  200.     ret
  201. ; HL = !!HL
  202. ?bool:    call    ?lneg
  203.     jmp    ?lneg
  204. ;
  205. ; HL = DE * HL [signed]
  206. ?mul:    mov    b,h
  207.     mov    c,l
  208.     lxi    h,0
  209. ?mul1:    mov    a,c
  210.     rrc
  211.     jnc    ?mul2
  212.     dad    d
  213. ?mul2:    xra    a
  214.     mov    a,b
  215.     rar
  216.     mov     b,a
  217.     mov    a,c
  218.     rar
  219.     mov    c,a
  220.     ora    b
  221.     rz
  222.     xra    a
  223.     mov    a,e
  224.     ral
  225.     mov    e,a
  226.     mov    a,d
  227.     ral
  228.     mov    d,a
  229.     ora    e
  230.     rz
  231.     jmp    ?mul1
  232. ; HL = DE / HL, DE = DE % HL
  233. ?div:    mov    b,h
  234.     mov    c,l
  235.     mov    a,d
  236.     xra    b
  237.     push    psw
  238.     mov    a,d
  239.     ora    a
  240.     cm    ?deneg
  241.     mov    a,b
  242.     ora    a
  243.     cm    ?bcneg
  244.     mvi    a,16
  245.     push    psw
  246.     xchg
  247.     lxi    d,0
  248. ?div1:    dad    h
  249.     call    ?rdel
  250.     jz    ?div2
  251.     call    ?cmpbd
  252.     jm    ?div2
  253.     mov    a,l
  254.     ori    1
  255.     mov    l,a
  256.     mov    a,e
  257.     sub    c
  258.     mov    e,a
  259.     mov    a,d
  260.     sbb    b
  261.     mov    d,a
  262. ?div2:    pop    psw
  263.     dcr    a
  264.     jz    ?div3
  265.     push    psw
  266.     jmp    ?div1
  267. ?div3:    pop    psw
  268.     rp
  269.     call    ?deneg
  270.     xchg
  271.     call    ?deneg
  272.     xchg
  273.     ret
  274. ; {DE = -DE}
  275. ?deneg: mov    a,d
  276.     cma
  277.     mov    d,a
  278.     mov    a,e
  279.     cma
  280.     mov    e,a
  281.     inx    d
  282.     ret
  283. ; {BC = -BC}
  284. ?bcneg: mov    a,b
  285.     cma
  286.     mov    b,a
  287.     mov    a,c
  288.     cma
  289.     mov    c,a
  290.     inx    b
  291.     ret
  292. ; {DE <r<r 1}
  293. ?rdel:    mov    a,e
  294.     ral
  295.     mov    e,a
  296.     mov    a,d
  297.     ral
  298.     mov    d,a
  299.     ora    e
  300.     ret
  301. ; {BC : DE}
  302. ?cmpbd: mov    a,e
  303.     sub    c
  304.     mov    a,d
  305.     sbb    b
  306.     ret
  307. ; case jump
  308. ?case:    xchg            ;switch value to DE
  309.     pop    h        ;get table address
  310. ?case1:    call    ?case4        ;get case value
  311.     mov    a,e
  312.     cmp    c        ;equal to switch value ?
  313.     jnz    ?case2        ;no
  314.     mov    a,d
  315.     cmp    b        ;equal to switch value ?
  316.     jnz    ?case2        ;no
  317.     call    ?case4        ;get case label
  318.     jz    ?case3        ;end of table, go to default
  319.     push    b
  320.     ret            ;case jump
  321. ?case2:    call    ?case4        ;get case label
  322.     jnz    ?case1        ;next case
  323. ?case3:    dcx    h
  324.     dcx    h
  325.     dcx    h
  326.     mov    d,m
  327.     dcx    h
  328.     mov    e,m
  329.     xchg
  330.     pchl            ;default jump
  331. ?case4:    mov    c,m
  332.     inx    h
  333.     mov    b,m
  334.     inx    h
  335.     mov    a,c
  336.     ora    b
  337.     ret
  338. ;
  339. ;
  340. ;
  341. Xstktop:    lxi    h,0    ;return current stack pointer (for sbrk)
  342.     dad    sp
  343.     ret
  344.     cseg
  345. etext:
  346.     dseg
  347. brkend:    dw    edata        ;current "break"
  348. edata:
  349. ;
  350. ;
  351. ;
  352.     end
  353.