Hello,
Could I have a z80 routine that outputs a number in base-10? Preferably simple or well-commented, so I can easily port it to KnightOS.
SirCmpwn wrote:
Hello,
Could I have a z80 routine that outputs a number in base-10? Preferably simple or well-commented, so I can easily port it to KnightOS.


Something like you enter 1000 and it gives back 10^3?

or you enter 10^3 and it gives you 1000?
How is the number stored?

Here's one technique (in pseudo-code) that works quite well and is simple to understand. It'll work between -999 and +999; I'm sure you can tell how to scale it up. Smile


Code:
number = 123

' handle negative numbers.
if number < 0
   display '-'
   number = -number
end if

' counters for the three possible output digits.
hundreds = 0
tens = 0
units = number

' count the number of hundreds.
while units > 99
   hundreds = hundreds + 1
   units = units - 100
end while

' count the number of tens.
while units > 9
   tens = tens + 1
   units = units - 10
end while

' the condition is to strip leading zeroes.
if number > 99
   display '0' + hundreds
end if

if number > 9
   display '0' + tens
end if

' we always want to display the units.
display '0' + units
Sad I hope the source you released doesnt have a font to far from the original OS's or this might make peoples suggestions a little more difficult...
[/code]
Anakclusmos wrote:
Sad I hope the source you released doesnt have a font to far from the original OS's or this might make peoples suggestions a little more difficult...
[/code]
I don't see why it would matter at all; a digit is a digit is a digit. For what it's worth, I agree with Ben's pseudocode, which would be both easy to implement and easy to modify to a different range if necessary.
it would mean a change in the math, if the original tios uses a font with the numbers lined as 0123456789 and sircmpwn uses 1234567890 it could throw a little issue with some routines.
Anakclusmos wrote:
it would mean a change in the math, if the original tios uses a font with the numbers lined as 0123456789 and sircmpwn uses 1234567890 it could throw a little issue with some routines.
Ahhh, I see what you're saying, you mean which characters have which ordinal values, that's a good point. I'd hope he at least follows ASCII standard for the numbers and lowercase and uppercase letters, the way TI did.
I follow standard ASCII. I can probably use Ben's psuedocode. The number should be stored in HL, by the way.
SirCmpwn wrote:
I follow standard ASCII. I can probably use Ben's psuedocode. The number should be stored in HL, by the way.
So are you going to start writing the code based on Ben's pseudocode and show us what you come up with? Smile
Yes, after looking around a bit more.
SirCmpwn wrote:
Yes, after looking around a bit more.
And have you happened to discover anything exciting or more optimal in the course of your looking-around?
Wow, I found this awesome website that has routines for everything you'd ever want to do to numbers in z80! Here's the routine I was looking for: Routine
SirCmpwn wrote:
Wow, I found this awesome website that has routines for everything you'd ever want to do to numbers in z80! Here's the routine I was looking for: Routine
Funnily enough, I've used that exact routine from that site before in Invalid Tangram. Just shows to go you the value of a little JFGI. Make sure you understand the code, though - it's useless to you if you lift it without understanding how it works imho. Smile
I actually went to Google several times, without success. I eventually just searched for "z80 routines," which is where I got this.
SirCmpwn wrote:
I actually went to Google several times, without success. I eventually just searched for "z80 routines," which is where I got this.
No, I'm sure you did; I was commenting on the general surprising breadth and depth of information that may be found on the internet with a bit of clever searching.
KermMartian wrote:
No, I'm sure you did; I was commenting on the general surprising breadth and depth of information that may be found on the internet with a bit of clever searching.

Ah, "JFGI" indicated you thought otherwise.
SirCmpwn wrote:
KermMartian wrote:
No, I'm sure you did; I was commenting on the general surprising breadth and depth of information that may be found on the internet with a bit of clever searching.

Ah, "JFGI" indicated you thought otherwise.
JFGI is my general reference to the act of Googling stuff; no judgement is intended from it unless I specifically command someone to JFGI.
Okay, got it. Smile
Here's a routine of my own design to do the reverse process - take an ASCII string representing a number between 0 and 65535, and store it in HL:

Code:
; DE == pointer to number to convert
; HL == Output
ASCIIToDec:
   ld hl, 0
   ld bc, 10000
   call NumConvert
   ld bc, 1000
   call NumConvert
   ld bc, 100
   call NumConvert
   ld bc, 10
   call NumConvert
   ld bc, 1
NumConvert:
   ld a, (de)
   sub a, '0'
ConvertLoop:
   or a
   jr z, ConvertLoopDone
   add hl, bc
   dec a
   jr ConvertLoop
ConvertLoopDone:
   inc de
   ret
That only works if you have a 5-character string though, right? You would need to have input string "00001", for 1.

Here is a routine I made a while back that will parse a string of undetermined length, stopping at the first non-digit:

Code:
;Start with 0
  ld hl,0
digitLoop:
;Read character and make sure it is a digit
  ld a,(de)
  sub '0'
  cp 10
  jr nc,notADigit
;Increment pointer
  inc de
;Multiply HL by 10
  ld b,h
  ld c,l
  add hl,hl
  add hl,hl
  add hl,bc
  add hl,hl
;Add the digit value in A to HL
  add a,l
  ld l,a
  jr nc,digitLoop
  inc h
  jr digitLoop
notADigit:
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement