mul16.asm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. __MUL16: ; Mutiplies HL with the last value stored into de stack
  2. ; Works for both signed and unsigned
  3. ;- PROC
  4. ;- LOCAL __MUL16LOOP
  5. ;- LOCAL __MUL16NOADD
  6. lda z80_e ;- ex de,hl
  7. ldx z80_l
  8. stx z80_e
  9. sta z80_l
  10. lda z80_d
  11. ldx z80_h
  12. stx z80_d
  13. sta z80_h
  14. pla ;- pop hl ; Return address
  15. sta z80_h
  16. pla
  17. sta z80_l
  18. tsx ;- ex (sp),hl ; CALLEE caller convention
  19. lda $0103,x
  20. ldy z80_h
  21. sta z80_h
  22. tya
  23. sta $0103,x
  24. lda $0104,x
  25. ldy z80_l
  26. sta z80_l
  27. tya
  28. sta $104,x
  29. ;;__MUL16_FAST: ; __FASTCALL ENTRY: HL = 1st operand, DE = 2nd Operand
  30. ;; ld c, h
  31. ;; ld a, l ; C,A => 1st Operand
  32. ;;
  33. ;; ld hl, 0 ; Accumulator
  34. ;; ld b, 16
  35. ;;
  36. ;;__MUL16LOOP:
  37. ;; sra c ; C,A >> 1 (Arithmetic)
  38. ;; rra
  39. ;;
  40. ;; jr nc, __MUL16NOADD
  41. ;; add hl, de
  42. ;;
  43. ;;__MUL16NOADD:
  44. ;; sla e
  45. ;; rl d
  46. ;;
  47. ;; djnz __MUL16LOOP
  48. __MUL16_FAST:
  49. lda %16 ;- ld b,16
  50. sta z80_b
  51. lda z80_d ;- ld a,d
  52. sta z80_a
  53. lda z80_e ;- ld c,e
  54. sta z80_c
  55. lda z80_e ;- ex de,hl
  56. ldx z80_l
  57. stx z80_e
  58. sta z80_l
  59. lda z80_d
  60. ldx z80_h
  61. stx z80_d
  62. sta z80_h
  63. lda #<0 ;- ld hl,0
  64. sta z80_l
  65. lda #>0
  66. sta z80_h
  67. __MUL16LOOP:
  68. asl z80_l ;- add hl,hl ; hl << 1
  69. rol z80_h
  70. ;- sla c
  71. ;- rla ; a,c << 1
  72. jcs __MUL16NOADD ;- jp nc, __MUL16NOADD
  73. clc ;- add hl,de
  74. lda z80_l
  75. adc z80_e
  76. sta z80_l
  77. lda z80_h
  78. adc z80_d
  79. sta z80_h
  80. __MUL16NOADD:
  81. ;- djnz __MUL16LOOP
  82. rts ;- ret ; Result in hl (16 lower bits)
  83. ;- ENDP