Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic

Posts about Z80 programming that do not relate to a particular computer
Post Reply
User avatar
akuyou
Posts: 563
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic

Post by akuyou » Tue Apr 23, 2019 12:22 pm



www.chibiakumas.com/z80/index.php#Lesson3

We used a Jump to effect a loop last time, but sometimes we'll need to jump to different places depending on a value.

Also, lets take a look at how to do some everyday maths in 8 bit... and finally, we'll use a simple basic program to act as a 'frontend' to our assembly

Lets get straight into coding!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
Gee-k
Posts: 28
Joined: Sat May 04, 2019 9:35 am
Location: Scotland
Contact:

Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic

Post by Gee-k » Mon Oct 07, 2019 3:46 pm

Hi,

I was getting a bit confised because of the use of a b and c being used in BASIC and asm.
So i've written this out and changed the BASIC to VAL1 VAL2 and FUNCT instead of a,b and c.

I've also written out how i've interperited the asm code.
Have I got it correct? I'm not too sure about the MathDivAgain section.

BASIC

Code: Select all

5 cls
10 INPUT "Val1";val1
20 INPUT "Val2";Val2
30 INPUT "0=Add,1=Sub,2=Mult,3=Div";Funct
40 poke (&9000),funct
50 poke (&9001),Val1
60 poke (&9002),Val2
70 Call &8000
80 print peek(&9003)
ASM

Code: Select all

org &8000
	ld a,(&9000)		; load funct into a
	ld bc,(&9001)		; load Val1 c, Val2 into b (because bc is a 16bit register it will load 9001 & 9002)
	
	cp 0			; compares a (&9000) with 0
	jp z,MathAdd 		; if previous statement is true then jump to MathAdd else next
	cp 1 			; compares a (&9000) with 1
	jp z,MathSub 		; if previous statement is true then jump to MathSub else next
	cp 2 			;compares a (&9000) with 2
	jp z,MathMult 		; if previous statement is true then jump to MathMult else next
	cp 3 			;compares a (&9000) with 3
	jp z,MathDiv 		; if previous statement is true then jump to MathDiv else next

	ld a,0 			; If we get here a was none of the above. could change this to 'xor a' to save a byte

SaveResult:			
	ld (&9003),a		; saves a into &9003 to be read by BASIC as answer
	ret			; returns us to basic

MathAdd:
	ld a,c			; loads Val1 (&9001) into a
	add b			; adds b (Val2) to a (Val1)
jr SaveResult			; jumps relative (saves a byte because it's so close in memory) to SaveResult


MathSub:
	ld a,c			; loads Val1 (&9001) into a
	sub b			; subtracts b (Val2) from a (val1)
jr SaveResult			; jumps relative (saves a byte because it's so close in memory) to SaveResult

MathMult:
	ld a,b			; loads Val2 (&9002) into a
	cp 0			; compares to 0
	jr z,SaveResult		; if previous statement is true then jump to SaveResult as multiplying by 0 else next
	ld a,0			; return a to 0
MathMultAgain:
	add c			; add c to a
	djnz MathMultAgain	; decrement b (Val2/&9002) by 1 then jump to MathMultAgain if b is not 0
jr SaveResult			; jump to save result if previous statement returned 0

MathDiv:
	ld a,c			; loads Val1 (&9001) into a
	cp 0			; compare a with 0
	jr z,saveResult		; jump to Save result if previous statement is true else next
	ld d,0			; return d to 0
MathDivAgain:
	sub b			; subtract b (Val2 / $9002) from a (Val1 / &9001)
	inc d			; increment d by 1
	jp nc,MathDivAgain	; jump if there is no carry, i.e. a is more than 0
	dec d			; if previous statement was true then take 1 away from d as it will have incremented 1 too far
	ld a,d			; loads d into a
jr SaveResult			; jump to save result
You don't fail, You learn how it's not done.
https://www.gee-k.net : Where I blog about my random geeky goings on.

User avatar
akuyou
Posts: 563
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic

Post by akuyou » Tue Oct 08, 2019 3:06 am

Yes, that all looks correct to me!
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
Gee-k
Posts: 28
Joined: Sat May 04, 2019 9:35 am
Location: Scotland
Contact:

Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic

Post by Gee-k » Tue Oct 08, 2019 1:00 pm

Thanks for checking that over. I'll continue the videos and hopefully I understand enough to start making my own programs eventually.
You don't fail, You learn how it's not done.
https://www.gee-k.net : Where I blog about my random geeky goings on.

Post Reply

Return to “General Z80 Programming”