Comparing two 16bit numbers...

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Comparing two 16bit numbers...

Post by MonkZy »

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
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Comparing two 16bit numbers...

Post by Nomad »

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/
User avatar
utz
Microbot
Posts: 114
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Comparing two 16bit numbers...

Post by utz »

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
  ...
User avatar
Joefish
Rick Dangerous
Posts: 2042
Joined: Tue Nov 14, 2017 10:26 am

Re: Comparing two 16bit numbers...

Post by Joefish »

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.
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Re: Comparing two 16bit numbers...

Post by MonkZy »

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.
User avatar
R-Tape
Site Admin
Posts: 6353
Joined: Thu Nov 09, 2017 11:46 am

Re: Comparing two 16bit numbers...

Post by R-Tape »

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
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.
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Re: Comparing two 16bit numbers...

Post by MonkZy »

[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.
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Comparing two 16bit numbers...

Post by Nomad »

R-Tape wrote: Wed Mar 07, 2018 12:04 pm 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.
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. :lol:

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.
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Re: Comparing two 16bit numbers...

Post by MonkZy »

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
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Comparing two 16bit numbers...

Post by Ast A. Moore »

MonkZy wrote: Wed Mar 07, 2018 12:03 pm I think the OR A is important, I was omitting this. I am still a bit hazy with z80 maths. Will try some more.
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]
you can do

Code: Select all

	ld de,-15
	add hl,de	;[11]
which is 8 T states faster and two bytes shorter.
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.
User avatar
MonkZy
Manic Miner
Posts: 278
Joined: Thu Feb 08, 2018 1:01 pm

Re: Comparing two 16bit numbers...

Post by MonkZy »

[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
Post Reply