Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic
Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic
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
Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809
Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic
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
ASM
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)
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.
https://www.gee-k.net : Where I blog about my random geeky goings on.
Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic
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
Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809
Re: Video: Lesson 3 - 'Case Statement' , 8 bit basic Maths, Writing to Ram and Reading from basic
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.
https://www.gee-k.net : Where I blog about my random geeky goings on.