Control Structures
These are examples of control structures. You may also be interested in:

 Conditional structures
 Exceptions
 Flow-control structures
 Loops



Assume functions   a   and   b   return boolean values,   and further, the execution of function   b   takes considerable resources without side effects, and is to be minimized.

If we needed to compute the conjunction   (and):

  x = a() and b() 
Then it would be best to not compute the value of   b()   if the value of   a()   is computed as   false,   as the value of   x   can then only ever be    false.

Similarly, if we needed to compute the disjunction (or):

  y = a() or b() 
Then it would be best to not compute the value of   b()   if the value of   a()   is computed as   true,   as the value of   y   can then only ever be   true.

Some languages will stop further computation of boolean equations as soon as the result is known, so-called   short-circuit evaluation   of boolean expressions



Task

Create two functions named   a   and   b,   that take and return the same boolean value.

The functions should also print their name whenever they are called.

Calculate and assign the values of the following equations to a variable in such a way that function   b   is only called when necessary:

  x = a(i) and b(j) 
  y = a(i) or b(j) 

If the language does not have short-circuit evaluation, this might be achieved with nested     if     statements.




;DEFINE 0 AS FALSE, $FF as true.
False equ 0
True equ 255
Func_A:
	;input: accumulator = value to check. 0 = false, nonzero = true.
	;output: 0 if false, 255 if true. Also prints the truth value to the screen.
	;USAGE: LDA val JSR Func_A
	BEQ .falsehood
	load16 z_HL,BoolText_A_True ;lda #<BoolText_A_True sta z_L lda #>BoolText_A_True sta z_H
	jsr PrintString
	jsr NewLine
	LDA #True
	rts
.falsehood:
	load16 z_HL,BoolText_A_False
	jsr PrintString
	jsr NewLine
	LDA #False
	rts
 
Func_B:
	;input: Y = value to check. 0 = false, nonzero = true.
	;output: 0 if false, 255 if true. Also prints the truth value to the screen.
	;USAGE: LDY val JSR Func_B
	TYA
	BEQ .falsehood	;return false
	load16 z_HL,BoolText_B_True
	jsr PrintString
	jsr NewLine
	LDA #True
	rts
.falsehood:
	load16 z_HL,BoolText_B_False
	jsr PrintString
	jsr NewLine
	LDA #False
	rts	
 
 
Func_A_and_B:
	;input: 
	;	z_B = input for Func_A
	;	z_C = input for Func_B
	;output:
		;0 if false, 255 if true
	LDA z_B
	jsr Func_A
	BEQ .falsehood
		LDY z_C
		jsr Func_B
		BEQ .falsehood
			;true
			load16 z_HL,BoolText_A_and_B_True
			jsr PrintString
			jsr NewLine
			LDA #True
			rts
.falsehood:
	load16 z_HL,BoolText_A_and_B_False
	jsr PrintString
	jsr NewLine
	LDA #False
	rts
 
Func_A_or_B:
	;input: 
	;	z_B = input for Func_A
	;	z_C = input for Func_B
	;output:
		;0 if false, 255 if true
	LDA z_B
	jsr Func_A
	BNE .truth
		LDY z_C
		jsr Func_B
		BNE .truth
			;false
			load16 z_HL,BoolText_A_or_B_False
			jsr PrintString
			LDA #False
			rts
.truth:
	load16 z_HL,BoolText_A_or_B_True
	jsr PrintString
	LDA #True
	rts
 
 
BoolText_A_True:
	db "A IS TRUE",0
BoolText_A_False:
	db "A IS FALSE",0
BoolText_B_True:
	db "B IS TRUE",0
BoolText_B_False:
	db "B IS FALSE",0
 
BoolText_A_and_B_True:
	db "A AND B IS TRUE",0
BoolText_A_and_B_False:
	db "A AND B IS FALSE",0
BoolText_A_or_B_True:
	db "A OR B IS TRUE",0
BoolText_A_or_B_False:
	db "A OR B IS FALSE",0