twh/f2 Posted February 2, 2017 Share Posted February 2, 2017 (edited) Hi ! I have here a 32bit multiplication routine which is simply adapted from this code from the Codebase64. http://codebase64.org/doku.php?id=base:16bit_multiplication_32-bit_product The following code was adapted by FJC. Num1 .ds 4 Num2 .ds 4 Num3 .ds 8 .proc MulLong ; 32 bit multiplication: Num1 by Num2, result in Num3 lda #0 sta Num3+4 sta Num3+5 sta Num3+6 sta Num3+7 ldx #32 ; loop for each bit multloop lsr Num1+3 ; divide multiplier by 2 lsr Num1+2 lsr Num1+1 ror Num1 bcc rotate lda Num3+4 ; get upper half of product and add multiplicand clc adc Num2 sta Num3+4 lda Num3+5 adc Num2+1 sta Num3+5 lda Num3+6 adc Num2+2 sta Num3+6 lda Num3+7 adc Num2+3 rotate ror ; rotate partial product sta Num3+7 ror Num3+6 ror Num3+5 ror Num3+4 ror Num3+3 ror Num3+2 ror Num3+1 ror Num3 dex bne multloop lda Num3 ldx Num3+1 rts .endp But I'm afraid it has a bug which I can't really fix. The following shows the dilemma: $4 * $16b62 = $5ad88 mda #$16b62 Num1 mda #$4 Num2 jsr MulLong mda Num3 Temp ; save result jmp * It's producing: "$1AD88" rather than "$5AD88". However this one works: $16b62 * $4 = $5ad88 mda #$4 Num1 mda #$16b62 Num2 jsr MulLong mda Num3 Temp ; save result FJC: It seems that some upper bits of multiplier/multiplicand aren't handled properly (so result is different depending on which has set or cleared upper bits). Any ideas?! Any help is appreciated!! greetings, \thomas Edited February 2, 2017 by twh/f2 1 Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted February 2, 2017 Share Posted February 2, 2017 Found it: lsr Num1+3 ; divide multiplier by 2 lsr Num1+2 lsr Num1+1 ror Num1 Should surely be: lsr Num1+3 ; divide multiplier by 2 ror Num1+2 ror Num1+1 ror Num1 3 Quote Link to comment Share on other sites More sharing options...
twh/f2 Posted February 2, 2017 Author Share Posted February 2, 2017 i'm not home now... but let's see if this works "in the field" Thank you Jon!!! Quote Link to comment Share on other sites More sharing options...
twh/f2 Posted February 2, 2017 Author Share Posted February 2, 2017 @home now and yep. it really solves the problem!!! it's a perfectly logical bug fix.. afterwards. I expected something more fundamental to be the problem :-) 1 Quote Link to comment Share on other sites More sharing options...
flashjazzcat Posted February 2, 2017 Share Posted February 2, 2017 I guess I dumbly invented this bug when hurriedly adapting the code in response to your earlier request. Fortunately the issue doesn't exist in any released software. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.