Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;This code is as yet UNTESTED.
- ;Feel free to experiment.
- ;This is the algorithm I personally use when doing arithmetic by hand of numbers with more than ~6 digits.
- ;It's a modified 'schoolbook' method.
- ;First create a table of values of one of the multiplicands multiplied by 1,2,3,...,9
- ;Now use this table to quickly look up partial products.
- BCD_mul:
- ;Input:
- ; HL points to the first multiplicand (big endian, 14-digit integer)
- ; DE points to the second multiplicand (big endian, 14-digit integer)
- ; 'scrap' starts at an address ending in 00, and at least 94 bytes long.
- ;Output:
- ;
- push de
- ld de,scrap
- xor a
- ld (de),a
- inc de
- call mov7
- pop hl
- ld de,scrap+72
- ld (de),a
- inc de
- call mov7
- ld h,a
- ld l,a
- ld (scrap+80),hl
- ld (scrap+82),hl
- ld (scrap+84),hl
- ld (scrap+86),hl
- ld (scrap+88),hl
- ld (scrap+90),hl
- ld (scrap+92),hl
- call mullutgen
- ld bc,scrap+79
- ld de,srap+93
- call bcdmul_sub_0
- xor a
- ld hl,scrap+71
- ld b,72
- rrd
- dec hl
- djnz $-3
- ld bc,scrap+79
- ld de,srap+93
- call +_
- _:
- call +_
- _:
- call +_
- _:
- ld a,(bc)
- inc bc
- and 15
- ret z
- rra
- and %01111000
- jp bcdmul_sub_1
- bcdmul_sub_0:
- call +_
- _:
- call +_
- _:
- call +_
- _:
- ld a,(bc)
- inc bc
- and 15
- ret z
- add a,a
- add a,a
- add a,a
- bcdmul_sub_1:
- sub 73
- cpl
- ld h,b
- ld l,a
- ld a,(de) \ add a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a \ dec de \ dec hl
- ld a,(de) \ adc a,(hl) \ ld (de),a
- ld hl,-6
- add hl,de
- ex de,hl
- ret
- mov7:
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ldi
- ret
- mullutgen:
- ld hl,scrap+71
- ld bc,scrap+63
- call mullutgen
- call +_
- _:
- call +_
- _:
- call mullutgen
- mullutgen_sub:
- ld de,scrap+71
- or a
- call +_
- _:
- call +_
- _:
- call +_
- _:
- ld a,(de)
- adc a,(hl)
- daa
- ld (bc),a
- dec hl
- dec de
- dec bc
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement