Comparing two 16bit numbers...
Comparing two 16bit numbers...
I have very almost finished a game I have been beavering away on. I have opted to write the whole game in machine code. I have one last feature that has me stumped. Hi-Score.
Is there a ROM routine or some elegant way to do the following IF score>hiscore routine (written in monknomics)
ld hl,(score)
ld de,(hiscore)
is hl greater than de
jp nz,no-hiscore-here
ld (hiscore),hl
no-hiscore-here:
call carry-on
Is there a ROM routine or some elegant way to do the following IF score>hiscore routine (written in monknomics)
ld hl,(score)
ld de,(hiscore)
is hl greater than de
jp nz,no-hiscore-here
ld (hiscore),hl
no-hiscore-here:
call carry-on
Re: Comparing two 16bit numbers...
You can do it like that but most people are going to advise you to just use ascii to store the high scores.
http://chuntey.arjunnair.in/?p=156
Take a look at this if you have not already, see if it floats your boat. if you still want to do a 16 bit compare then look at the lance l z80 book.
If you are following the Nirvana+ tutorial of seven-FFF its discussed in this part..
https://seven-fff.com/blog/zalaxa-10-a- ... g-routine/
http://chuntey.arjunnair.in/?p=156
Take a look at this if you have not already, see if it floats your boat. if you still want to do a 16 bit compare then look at the lance l z80 book.
If you are following the Nirvana+ tutorial of seven-FFF its discussed in this part..
https://seven-fff.com/blog/zalaxa-10-a- ... g-routine/
Re: Comparing two 16bit numbers...
Simple. A comparison is just a subtraction with the result thrown away.
Code: Select all
or a ;clear carry flag
ld hl,(highscore)
ld de,newscore
sbc hl,de
jr nc,cont ;if highscore >= newscore, carry flag will be cleared
ld (highscore),de ;if newscore > highscore, store new highscore
cont
...
Re: Comparing two 16bit numbers...
I do scores and high-scores using a list of digits, each byte holds one digit 0..9.
To add to the score you set up a parallel buffer of the digits you want to add, then work from right to left (low digit to high digit) adding the new digits to the total and carrying '1' if needed.
To compare two scores, this time you work left to right (from high digit down to low digit), comparing each digit. If one digit is greater or less than the other, that's your answer. If they're equal, then you move on to compare the next digit.
To print the score, you can use sprites for the digits or add 48 (0x30) to get the ASCII character. Working from left-to-right, you can have a flag in your routine so that you start printing spaces for '0' digits, then after your first non-zero digit, you switch to printing '0' for zeroes.
To add to the score you set up a parallel buffer of the digits you want to add, then work from right to left (low digit to high digit) adding the new digits to the total and carrying '1' if needed.
To compare two scores, this time you work left to right (from high digit down to low digit), comparing each digit. If one digit is greater or less than the other, that's your answer. If they're equal, then you move on to compare the next digit.
To print the score, you can use sprites for the digits or add 48 (0x30) to get the ASCII character. Working from left-to-right, you can have a flag in your routine so that you start printing spaces for '0' digits, then after your first non-zero digit, you switch to printing '0' for zeroes.
Re: Comparing two 16bit numbers...
Cheers Nomad, thanks for the pointer to the tutorial, I am looking at NIRVANA+ for my next game idea. Bloomin' awesome looking sprite routine. I am converting my score to two rows of UDG's (the score is shown as a 2 char high 7-seg LCD).
Thank you utz
I was trying SBC but I was getting confused. I think the OR A is important, I was omitting this. I am still a bit hazy with z80 maths. Will try some more.
Thank you utz
I was trying SBC but I was getting confused. I think the OR A is important, I was omitting this. I am still a bit hazy with z80 maths. Will try some more.
Re: Comparing two 16bit numbers...
I'd recommend ASCII too, storing score as a 16bit number is easier to compare (as in Utz's example) but printing a 16bit number is much more onerous.Nomad wrote: ↑Wed Mar 07, 2018 11:43 am You can do it like that but most people are going to advise you to just use ascii to store the high scores.
http://chuntey.arjunnair.in/?p=156
Re: Comparing two 16bit numbers...
[mention]Joefish[/mention] I have been doing this in reverse each time I display the score. The score gets increased every game 'round' so I do nto think it will be a problem in this game. In a game with a less regular score change , your method would be way more optimised.
Re: Comparing two 16bit numbers...
Yes that is exactly the path I followed.
At first because I was lazy I just stored the values as 16 bit, but when it comes time to display it - disaster. All that time i saved keeping things simple with the 16 bit score was lost trying to figure out a decent way to print the values. I should have just bitten the bullet and use the ascii score from the start.
Just pure conjecture but I would say most of the programs in the archive that use high score are going to go the ascii route also.
Re: Comparing two 16bit numbers...
Yahhooo,
It works
Thank you, Thank you
I will maybe re-write my whole score routine in a future game, but I have a working game now...it's for CSSCGC sooo
It works
Thank you, Thank you
I will maybe re-write my whole score routine in a future game, but I have a working game now...it's for CSSCGC sooo
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Comparing two 16bit numbers...
SBC simply takes the carry flag into account and subtracts it as well. In other words, SBC HL,DE always works like this:
HL - DE - CY -> HL
Since Carry can be either 0 or 1, your result will vary depending on its state. Thus, if you don’t want errors (and know that your results will fit the 0–65535 range), you need to clear Carry prior to executing this instruction.
Eight-bit registers have the benefit of the SUB instruction, which simply ignores the carry.
Quick Tip: SBC is kind of slow—15 T states. Sometimes, especially when dealing with constants, it’s more beneficial to add a negative number than to subtract a positive one.
Instead of
Code: Select all
or a ;[4]
ld de,15
sbc hl,de ;[15]
Code: Select all
ld de,-15
add hl,de ;[11]
Every man should plant a tree, build a house, and write a ZX Spectrum game.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Re: Comparing two 16bit numbers...
[mention]Ast A. Moore[/mention]
This makes things so much clearer. As soon as I discovered SBC I knew I had strayed into deeper waters.
I have seen the technique you speak of in a routine I pilfered to strip my 16bit score into individual digits. I need to re read my comments on that routine because I may not have fully understood the reasoning behind the use of ADD with a negative 16bit number.
Cheers, the support here a SCF is so helpful to me
This makes things so much clearer. As soon as I discovered SBC I knew I had strayed into deeper waters.
I have seen the technique you speak of in a routine I pilfered to strip my 16bit score into individual digits. I need to re read my comments on that routine because I may not have fully understood the reasoning behind the use of ADD with a negative 16bit number.
Cheers, the support here a SCF is so helpful to me