cii.s 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. .define .cii
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! Convert integer to integer
  8. ! Expects in a-reg: 1 for signed integer to signed integer (cii)
  9. ! 0 for unsigned integer to unsigned integer (cuu)
  10. ! Expects on stack: destination size
  11. ! source size
  12. ! source
  13. ! Yields on stack: result
  14. .cii: pop h
  15. shld .retadr
  16. mov h,b
  17. mov l,c
  18. shld .bcreg
  19. sta .areg ! save a-register
  20. pop b
  21. mov e,c
  22. pop b ! c = source size
  23. mov b,e ! b = destination size
  24. mov a,b
  25. cmp c
  26. jz 3f ! destination size = source size
  27. jc shrink ! destination size < source size
  28. ! if destination size > source size only:
  29. lxi h,0
  30. dad sp
  31. mov e,l
  32. mov d,h ! de = stackpointer
  33. mov a,b
  34. sub c ! c = (still) source size
  35. mov b,a ! b = destination size - source size
  36. cma
  37. mov l,a
  38. mvi h,255
  39. inx h
  40. dad d ! hl = stackpointer - (dest. size - source size)
  41. sphl ! new stackpointer
  42. 1: ldax d ! move source downwards
  43. mov m,a
  44. inx d
  45. inx h
  46. dcr c
  47. jnz 1b
  48. ral ! a-reg still contains most significant byte of source
  49. jnc 1f ! jump if is a positive integer
  50. lda .areg
  51. ora a
  52. jz 1f ! jump if it is a cuu
  53. mvi c,255 ! c-reg contains filler byte
  54. 1: mov m,c ! fill
  55. inx h
  56. dcr b
  57. jnz 1b
  58. jmp 3f ! done
  59. !if destination size < source size only:
  60. shrink: mov l,c ! load source size in hl
  61. mvi h,0
  62. dad sp
  63. mov d,h
  64. mov e,l ! de points just above source
  65. mov l,b ! load destination size in hl
  66. mvi h,0
  67. dad sp ! hl points just above "destination"
  68. 1: dcx d ! move upwards
  69. dcx h
  70. mov a,m
  71. stax d
  72. dcr b
  73. jnz 1b
  74. sphl
  75. 3: lhld .bcreg
  76. mov b,h
  77. mov c,l
  78. lhld .retadr
  79. pchl