cii.s 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. .define .cii
  2. .define .cuu
  3. .sect .text
  4. .sect .rom
  5. .sect .data
  6. .sect .bss
  7. .sect .text
  8. ! cii: convert integer to integer
  9. ! parameters:
  10. ! stack: destination size
  11. ! source size
  12. ! source
  13. ! stack: result (out)
  14. ! This code is also used by cuu.
  15. ! The contents of the a-register determines
  16. ! if we're doing a cii (a=0) or a cuu (a=1),
  17. ! so be very careful with this register!
  18. .cii:
  19. pop ix ! return address
  20. pop hl ! destination size
  21. pop de ! source size
  22. ld b,h ! bc := destination size
  23. ld c,l
  24. xor a ! watch it, this is dirty!
  25. ! Besides clearing the carry
  26. ! this instruction sets a-reg.
  27. ! to 0, to indicate this is
  28. ! a cii and not a cuu.
  29. sbc hl,de ! hl := destination size
  30. ! - source size
  31. jr z,1f ! equal, return
  32. jp p,2f ! larger, expand
  33. ! smaller, shrink
  34. ! The most significant part of the source
  35. ! is removed. As the least sign. part is
  36. ! on top of the stack, we have to move an
  37. ! entire data block.
  38. 9:
  39. add hl,sp ! note that hl < 0
  40. ! (also come here via cuu)
  41. add hl,de
  42. dec hl ! now hl points to most
  43. ! significant byte of what
  44. ! will be left over of source
  45. ex de,hl
  46. add hl,sp
  47. ex de,hl
  48. dec de ! now de points to highest
  49. ! byte of source
  50. lddr ! move 'destination size'
  51. ! bytes upwards (i.e. away
  52. ! from top of stack)
  53. inc de
  54. ex de,hl
  55. ld sp,hl ! adjust stackpointer
  56. 1:
  57. jp (ix) ! return
  58. 2:
  59. ! larger, expand
  60. ! A number of bytes (containing the signbits
  61. ! of the source) is inserted before the most
  62. ! significant byte of the source.
  63. ! As this byte is somewhere in the middle of
  64. ! the stack, the entire source must first be
  65. ! moved downwards (in the direction of the
  66. ! top)
  67. 8:
  68. ld b,d ! bc := source size
  69. ! (also come here via cuu)
  70. ld c,e
  71. ex de,hl ! de := difference (> 0)
  72. ld hl,0
  73. add hl,sp ! hl := sp
  74. dec de ! if difference = 1, don't adjust stack pointer
  75. jr nz, 4f
  76. inc de
  77. jr 5f
  78. 4:
  79. inc de
  80. push hl
  81. or a
  82. sbc hl,de
  83. ex de,hl ! de := sp - difference
  84. pop hl ! hl := sp
  85. ex de,hl ! adjust sp
  86. ld sp,hl
  87. ex de,hl
  88. ldir ! move source upwards,
  89. ! creating a 'hole'
  90. ! inside the stack
  91. ! now we will fill the hole with bytes
  92. ! containing either 0 or -1, depending
  93. ! on the signbit of the source.
  94. or a
  95. sbc hl,de
  96. ex de,hl ! de := difference
  97. dec hl ! now hl points to
  98. ! most significant byte
  99. ! of the source
  100. 5:
  101. or a ! see if we're doing
  102. ! a 'cii' or a 'cuu'
  103. jr nz,3f ! cuu, expand with zeroes
  104. bit 7,(hl) ! test signbit
  105. jr z,3f
  106. dec b ! b := -1 (was 0 after ldir)
  107. 3:
  108. inc hl
  109. ld (hl),b ! either 0 or -1
  110. dec de
  111. ld a,d
  112. or e
  113. jr nz,3b
  114. jp (ix) ! return
  115. ! cuu: convert unsigned to unsigned
  116. ! parameters:
  117. ! stack: destination size
  118. ! source size
  119. ! source
  120. ! stack: result (out)
  121. ! The only difference between a cuu and a cii is:
  122. ! if the destination is larger than the source,
  123. ! the former extends with zeroes and the latter
  124. ! extends with sign bits
  125. ! cuu uses the code of cii. In this case it puts
  126. ! a '1' in the accumulator to indicate this is
  127. ! a cuu.
  128. .cuu:
  129. pop ix
  130. pop hl
  131. pop de
  132. ld b,h
  133. ld c,l
  134. xor a ! clear carry
  135. sbc hl,de
  136. jr z,1b ! equal, return
  137. jp m,9b ! smaller, shrink
  138. inc a ! a := 1
  139. jr 8b ! larger, expand