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!

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

Posted: Mon Oct 07, 2019 3:46 pm

by Gee-k

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.

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

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

Posted: Tue Oct 08, 2019 3:06 am

by akuyou

Yes, that all looks correct to me!

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

Posted: Tue Oct 08, 2019 1:00 pm

by Gee-k

Thanks for checking that over. I'll continue the videos and hopefully I understand enough to start making my own programs eventually.