Another rather newbie question

Posts about Z80 programming that do not relate to a particular computer
Post Reply
Nephesh
Posts: 6
Joined: Wed Dec 02, 2020 2:16 pm

Another rather newbie question

Post by Nephesh » Sat Jan 16, 2021 8:09 am

Hello. So I'm looking at the hex converter you wrote, and I have two questions:
So the division works by loading the register b with 16 (because hex is base 16), subtracting the "main" number with it, and then adding a "tally" by increasing the c register until the register B gets to zero.

That I understood. However, now I am rather curious as to how this works:

Code: Select all

PrintHexChar:
	cp 10				;If we're less then 10, add Ascii '0' to it (48) to print a Digit
	jr c,PrintHexCharNotAtoF
					;Our digit is 10 or above so we need to print an Ascii letter 
	add 7				;A-F, so add another 7
PrintHexCharNotAtoF:
	add 48				;Add 48 (character '0')
	jp PrintChar			;print it
Why "add 7" and why "add 48"? I know it has something to do with the fact that the difference between 9 and F is 7, but adding for example, 10+7 will yield 11 in hex, so no letter.

Also, why add 48? I know it's the character for "0" in ASCII, but I'm still rather confused.

User avatar
akuyou
Posts: 463
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Another rather newbie question

Post by akuyou » Sat Jan 16, 2021 11:24 am

We want to convert a Hex value to a Ascii letter.
As you said 0 in Ascii is 48, so we add Ascii '0' (48) to number zero to convert our number to a letter - we can then show this letter to the screen.

this only works up to 9, we then need to jump to the letters, so we add 7 when needed.

If you don't understand, try changing the values or remove the 'Add 7' and see what happens!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

John
Posts: 32
Joined: Tue Mar 24, 2020 1:18 am

Re: Another rather newbie question

Post by John » Sun Jan 17, 2021 2:07 am

Hi Nephesh,

If you didn't understand Keith's explanation I will add a little more to it so maybe you can understand it. To convert a hex value to an ASCII letter first notice that in the ASCII table there is a gap, of size 7,between the two groups of characters 0 to 9 and A to F, this obviously suggest to split the conversion into two cases adding 48 or adding 52-48+7 (the size of the gap), depending on if it is converting to a number or hex letter. Keith has done the 52 addition as an add 48, then an add 7 (if required); the character to be converted is assumed to be a number from the start so the add 48 is always there. Thus this implements the two cases for conversion in Keith's way.

Nephesh
Posts: 6
Joined: Wed Dec 02, 2020 2:16 pm

Re: Another rather newbie question

Post by Nephesh » Mon Jan 18, 2021 4:12 pm

If you didn't understand Keith's explanation I will add a little more to it so maybe you can understand it. To convert a hex value to an ASCII letter first notice that in the ASCII table there is a gap, of size 7,between the two groups of characters 0 to 9 and A to F, this obviously suggest to split the conversion into two cases adding 48 or adding 52-48+7 (the size of the gap), depending on if it is converting to a number or hex letter. Keith has done the 52 addition as an add 48, then an add 7 (if required); the character to be converted is assumed to be a number from the start so the add 48 is always there. Thus this implements the two cases for conversion in Keith's way.
Hi.
So in ASCII, checking out the table, the distance from the numbers to the letters is in fact 7 as there are some punctuation characters in between them.

But what about the 48? It's a little hard to wrap my head around.
Thank you.

User avatar
akuyou
Posts: 463
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Another rather newbie question

Post by akuyou » Tue Jan 19, 2021 6:27 am

You already answered that yourself... it's the ascii for '0'!
We're converting a number in a register to an ascii character for the screen, after all.
Try changing it to a different number, and see what happens!

Alternatively, it may help to work this the other way round... start from scratch, and try to write your own routine to show the Hex value of a register - There's sure to be other (better?) ways to do it than mine!
Often it's only when you try to work out the solution to a problem you start to learn why things are the way they are (it was that way for me)
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Post Reply

Return to “General Z80 Programming”