Hmm, not sure why I haven't posted this here, yet, either. This is pretty useful, especially for parsing a list of numbers from some form of user input. Feel free to optimise and report back

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| ;=============================================================== ConvRStr: ;=============================================================== ;Input: ; DE points to the base 10 number string in RAM. ;Outputs: ; HL is the 16-bit value of the number ; DE points to the byte after the number ; BC is HL/10 ; c flag reset (nc) ; z flag reset (nz) ;Destroys: ; A (actually, add 30h and you get the ending token) ;Size: 23 bytes ;Speed: 104n+42+11c ; n is the number of digits ; c is at most n-2 ; at most 595 cycles for any 16-bit decimal value ;=============================================================== ld hl,0 ; 10 : 210000 ConvLoop: ; ld a,(de) ; 7 : 1A sub 30h ; 7 : D630 cp 10 ; 7 : FE0A ret nc ;5|11 : D0 inc de ; 6 : 13 ; ld b,h ; 4 : 44 ld c,l ; 4 : 4D add hl,hl ; 11 : 29 add hl,hl ; 11 : 29 add hl,bc ; 11 : 09 add hl,hl ; 11 : 29 ; add a,l ; 4 : 85 ld l,a ; 4 : 6F jr nc,ConvLoop ;12|23: 30EE inc h ; --- : 24 jr ConvLoop ; --- : 18EB
|
The ones with t-states as '---' are computed along with the previous instruction to make calculations easier. Anyways, to give an idea, at the slowest, this can execute 9803 times per second (assuming you are using call which takes another 17 t-states). This stops reading when a character that is not a decimal number is run into (for example, a comma or newline).
EDIT: By removing that one byte, timing is much more easily computed and slowest time drops from 625 to 595 t-states

This means it can execute an extra 459 times per second. It also makes the c flag have a definite output and as well the z flag