Jump to content
Site Stability Read more... ×
  • Advertisement
  • entries
  • comments
  • views

Expression Evaluation in Z80 Assembly

Sign in to follow this  


The expression evaluators I've written in the past have been memory hungry and complex. Reading the BBC BASIC ROM user's guide introduced me to the concept of expression evaluation using top-down analysis, which only uses a small amount of constant RAM and the stack.

I took some time out over the weekend to write an expression evaluator in Z80 assembly using this technique. It can take an expression in the form of a NUL-terminated string, like this:

.db "(-8>>2)+ceil(pi())+200E-2**sqrt(abs((~(2&4)>>>(30^sin(rad(90))))-(10>?1)))",0

and produce a single answer (or an error!) in the form of a floating-point number. The source code and some notes can be downloaded here.

I initially wrote a simple evaluator using 32-bit integers. I supported the operations the 8-bit Z80 could do relatively easily (addition, subtraction, shifts and logical operations) and got as far as 32-bit multiplication before deciding to use BBC BASIC's floating-point maths package instead. The downside is that BBC BASIC has to be installed (the program searches for the application and calls its FPP routine).

I'm not sure if the technique used is obvious (I'd never thought of it) but it works well enough and the Z80 code should be easy to follow - someone may find it useful. [smile]
Sign in to follow this  


Recommended Comments

Original post by benryves
[...]I'm not sure if the technique used is obvious (I'd never thought of it)[...]
Have you read Compilers: Principles, Techniques, and Tools? There is a new edition out that is probably far better, but I think you'd find even the old edition most helpful in writing compilers. It's one of the few books I've seen that covers the theory well - far too many skip theory almost entirely and end up being little more than code dumps between paragraphs of implementation details.

Share this comment

Link to comment
No, I've never read the book not studied compiler design or techniques so thanks for the recommendation - that sounds like a good book! [smile]

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!