About division by 10

ZXNet echo conference «code.zx»

From Paul Falcon To Kirill Smelkow 6 March 1998

I’m sitting here, reading, and suddenly I see Kirill Smelkow writes to ALL... Hi Kirill! KS> Can anyone tell me the fast division algorithm KS> integers by ten. I just really need it, and I KS> I came up with something, but it looks pretty clumsy KS> and that means most likely we can do better. ld a,num yeah rrca rrca rrca rrca looks like it should work ▌█▐▐║▌█▐▐▌▐ W!7h 7he Be$7 W!$he$ fR0M CREATOR product ▌812/03.00▐ Powered by Cyrix 6x86L 200/16DIMM/S3TrV2 2Mb/SBPro/Q1.6Gb/USR33.6

From 500:812/22.01 To Paul Falcon 7 March 1998

┌┐ CM07p├0 B┴┴py┌ 06-03-98, B 01:23:34 ├┤ekT0 Paul Falcon ┌┐│/│Ca/ k Kirill Smelkow ┌┐p0 About division by 10: ├┤i Paul & Kirill! KS>> Can anyone tell me the fast division algorithm KS>> integers by ten. I just really need it, and I KS>> I came up with something, but it looks pretty clumsy KS>> and that means most likely we can do better. PF> ld a,num PF>daa PF>rrca PF>rrca PF>rrca PF>rrca PF> seems to work I thought again and finally decided - this is misinformation! It will never work, even after LSD. There are 2 errors here: after RRCA you need AND #0F and DAA... The fastest way would be a 256 byte plate for numbers in the range 0..255. If 16-bit operands are used, then the sign can take 4 pages :( Transfer from BIN->BCD is not carried out by the DAA command! To translate 1 byte number you need to add 6 to the value, if the lowest tetrad is greater than 9 and another 6 for each unit from the older notebook. That is number #3A converted to BCD format will be as follows: because the lowest tetrad > 9 is added6 and 6*3=18 is added, for a total of #58. 2All: who can suggest a _normal_ translation algorithm BIN<->BCD write... Wi7h be57 Wi5he5, FK0. -+- BASIC_48 AND POKES

From 500:812/06.06 To Kirill Smelkow 9 March 1998

03/05/98, at 01:02 Kirill Frolov wrote to Kirill Smelkow on the topic About division by 10 and I solved join... Glad to welcome you, Kirill!!! KS>> Can anyone tell me the fast division algorithm KS>> integers by ten. I just really need it, and I Not even just an algorithm, you can give a ready-made procedure: ;L=C/10 LD B,10 XOR A LD L,1 D1 RL C R.L.A. CP B JR C,ZER SUB B SLI L JP NC,D1 RET ZER SLA L JP NC,D1 RET Speed: from 388 to 396 clock cycles, 24 bytes. Taken from the fifth Format. Best regards, DMA from GRENNFORT INC. -+- SMM version 1.08

From 500:812/22.01 To Paul Falcon 11 March 1998

┌┐ CM07p├0 B┴┴py┌ 06-03-98, B 01:23:34 ├┤ekT0 Paul Falcon ┌┐│/│Ca/ k Kirill Smelkow ┌┐p0 About division by 10: ├┤i Paul & Kirill! KS>> Can anyone tell me the fast division algorithm KS>> integers by ten. I just really need it, and I KS>> I came up with something, but it looks pretty clumsy KS>> and that means most likely we can do better. PF> ld a,num PF>daa PF>rrca PF>rrca PF>rrca PF>rrca PF> seems to work DAA - decimal accumulator correction, used after adding or subtracting numbers in BCD format.... ...After addition it works as follows: 1).If the contents of the low tetrad A is greater than 9 or H=1, then the number 6 is added to the accumulator. 2).If the contents of the highest tetrad A became after this becomes more than 9 or C=1, then 6 is added to the highest battery notebook. After subtraction: 1).If H=1, then the number 6 is subtracted from the battery. 2).If C=1, then the number 6 is subtracted from the highest tetrad batteryFlasks: C - carry carry flag... H - halfcarry flag Addition, subtraction, increment commands, decrement and compare 8 bit operands set the H flag when there was a transfer from the third bit of the result into the fourth, or there was a loan from the fourth bit to the third. with this he tells the DAA team whether it requires result of decimal correction. N - addition/subtraction flag Addition, Increment, and Logical Instructions operations reset flag N, and subtraction commands decrement, comparison and inversion of it install. The N flag state is used by the command DAA because The correction algorithm is different when adding and subtracting, this flask shows what kind of action took place before this. Effect of executing various commands on flags: C Z P S N H LD R,N . . . . . . . - does not change ? - undefined DAA? ? P? . ? 0 - reset P - even result RRCA? . . . 0 0these were pieces of a book. And now to the essence of the question at hand - the algorithm is crazy. The flags are undefined and the result is appropriate. For him normal operation must first be set to the appropriate flask condition C,H,N. And will it still work after this? I'm at a loss. Maybe it will, maybe not. Wi7h be57 Wi5he5, FK0. -+- SMM version 1.08

From 500:812/22.01 To Paul Falcon 11 March 1998

┌┐ CM07p├0 B┴┴py┌ 06-03-98, B 01:23:34 ├┤ekT0 Paul Falcon ┌┐│/│Ca/ k Kirill Smelkow ┌┐p0 About division by 10: ├┤i Paul & Kirill! KS>> Can anyone tell me the fast division algorithm KS>> integers by ten. I just really need it, and I KS>> I came up with something, but it looks pretty clumsy KS>> and that means most likely we can do better. PF> ld a,num PF>daa PF>rrca PF>rrca PF>rrca PF>rrca PF> seems to work DAA - decimal accumulator correction, used after adding or subtracting numbers in BCD format.... ...After addition it works as follows: 1).If the contents of the low tetrad A is greater than 9 or H=1, then the number 6 is added to the accumulator. 2).If the contents of the highest tetrad A became after this becomes more than 9 or C=1, then 6 is added to the highest battery notebook. After subtraction: 1).If H=1, then the number 6 is subtracted from the battery. 2).If C=1, then the number 6 is subtracted from the highest tetrad batteryFlasks: C - carry carry flag... H - halfcarry flag Addition, subtraction, increment commands, decrement and compare 8 bit operands set the H flag when there was a transfer from the third bit of the result into the fourth, or there was a loan from the fourth bit to the third. with this he tells the DAA team whether it requires result of decimal correction. N - addition/subtraction flag Addition, Increment, and Logical Instructions operations reset flag N, and subtraction commands decrement, comparison and inversion of it install. The N flag state is used by the command DAA because The correction algorithm is different when adding and subtracting, this flask shows what kind of action took place before this. Effect of executing various commands on flags: C Z P S N H LD R,N . . . . . . . - does not change ? - undefined DAA? ? P? . ? 0 - reset P - even result RRCA? . . . 0 0these were pieces of a book. And now to the essence of the question at hand - the algorithm is crazy. The flags are undefined and the result is appropriate. For him normal operation must first be set to the appropriate flask condition C,H,N. And will it still work after this? I'm at a loss. Maybe it will, maybe not. Wi7h be57 Wi5he5, FK0. -+- SMM version 1.08

From 500:812/08.14 To Kirill Smelkow 11 March 1998

Hi, Kirill! KS> Actually needed for the following options: KS> unsigned integer #00..#FF KS> signed integer -128..+127 KS> the same for integers consisting of two bytes. hehe... I'm about to give birth... The simplest is the subtraction method. ;very long method ;X/Y=N.M, where X=N*Y+M ;A/C=B.A < processor registers at input and output DIV INC C DEC C JR Z,...; DIVIDE BY ZERO LD B,#FF M0 INC B SUB C JR NC,M0 ADD A,C RET ; with a sign ; A/C=B.A DIVS ADD A,A PUSH AF RRCA CALL DIV LD D,A SLA B POP AF RR B LD A,D RET The method is suitable for slow tasks. you can do the same to divide words, but when dividing by say 65525/2 it will take just a second... normal method: ;DE/BC=DE.HL DIV16 LD HL,0 LD HX,16 DIV0 SLI E RL D ADC HL,HL SBC HL,BC JR NC,DIV1 ADD HL,BC DEC E DIV1 DEC HX JR NZ,DIV0 RET You can come up with the sign yourself... now translation HEX to BCD ;DE-hex ;C(3..0),H(7..4),H(3..0),L(7..4),L(3..0)-bcd HTOBCD XOR A LD H,A:LD L,A:LD C,A LD B,16 HTB0 RL E:RL D LD A,L:ADC A,L:DAA:LD L,A LD A,H:ADC A,H:DAA:LD H,A LD A,C:ADC A,C:DAA:LD C,A DJNZ HTB0 RETand vice versa... ;DE-address of the string, at the end - #00 ;HL-hex BCDTH LD HL,0 LD HX,5 BCDTH0 ADD HL,HL LD B,H:LD C,L ADD HL,HL ADD HL,HL ADD HL,BC LD A,(DE):INC DE SUB "0":RET C ADD A,L:LD L,A JR BCDTH0 All the best. Dmitry aka -=LD=- / X-TRADE GROUP. -+- SMM version 1.08

From 500:812/08.29 To Kirill Smelkow 11 March 1998

Hi All! KS>> Can anyone tell me..... I found this question interesting and this is what I wrote: ; !!! DIVISION BY 10!!! ; ; A = A / 10 ; TIME IN BEATS ; LD BC,#A007 ; 10 ; ────┐ METKA CP B ; 4 │ JR C,$+3 ; 8/13 │ SUB B ; 4/\n R C ; 8 > 5 times SRL B ; 8/ BIT 0,B ; 8 │ JR Z, METKA ; 8/13 │-4 times=13 ; ────┘-1 time =8 LD A,C ; 4 CPL ; 4 RET ; 10 TOTAL MAXIMUM NUMBER OF CYCLES: ; 10+20+65+40+40+40+60+18 = 293 BLOCKS LENGTH = 18 BYTES If speed is very important, then the cycle can be deployed while winning about 100 bars... -+- ZX-ASM