WinAli is a model assembler for Windows. It emulates a real CPU and is meant to help students learning and understanding the Assembly language.
German Wikipedia article: http://de.wikipedia.org/wiki/WinAli (there is no english article at the moment)
In case you want to test your produced output: Link to Google Drive (the program itself is in German but is easy to use even if you don't understand German)
Some information about WinAli (data and syntax)Data types:
The only data type in WinAli is a 2 byte long "Integer" which is in fact a Smallint or short. So you don't have to take care of correct types. This is also rounded when dividing.Variables and constants:
Variables are defined like this after the commands:
name DS F
- name is obviously the name of the variable
- DS is the keyword for a new variable
- F defines the length of the variable in bits; this is zero-based and hexadecimal so F = 16 bits
Constants are very similar:
name DC value
- name is simply the name of the constant
- DC is the keyword for a new constant
- value is the value assigned to that constant e.g.
two DC '2'
- You don't need to specify constants at the end of the code. You can also write them inline with the
LDA(load) operation e.g.
LDA 0,'2'. This loads "2" into register 0 (the accumulator).
The code is aligned into 4 columns which are separated by a space or tab:
label command params comment
- label is used for the
b labelcommand (
goto label) and is often skipped.
- command one of the commands listed below.
- params one or two parameters with at least one register and if needed another register, variable, stack or address; these parameters are separated by a comma (
- comments (optional) a comment prefixed with
*until the end of the line.
Small code reference:
There are more commands but for this challenge you only need those:
INI AStores the next user input into variable
OUTI AOutputs the value the variable
Ais holding. You can't use a register here for some reason.
LDA 0,ALoads the value of
Ainto register 0
STA 0,AStores the value of register 0 into the variable
ADD 0,AAdds the value of
Ato the value of register 0
SUB 0,AMultiplies the value of
Awith the value of register 0
DIV 0,ADivides the value of
Aby the value of register 0
EOJDefines the end of the job i.e. executable code. Variable and constant declarations begin after this mark.
Not explicitly needed for this challenge:
program START 0Sets the first address to be executed to 0 i.e. the next line (
programis just the name of the program but is needed for the compiler)
END programEnds the program i.e. stop executing, even if other operations would follow
Write a program or script that takes a mathematical expression with constant numbers and variables and outputs the WinAli code that takes all variables as input and outputs the result of that expression.
- You don't need to output the
- Even though there are 16 registers defined in WinAli we only use register 0, which is the accumulator for all calculations
- You can't use two variables as parameters of a command. You have to save all your results into the accumulator (register 0)
- The input is always on the form of either
(a+b)/c, you decide
- You only need to take care of
+ - * / (ADD, SUB, MUL, DIV), so no complex calculations like roots, squares or even sinus
- Tip: Try to first extract simple expressions with only 2 arguments e.g.
- Tip: Your program is allowed to use additional (auxiliary) variables if you can't overwrite variables that are not used later in your code. Define them as
h1 DS F.
- Tip: Empty lines are useless. Try to avoid them. (You really don't need them)
Mathematical expression: x := (a+b)*c Expected output (empty lines are for easier reading): INI a INI b INI c LDA 0,a ADD 0,b MUL 0,c STA 0,a OUTI a EOJ a DS F b DS F c DS F
Your score is defined by the length of your code + the length of the output of the following expressions:
x:=a+b+c*c/d x:=(a+b)/c-d x:=a-b*c-d/e x:=2*a-b*c+d
- -10% if your code can handle unary minus in expressions.
In case of a tie, the answer that produces the shortest working WinAli code wins.
The solutions may not be hardcoded in any way. I will validate each answer with additional expressions to make sure your answer is correct.
MUL 0,-a? this is not possible because there are no negative addresses (
ais just a pointer to a value), but you can subtract the value from 0 to get the negative representation (
SUB 0,a) and then work with this negative value; you may even be able to store this in the original variable. \$\endgroup\$
x:=a/-(b+c). Since the input order is not specified I guess we have to use the same variables names in the WinAli code, is that the case? With those restrictions are there identifiers that are never on the input? For example can
h1be an input variable name? \$\endgroup\$