cmu.s 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. .define .cmu
  2. .sect .text
  3. .sect .rom
  4. .sect .data
  5. .sect .bss
  6. .sect .text
  7. ! parameters:
  8. ! hl : size (#bytes)
  9. ! stack: second operand
  10. ! first operand
  11. ! stack: result (out)
  12. .cmu:
  13. ! The two operands are compared byte by byte,
  14. ! starting at the highest byte, until
  15. ! they differ.
  16. pop ix ! return address
  17. pop hl ! #bytes
  18. ld b,h ! bc := hl
  19. ld c,l
  20. add hl,sp
  21. dec hl ! pointer to highest byte
  22. ! of second operand
  23. ld d,h ! de := hl
  24. ld e,l
  25. add hl,bc ! pointer to highest byte
  26. ! of first operand
  27. ld sp,hl ! points to where the
  28. ! result will be stored
  29. ex de,hl
  30. ! now, de points to highest byte of 1st operand
  31. ! sp ,, ,, ,,
  32. ! hl ,, ,, 2nd ,,
  33. ! bc contains #bytes
  34. 0:
  35. ! loop, compare the two operands
  36. ! byte by byte.
  37. ld a,(de)
  38. xor (hl) ! Avoid overflow during
  39. ! subtraction. If the
  40. ! signbits differ, then
  41. ! the operands differ.
  42. jp m,2f ! signbits differ
  43. ld a,(de) ! signbits are equal,
  44. ! so we can savely
  45. ! compare the bytes.
  46. sub (hl)
  47. jr nz,1f ! operands are different
  48. dec de ! the two bytes are the
  49. ! same, try next bytes,
  50. ! if any.
  51. dec hl ! bump pointers
  52. dec bc
  53. ld a,b ! bc = 0 ?
  54. or c
  55. jr nz,0b ! no, try next bytes
  56. ! yes, then the two operands are equal.
  57. ! Note that a=0 now.
  58. 1:
  59. ld h,a ! hl := result
  60. ld l,a
  61. jr 3f
  62. 2:
  63. ! the signbits differ
  64. ld h,(hl) ! hl := positive if
  65. ! signbit of current
  66. ! byte of 2nd operand
  67. ! is "0", else negative
  68. ld l,1 ! just in case (hl)=0
  69. 3:
  70. ex (sp),hl ! sp was set above
  71. jp (ix) ! return