Comparing a (1 byte) variable with a number for equal

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
bkumanchik
Posts: 12
Joined: Thu Sep 14, 2023 11:19 pm

Comparing a (1 byte) variable with a number for equal

Post by bkumanchik »

I've created a variable called i_frame that (in this case) will either be a 1 or a 2 (see code below for a simple example)

In the check frame area I'm just trying to see if i_frame is equal to 2 or not, if it is I'd like to do frame 2 stuff else if it's 1 it would drop through to do frame 1 stuff then skip over the do frame 2 stuff to the end, then be repeated.

So basically I'm having trouble with the check frame part of my code. I can get this to work with two 16-bit constants (there are lots of tutorials and examples of this) but not an 8-bit variable and an imediate number, any help would be appreciated,

Thanks, Brian :)

Code: Select all


; variables
i_frame		.byte	1			; starting frame


main_loop:	
		
	
	; check frame
	LD		A,(i_frame)		; get current frame
	CP		2				; compare to see if it's 2	
	OR 		A    				; reset carry flag	
	JP 		Z,frame_2_do 		; if so...	
	

; ...if not continue here
	; do frame 1 stuff	
	JP 		after_check_frame  	; then jump to...
		

; ...jump to here
frame_2_do:
	; do frame 2 stuff 
	

after_check_frame:				; ...here
	
	
	JP		main_loop			; return to start
	
dr beep
Posts: 2080
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Comparing a (1 byte) variable with a number for equal

Post by dr beep »

skip the OR A
bkumanchik
Posts: 12
Joined: Thu Sep 14, 2023 11:19 pm

Re: Comparing a (1 byte) variable with a number for equal

Post by bkumanchik »

It works if i_frame is a 2 but if I change it to a 1 it doesn't, it crashes with a 5/10
bkumanchik
Posts: 12
Joined: Thu Sep 14, 2023 11:19 pm

Re: Comparing a (1 byte) variable with a number for equal

Post by bkumanchik »

weird thing is it works with a 2 and a 3 - this is the first time I had tried it like that.

Hmm
User avatar
stefano
Posts: 568
Joined: Tue Dec 11, 2012 9:24 am
Contact:

Re: Comparing a (1 byte) variable with a number for equal

Post by stefano »

remove OR A !

listen to dr beep!
User avatar
1024MAK
Posts: 5118
Joined: Mon Sep 26, 2011 10:56 am
Location: Looking forward to summer in Somerset, UK...

Re: Comparing a (1 byte) variable with a number for equal

Post by 1024MAK »

OR A affects both register A contents and various flags. This is not something you want to do if after this instruction, you are then going to be using/testing the state of the flags from a previous operation.

Mark
ZX81 Variations
ZX81 Chip Pin-outs
ZX81 Video Transistor Buffer Amp

:!: Standby alert :!:
There are four lights!
Step up to red alert. Sir, are you absolutely sure? It does mean changing the bulb :!:
Looking forward to summer later in the year.
bkumanchik
Posts: 12
Joined: Thu Sep 14, 2023 11:19 pm

Re: Comparing a (1 byte) variable with a number for equal

Post by bkumanchik »

I did remove the OR A, it works with 2,3 but crashes on 1.
dr beep
Posts: 2080
Joined: Thu Jun 16, 2011 8:35 am
Location: Boxmeer

Re: Comparing a (1 byte) variable with a number for equal

Post by dr beep »

How can the value be 3 when your first comment state it is 1 or 2 only?

with testing odd or even you could replace the cp 2 with and 1 and when Z goto frame2.
bkumanchik
Posts: 12
Joined: Thu Sep 14, 2023 11:19 pm

Re: Comparing a (1 byte) variable with a number for equal

Post by bkumanchik »

It's just checking to see whether it's 2 or NOT (3 being not) but 1 doesn't work (as NOT)
User avatar
Andy Rea
Posts: 1606
Joined: Fri May 09, 2008 2:48 pm
Location: Planet Earth
Contact:

Re: Comparing a (1 byte) variable with a number for equal

Post by Andy Rea »

If it were me i would use 0 or 1 ( single bit rather than 2 separate bits)
and change the code to something like :-

Code: Select all


; variables
i_frame		.byte	1			; starting frame


main_loop:	
		
	
	; check frame
	LD		A,(i_frame)		; get current frame  
	AND		1				; bit 0 set ?
	
	JP 		NZ,frame_1_do 		; if so...	
	

; ...if not continue here
	; do frame 0 stuff	
	JP 		after_check_frame  	; then jump to...
		

; ...jump to here
frame_2_do:
	; do frame 1 stuff 
	

after_check_frame:				; ...here
	
	
	JP		main_loop			; return to start
	
I think.... im a little rusty on the old Z80

Regards Andy
what's that Smell.... smells like fresh flux and solder fumes...
Post Reply