# MSX Assembly Fibonacci

Page 1/2
| 2

I have been looking for Z80 assembly examples for MSX. The majority are either too simple or too complex. Could someone please show a simple way to print the fibonacci sequence (let's say until 1000) on the screen. If you can, please demonstrate on this environment:
https://8bitworkshop.com/v3.9.0/?platform=msx&file=helloworl...
Thank you!
Cheers!

The algorithm is just the sum of the two previous terms in the sequence...

My point is not the algorithm, it is easy, I can write it in C, python, etc... Linear, recursively, with memoization, etc... I just want a good example in MSX assembly. To learn how to use variables over 8-bits, to learn how MSX print them on screen, etc...

i.e.: in Python:
# --------------------------------------
# fibonacci linear:
# --------------------------------------
def fibo(n):
x = 0
y = 1

for i in range(0, n):
z = x + y
x = y
y = z

return x

# --------------------------------------
# fibonacci recursive
# --------------------------------------
def fibo_recursive(n):
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return fibo_recursive(n - 1) + fibo_recursive(n - 2)

# --------------------------------------
# implement memoization explicit
# --------------------------------------
fibonacci_cache = {}

def fibo_rec_cache(n):
# If we have cached the value, then return it.
if n in fibonacci_cache:
return fibonacci_cache[n]

if n == 1:
value = 1
elif n == 2:
value = 1
elif n > 2:
value = fibo_rec_cache(n - 1) + fibo_rec_cache(n - 2)

# Cache the value and return it.
fibonacci_cache[n] = value
return value

# --------------------------------------
# optimal memoization
# --------------------------------------
from functools import lru_cache

@lru_cache(maxsize = 1000) # default is 128
def super_rec_fibo(n):
# check if the input is a positive integer.
if type(n) != int:
raise TypeError("n must be a positive int")
if n < 1:
raise ValueError("n must be a positive int")

# compute nth term
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return super_rec_fibo(n - 1) + super_rec_fibo(n - 2)

I think printing the fibonacci sequence is not a good starting example for assembler in MSX. Notice that this example would involve converting numbers from binary to decimal for showing on the screen, scrolling the screen up, etc. In raw assembler, there are no functions to do any of that. For example, there is no such thing as "print" in assembler, nor the MSX BIOS, so, you have to do all of that from scratch), so, the example will have to include code to do all of that (e.g., copying the whole video name table one row up, to scroll the text when reaching the bottom of the screen, etc.)

I think that maybe simple games, like pong would be better examples (pixels are easier to draw than text in assembler! haha). Would these examples that come with the asMSX compiler be of use? https://github.com/Fubukimaru/asMSX/tree/master/code

I will check them out. Thanks! About the Fibonacci example, I got your point. Could be possible then, another example like getting two integers (16-bits / i.e.: 256) from user input, add them and show the results?

Thanks again Santiago!

Well, to say the truth there is CHPUT (a2h) on MSX BIOS to PUT CHaracters on screen. A must contain the ascii value of the character.
And POSIT (c6h) to select where on screen put the cursor before print. H=y L=x

Go to this link to see the MSX BIOS routines. The ones I've mentioned (and some other you will find useful) are in "console routines"

Thanks Thegeps!

Ah, yes, good point! There is CHPUT / POSIT, but I was thinking of the usual "print" that prints a line of text, one below the other, and then scrolls up upon reaching the bottom of the screen, like PRINT in BASIC, printf in C, etc.

And yeah, I think the read two numbers and write the result might be easier! There will be the reading numbers on character at a time, converting them to integers, and then the usual integer to to ascii for printing the result out. Definitively doable! I'll see if I can get some time tomorrow to give it a try and share

Page 1/2
| 2