| ??? 07/16/03 18:34 Read: times |
#50835 - 32 bit division error |
Hi guys, I had been testing the 32-bit routine provided by Dr. Marshall in the code library section. I had been testing fixed values, but I kept getting incorrect values for my results from the division, and I found a pattern. For some reason, the quotient result is multiplied by 256 and that is the result given. For instance, I tested out 130,560/510. The correct answer is 256, but for some reason, it is giving me 256*256 = 65,536. I tried out simpler numbers like 2/1. The correct answer is 2, but it gave me 2*256 = 512. It keeps following the same pattern. I am posting the division code as well as my display routine below. Please give me advice if you can figure out where the problem is coming from, because I haven't been able to so far. The quotient should be in r0-r3 and the remainder in r4-r7. Thanks for the help.
Here is the code, I know it's long, but please bear with me: Start: ;registers r0-r3 are the dividend and equal 130,560 mov r0, #00h mov r1, #01h mov r2, #0FEh mov r3, #00h ;register r4 & r5 are the divisor and equal 510 mov r4, #01h mov r5, #0FEh lcall UDIV32 mov num, #0 ;variable to track leading zeros since i don't want them displayed lcall BilConv ;this is the display routine ljmp 0 ;this part below is the 32-bit division routine UDIV32: push 08 ; Save Register Bank 1 push 09 push 0AH push 0BH push 0CH push 0DH push 0EH push 0FH push dpl push dph push B setb RS0 ;Select Register Bank 1 mov r7, #0 ;clear partial remainder mov r6, #0 mov r5, #0 mov r4, #0 mov B, #32 ; set loop count div_lp32: clr RS0 ; Select Register Bank 0 clr C ; clear carry flag mov a, r0 rlc a mov r0, a mov a, r1 rlc a mov r1, a mov a, r2 rlc a mov r2, a mov a, r3 rlc a mov r3, a setb RS0 ;Select Register Bank 1 mov a, r4 ;the lowest bit of the rlc a ; partial remainder mov r4, a mov a, r5 rlc a mov r5, a mov a, r6 rlc a mov r6, a mov a, r7 rlc a mov r7, a mov a, r4 ;trial subtract divisor clr C ;from partial remainder subb a, 04 mov dpl, a mov a, r5 subb a, 05 mov dph, a mov a, r6 subb a, #0 mov 06, a mov a, r7 subb a, #0 mov 07, a cpl C jnc div_321 mov r7, 07 mov r6, 06 mov r5, dph mov r4, dpl div_321: mov a, r0 rlc a mov r0, a mov a, r1 rlc a mov r1, a mov a, r2 rlc a mov r2, a mov a, r3 rlc a mov r3, a djnz B, div_lp32 mov 07, r7 mov 06, r6 mov 05, r5 mov 04, r4 ;put remainder, saved before the last subtraction, in bank 0, and put quotient in bank 0 mov 03, r3 mov 02, r2 mov 01, r1 mov 00, r0 clr RS0 pop B pop dph pop dpl pop 0FH ; Retrieve Register Bank 1 pop 0EH pop 0DH pop 0CH pop 0BH pop 0AH pop 09 pop 08 ret ;----------------------------------------------------- ;this part below is my display routine BilConv: clr c mov counter, #0 BilLoop: MOV A,R3 ;how many time we can subtract 1,000,000,000 without carry (3B9ACA00h) SUBB A,#00H MOV R3,A MOV A,R2 SUBB A,#0CAH MOV R2,A MOV A,R1 SUBB A,#9AH MOV R1,A mov a, r0 subb a, #3Bh mov r0, a JC BilSkip INC counter mov num, #1 LJMP BilLoop BilSkip: mov a, counter mov save, a mov a, num cjne a, #1, continue mov a, save acall toASCII continue: mov a, r3 add a, #00h mov r3, a mov a, r2 addc a, #0CAh mov r2, a mov a, r1 addc a, #9Ah mov r1, a mov a, r0 addc a, #3Bh mov r0, a ;----------------------------------------- clr c mov counter, #0 HunMilLoop: MOV A,R3 ;how many time we can subtract 100,000,000 without carry (5F5E100h) SUBB A,#00H MOV R3,A MOV A,R2 SUBB A,#0E1H MOV R2,A MOV A,R1 SUBB A,#0F5H MOV R1,A mov a, r0 subb a, #05h mov r0, a JC HunMilSkip INC counter mov num, #1 LJMP HunMilLoop HunMilSkip: mov a, counter mov save, a mov a, num cjne a, #1, continue2 mov a, save acall toASCII continue2: mov a, r3 add a, #00h mov r3, a mov a, r2 addc a, #0E1h mov r2, a mov a, r1 addc a, #0F5h mov r1, a mov a, r0 addc a, #05h mov r0, a ;---------------------------------------------- clr C mov counter, #0 Hex3DecLoop1: MOV A,R3 ;how many time we can subtract 10,000,000 without carry (989680H) SUBB A,#80H MOV R3,A MOV A,R2 SUBB A,#96H MOV R2,A MOV A,R1 SUBB A,#98H MOV R1,A mov a, r0 subb a, #00h mov r0, a JC Hex3DecSkip1 INC counter mov num, #1 LJMP Hex3DecLoop1 Hex3DecSkip1: mov a, counter mov save, a mov a, num cjne a, #1, continue3 mov a, save acall toASCII continue3: mov a, r3 add a, #80h mov r3, a mov a, r2 addc a, #96h mov r2, a mov a, r1 addc a, #98h mov r1, a mov a, r0 addc a, #00h mov r0, a ;------------------------------------------------------------ clr c mov counter, #0 Hex3DecLoop2: MOV A,R3 ;how many times we can subtract 1,000,000 (F4240H) without carry SUBB A,#40H MOV R3,A MOV A,R2 SUBB A,#42H MOV R2,A MOV A,R1 SUBB A,#0FH MOV R1,A mov a, r0 subb a, #00h mov r0, a JC Hex3DecSkip2 INC counter mov num, #1 LJMP Hex3DecLoop2 Hex3DecSkip2: mov a, counter mov save, a mov a, num cjne a, #1, continue4 mov a, save acall toASCII continue4: MOV A,R3 ;how many times we can subtract 1,000,000 (F4240H) without carry add A,#40H MOV R3,A MOV A,R2 addc A,#42H MOV R2,A MOV A,R1 addc A,#0FH MOV R1,A mov a, r0 addc a, #00h mov r0, a ;------------------------------------------------------------ CLR C MOV counter, #0 Hex3DecLoop3: MOV A,R3 ;100000 (186A0H)? SUBB A,#0A0H MOV R3,A MOV A,R2 SUBB A,#86H MOV R2,A MOV A,R1 SUBB A,#01H MOV R1,A mov a, r0 subb a, #00h mov r0, a JC Hex3DecSkip3 INC counter mov num, #1 LJMP Hex3DecLoop3 Hex3DecSkip3: mov a, counter mov save, a mov a, num cjne a, #1, continue5 mov a, save acall toASCII continue5: MOV A,R3 ADD A,#0A0H MOV R3,A MOV A,R2 ADDC A,#86H MOV R2,A MOV A,R1 ADDC A,#01H MOV R1,A mov a, r0 addc a, #00h mov r0, a ;------------------------------------------------------------ CLR C MOV counter, #0 Hex2DecLoop1: MOV A,R3 ;10000 (2710h)? SUBB A,#010H MOV R3,A MOV A,R2 SUBB A,#27H MOV R2,A MOV A,R1 SUBB A,#0 MOV R1,A mov a, r0 subb a, #00h mov r0, a JC Hex2DecSkip1 INC counter mov num, #1 LJMP Hex2DecLoop1 Hex2DecSkip1: mov a, counter mov save, a mov a, num cjne a, #1, continue6 mov a, save acall toASCII continue6: MOV A,R3 ADD A,#010H MOV R3,A MOV A,R2 ADDC A,#27H MOV R2,A mov a, r1 addc a, #00h mov r1, a mov a, r0 addc a, #00h mov r0, a ;------------------------------------------------------------ CLR C MOV counter,#0 Hex2DecLoop2: MOV A,R3 ;1000 (3E8h)? SUBB A,#0E8H MOV R3,A MOV A,R2 SUBB A,#03H MOV R2,A JC Hex2DecSkip2 INC counter mov num, #1 LJMP Hex2DecLoop2 Hex2DecSkip2: mov a, counter mov save, a mov a, num cjne a, #1, continue7 mov a, save acall toASCII continue7: MOV A,R3 ADD A,#0E8H MOV R3,A MOV A,R2 ADDC A,#03H MOV R2,A ;------------------------------------------------------------ CLR C MOV counter,#0 Hex2DecLoop3: MOV A,R3 ;100 (64h)? SUBB A,#064H MOV R3,A MOV A,R2 SUBB A,#0H MOV R2,A JC Hex2DecSkip3 INC counter mov num, #1 LJMP Hex2DecLoop3 Hex2DecSkip3: mov a, counter mov save, a mov a, num cjne a, #1, continue8 mov a, save acall toASCII continue8: MOV A,R3 ADD A,#064H MOV R3,A MOV A,R2 ADDC A,#0H MOV R2,A ;------------------------------------------------------------ CLR C MOV counter,#0 Hex2DecLoop4: MOV A,R3 ;10 (0Ah)? SUBB A,#0AH MOV R3,A JC Hex2DecSkip4 INC counter mov num, #1 LJMP Hex2DecLoop4 Hex2DecSkip4: mov a, counter mov save, a mov a, num cjne a, #1, continue9 mov a, save acall toASCII continue9: MOV A,R3 ADD A,#0AH acall toASCII ;ljmp 0 RET toASCII: mov b, #30h add a, b lcall lcd_cout ret |
| Topic | Author | Date |
| 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division - Raj & Cristian | 01/01/70 00:00 | |
| RE: 32 bit division | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
| RE: 32 bit division error | 01/01/70 00:00 | |
RE: 32 bit division error | 01/01/70 00:00 |



