Advertisement
Zeda

BCD_mul

Jul 5th, 2017
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;This code is as yet UNTESTED.
  2. ;Feel free to experiment.
  3. ;This is the algorithm I personally use when doing arithmetic by hand of numbers with more than ~6 digits.
  4. ;It's a modified 'schoolbook' method.
  5. ;First create a table of values of one of the multiplicands multiplied by 1,2,3,...,9
  6. ;Now use this table to quickly look up partial products.
  7.  
  8. BCD_mul:
  9. ;Input:
  10. ;  HL points to the first multiplicand (big endian, 14-digit integer)
  11. ;  DE points to the second multiplicand (big endian, 14-digit integer)
  12. ;  'scrap' starts at an address ending in 00, and at least 94 bytes long.
  13. ;Output:
  14. ;  
  15.     push de
  16.     ld de,scrap
  17.     xor a
  18.     ld (de),a
  19.     inc de
  20.     call mov7
  21.     pop hl
  22.     ld de,scrap+72
  23.     ld (de),a
  24.     inc de
  25.     call mov7
  26.     ld h,a
  27.     ld l,a
  28.     ld (scrap+80),hl
  29.     ld (scrap+82),hl
  30.     ld (scrap+84),hl
  31.     ld (scrap+86),hl
  32.     ld (scrap+88),hl
  33.     ld (scrap+90),hl
  34.     ld (scrap+92),hl
  35.     call mullutgen
  36.  
  37.     ld bc,scrap+79
  38.     ld de,srap+93
  39.     call bcdmul_sub_0
  40.     xor a
  41.     ld hl,scrap+71
  42.     ld b,72
  43.     rrd
  44.     dec hl
  45.     djnz $-3
  46.     ld bc,scrap+79
  47.     ld de,srap+93
  48.     call +_
  49. _:
  50.     call +_
  51. _:
  52.     call +_
  53. _:
  54.     ld a,(bc)
  55.     inc bc
  56.     and 15
  57.     ret z
  58.     rra
  59.     and %01111000
  60.     jp bcdmul_sub_1
  61. bcdmul_sub_0:
  62.     call +_
  63. _:
  64.     call +_
  65. _:
  66.     call +_
  67. _:
  68.     ld a,(bc)
  69.     inc bc
  70.     and 15
  71.     ret z
  72.     add a,a
  73.     add a,a
  74.     add a,a
  75. bcdmul_sub_1:
  76. sub 73
  77.     cpl
  78.     ld h,b
  79.     ld l,a
  80.     ld a,(de) \ add a,(hl) \ ld (de),a \ dec de \ dec hl
  81.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  82.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  83.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  84.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  85.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  86.     ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
  87.     ld a,(de) \ adc a,(hl) \ ld (de),a
  88.     ld hl,-6
  89.     add hl,de
  90.     ex de,hl
  91.     ret
  92. mov7:
  93.     ldi
  94.     ldi
  95.     ldi
  96.     ldi
  97.     ldi
  98.     ldi
  99.     ldi
  100.     ret
  101. mullutgen:
  102.     ld hl,scrap+71
  103.     ld bc,scrap+63
  104.     call mullutgen
  105.     call +_
  106. _:
  107.     call +_
  108. _:
  109.     call mullutgen
  110. mullutgen_sub:
  111.     ld de,scrap+71
  112.     or a
  113.     call +_
  114. _:
  115.     call +_
  116. _:
  117.     call +_
  118. _:
  119.     ld a,(de)
  120.     adc a,(hl)
  121.     daa
  122.     ld (bc),a
  123.     dec hl
  124.     dec de
  125.     dec bc
  126.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement