Answer the question
In order to leave comments, you need to log in
Code generation for conditionals?
Good day, the task is to write a generator for conditional expressions. To do this, I drove all these expressions into reverse Polish notation, that is, the expression: 7 > 6 and 6 < 3 in this entry will be: 7 6 > 6 3 < and , which is quite normal and corresponds to priorities. But how to program it? in general, that is the idea, but there are inconsistencies.
It was I who planned to use the stack in assembler and put the result of the operation there in the form of zero and one, that is, before reaching end, the stack will have 0 1, after which end itself will take 0 and 1 and also return the result (in this case 0), after the logical expression is completed, we look at the stack and if it contains 1, then we execute the branch of the true condition, if it is false, then, respectively, the branch of the false condition.
I planned to implement this using labels
, something like: zero
:
push $0
one:
push $1 ?
(you need to understand that this code will be written by the generator) or maybe there are simpler algorithms for solving this problem?
Answer the question
In order to leave comments, you need to log in
The Polish notation approach is possible if only expressions are needed. Regarding "How to go back?":
1. If you store a string with tokens in Polish notation in some array and process them in a loop, then after push $0/ push $1 you can make an unconditional jump to the beginning of the processing loop.
2. Use call, the return address will be pushed onto the stack and you will return to the place of the call on ret. But in this case, it will not be possible to use the hardware (push, pop) stack without additional perversions. Have an array under another stack for your data. Google "stack on array" if you don't know what I mean. This seems like a complication, but chances are you'll want to use the stack for something else later. Do not immediately reserve it for one purpose.
3. Instead of conditional jumps, you can try to load the result of comparisons from the flag register directly.
pushf ; Скопировать регистр флагов...
pop ax ; ...в регистр AX
Before the phrase "... we look at the stack and if it contains 1, then we execute the branch of the true condition, if it is false, then, accordingly, the branch of the false condition" everything is absolutely true. And then I do not understand the course of your thoughts, sorry ... Explain in other words? What is your end goal?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question