Page 1 of 2

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

Posted: Fri Sep 15, 2023 3:00 pm
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
	

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

Posted: Fri Sep 15, 2023 4:59 pm
by dr beep
skip the OR A

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

Posted: Fri Sep 15, 2023 5:23 pm
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

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

Posted: Fri Sep 15, 2023 5:30 pm
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

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

Posted: Fri Sep 15, 2023 11:09 pm
by stefano
remove OR A !

listen to dr beep!

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

Posted: Sat Sep 16, 2023 10:11 am
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

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

Posted: Sat Sep 16, 2023 4:19 pm
by bkumanchik
I did remove the OR A, it works with 2,3 but crashes on 1.

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

Posted: Sat Sep 16, 2023 5:04 pm
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.

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

Posted: Sat Sep 16, 2023 6:08 pm
by bkumanchik
It's just checking to see whether it's 2 or NOT (3 being not) but 1 doesn't work (as NOT)

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

Posted: Sat Sep 16, 2023 6:24 pm
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