Advertisement
AnthonyCagliano

Untitled

Nov 29th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. ; bigint_add(uint8_t *op1, uint8_t *op2);
  2. ; hard limit to 32 bytes
  3. ; output in op1
  4. _bigint_add:
  5. ti._frameset0
  6. ld hl, (ix + 3) ; op2
  7. ld de, (ix + 6) ; op1
  8. ld b, 32
  9. .loop:
  10. ld a, (de)
  11. add a, (hl)
  12. ld (de), a
  13. jr nc, .skip_carry
  14. ld a, b
  15. or a
  16. jr z, .skip_carry
  17. ;if carry and b!=0, inc (de+1)
  18. inc de
  19. ex de, hl
  20. inc (hl)
  21. ex de, hl
  22. .skip_carry
  23. inc hl
  24. djnz .loop
  25. ld sp, ix
  26. pop ix
  27. ret
  28.  
  29.  
  30. ; bigint_mul(uint8_t *op1, uint8_t *op2)
  31. _bigint_mul:
  32. ld hl, 32
  33. ti._frameset
  34. ld de, ix - 32 ; stack mem?
  35. ld hl, (ix + 6) ; op1 (save a copy)
  36. ld bc, 32
  37. ldir
  38. ld de, (ix + 6) ; op1
  39. ld hl, (ix + 3) ; op2
  40. ld b, 32
  41. .byteloop:
  42. push bc
  43. ld b, 8
  44. .bitloop:
  45. ex de, hl
  46. push bc
  47. ld bc, 32
  48. call bigint_rshift1 ; double
  49. pop bc
  50. ex de, hl
  51. ld a, (hl)
  52. rra
  53. jr nc, .bitloop
  54. push hl ; save current hl
  55. lea hl, ix - 32
  56. push hl
  57. ld hl, (ix + 6)
  58. push hl
  59. call bigint_add ; then add
  60. pop hl,hl,hl
  61. djnz .bitloop
  62. pop bc
  63. djnz .byteloop
  64. ld sp, ix
  65. pop ix
  66. ret
  67.  
  68.  
  69. ; bigint_sub(uint8_t *op1, uint8_t *op2);
  70. _bigint_sub:
  71. ti._frameset0
  72. ld hl, (ix + 6) ; op1
  73. ld bc, 32
  74. add hl, bc
  75. ex de, hl
  76. ld hl, (ix + 3) ; op2
  77. add hl, bc
  78. ld b, c
  79. ; hl = op2 + 32, de = op1 + 32
  80. .loop:
  81. ld a, (de)
  82. sub a, (hl)
  83. ld (de), a
  84. ; handle carry flag set
  85. djnz .loop
  86. ld sp, ix
  87. pop ix
  88. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement