CPCWiki forum

General Category => Programming => Topic started by: IndyUK on 11:56, 17 December 18

Title: 16 Bit register compare
Post by: IndyUK on 11:56, 17 December 18
Hi All,
I've been delving into Assembly leanguage, in particular Z80 (Amstrad fan). I've put together a program which moves a pixel around the screen (Mode 1) using the Arrow keys. This is all working fine however, I'm stuck on how to check the min/max x/y positions so the pixel doesn't disappear of the screen. In any high level language it would be a very simple case of using a typical x-pos>320 etc. However, that's not the case with Assembly. The pixel co-ordinates in my code are being stored in registers DE & HL because I'm using the firmware to plot the pixel (&BBEA). So, the problem is I don't know how to compare DE to a maximum allowed value i.e the screen max x pixels. Of course I have the same issue with the y-pos. I have a feeling I have to split the registers since there is no 16 bit comparison option.
I would be grateful for any advice/pointer.
Thanks.
Title: Re: 16 Bit register compare
Post by: Docent on 13:15, 17 December 18
Quote from: IndyUK on 11:56, 17 December 18
Hi All,
I've been delving into Assembly leanguage, in particular Z80 (Amstrad fan). I've put together a program which moves a pixel around the screen (Mode 1) using the Arrow keys. This is all working fine however, I'm stuck on how to check the min/max x/y positions so the pixel doesn't disappear of the screen. In any high level language it would be a very simple case of using a typical x-pos>320 etc. However, that's not the case with Assembly. The pixel co-ordinates in my code are being stored in registers DE & HL because I'm using the firmware to plot the pixel (&BBEA). So, the problem is I don't know how to compare DE to a maximum allowed value i.e the screen max x pixels. Of course I have the same issue with the y-pos. I have a feeling I have to split the registers since there is no 16 bit comparison option.
I would be grateful for any advice/pointer.
Thanks.

You can use the following code:

ld bc, 320
or a
ex de, hl
sbc hl, bc
add hl, bc
ex de, hl
jr c, bc_higher
...


Title: Re: 16 Bit register compare
Post by: Axelay on 14:01, 17 December 18
An option for checking de>=320 if you can live with effectively only being able to check even coordinate positions, that might work for your situation:

ld a,d
rra
ld a,e
rra
cp a,160
jr c, de_on_screen
...
Title: Re: 16 Bit register compare
Post by: IndyUK on 15:22, 17 December 18
Quote from: Docent on 13:15, 17 December 18
You can use the following code:

ld bc, 320
or a
ex de, hl
sbc hl, bc
add hl, bc
ex de, hl
jr c, bc_higher
...

Hi,
Thanks for the example. I added that into my code in all four key checks and, with a bit of adjustments, everything is working very nicely. Thank You!
Does your approach have any limitations? And also, what is the "or a" used for? I commented it out since I couldn't see what purpose it served and the code works fine.
Thanks
Title: Re: 16 Bit register compare
Post by: dthrone on 15:46, 17 December 18
"or a" resets the carry flag because that is included in the "sbc".
Title: Re: 16 Bit register compare
Post by: IndyUK on 15:55, 17 December 18
Quote from: dthrone on 15:46, 17 December 18
"or a" resets the carry flag because that is included in the "sbc".
Thanks for the clarification. I shall reinstate it.
Title: Re: 16 Bit register compare
Post by: Docent on 01:10, 18 December 18
Quote from: IndyUK on 15:22, 17 December 18
Hi,
Thanks for the example. I added that into my code in all four key checks and, with a bit of adjustments, everything is working very nicely. Thank You!
Does your approach have any limitations? And also, what is the "or a" used for? I commented it out since I couldn't see what purpose it served and the code works fine.
Thanks

No problem.
There are two limitations I can think of - the execution time is quite long and you need to load one of values into hl due to add/sbc operating only on hl. There are also benefits - the code is compact and it doesn't modify any registers.

'or a' clears the carry flag.
Title: Re: 16 Bit register compare
Post by: AMSDOS on 05:03, 18 December 18
I've just grabbed these from my Assembly book (Assembly Language Programming For the Amstrad CPC 464,664 & 6128):


Example 9.3
Objective: branch to 'over' if 'number' in register pair = zero

LD BC,(number)
LD A,B
OR C
JR Z,over
- - - - - - -
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.4
Objective: branch to 'over' if 'number' in register pair = non zero.



LD BC,(number)
LD A,B
OR C
JR NZ,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.6
Objective: branch to label 'over' if 'first' double byte number = 'second' double byte number.



LD HL,(first)
LD DE,(second)
AND A
SBC HL,DE
JR Z,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.8
Objective: branch to 'over' if 'first' double byte number is not equal to, 'second double byte number.

LD HL,(first)
LD DE,(second)
AND A
SBC HL,DE
JR NZ,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.10
Objective: branch to 'over' if 'first' double byte number is greater than, or equal to, 'second' double byte number.

LD HL,(first)
LD DE,(second)
AND A
SBC HL,DE
JR NC,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.12
Objective: branch to 'over' if 'first' double byte number is less than 'second' double byte number.

LD HL,(first)
LD DE,(second)
AND A
SBC HL,DE
JR NC,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.14
Objective: branch to 'over' if 'first' double byte number is less than 'second' double byte number.

LD DE,(first)
LD HL,(second)
AND A
SBC HL,DE
JR C,over
- - - - - - -
- - - - - - -
over:
- - - - - - -



Example 9.16
Objective: branch to 'over' if 'first' double byte number is less than, or equal to, 'second' double byte number.

LD HL,(second)
LD DE,(first)
AND A
SBC HL,DE
JR NC,over
- - - - - - -
- - - - - - -
over:
- - - - - - -
Title: Re: 16 Bit register compare
Post by: IndyUK on 11:56, 18 December 18
Hi Guys
Thank you to all of you! You've been a great help.

AMSDOS - Thank you for the different examples. They will no doubt come very handy. The book you have referred to seems to be very useful. I have a book on Machine Code for the Amstrad but doesn't give examples like yours.
Thanks



Title: Re: 16 Bit register compare
Post by: AMSDOS on 11:06, 20 December 18
Quote from: IndyUK on 11:56, 18 December 18
Hi Guys
Thank you to all of you! You've been a great help.

AMSDOS - Thank you for the different examples. They will no doubt come very handy. The book you have referred to seems to be very useful. I have a book on Machine Code for the Amstrad but doesn't give examples like yours.
Thanks


Some of the books which were available for the Amstrad seem to be dealing with routines which would normally be too slow to operate in BASIC. My book I feel correctly avoids using the term "Machine Code", which is essentially the final output of that Assembly Source Code, so by saying "Assembly Language", it's implying it's a book with Assembly Language examples. Some BASIC stuff has crept into it, though merely as example to deal with an Assembly routine. It's not a book with Advanced Assembly code and many of the routines make use of the Firmware, which is why those examples I posted present a more simplistic approach for comparing larger numbers, so like all things Assembly can also be refined or improved.


Another book which looks similar to mine is "Machine Code for Beginners on the Amstrad", it has a chapter on  Flags, Conditions and Decision Making. The Decision Making part of my book was where those examples were included, though my book continues on with Loops of all kinds.
The small Assembly game (http://www.cpcwiki.eu/index.php/Programming:Coding_a_simple_BASIC_game_into_Assembly#Assembly_Version) I made earlier in the year presented me with a new problem of creating an infinite loop which I hadn't done before. In that game, I had written a way to test where the Rocks were placed and return a colour to determine if the game continued or cease.
Title: Re: 16 Bit register compare
Post by: GUNHED on 19:01, 20 December 18
If you hack in hex codes, then this is machine code. Well, the world before MAXAM was all about to hack it in with your own fingers.  :laugh: :)
Powered by SMFPacks Menu Editor Mod