Making a Programming Language From Scratch/Simple Expressions
An expression includes all forms of commands that require usage of the ALU excepting logic functions . They also include function calls but these will be covered later. In this chapter we deal with the conversion of simple expressions or expressions excluding parenthesis and function calls or any sort of referencing.
We all know the four basic arithmetic operations that is +,-,*,/. Together they combine to form expressions such as :
However assembly language does not recognize such expressions, but it only recognizes binary commands such as:
Also it does not recognize precedence. It just executes commands as they are presented in a sequential order. Thus we have to order theses commands by ourselves.
Also the results are always stored in a certain location known as a register. More specifically it is stored in the Eax register.
There are the following instructions for each operation on integers. For operations on char replace eax by al:
Mov Eax,[operand1] Imul [operand2]
Cdq (sign extension, mandatory but only once in the program) Idiv [operand1],[operand2]
Cdq Idiv [operand1],[operand2] Mov Eax,edx
Following are the corresponding instructions for floats.
Fld [operand2] Fstp [operand1]
Fld [operand1] Fadd [operand2]
Fld [operand1] Fsub [operand2]
Fld [operand1] Fmul [operand2]
Fld [operand1] Fdiv [operand2]
There is no modulus for float variables.
Algorithm For ConversionEdit
1. While character not ; or *,/,%. increment index 1.1 If character be ; goto part2 1.2 get operand before operator and after 1.3 depending on operator use the appropriate instructions as given above. 1.4 remove the operands and operator from the line 1.5 if next operator be add or sub replace by var[NUM] where NUM is an int and incremented. Write var[NUM] as a variable of type of expression. Assign eax to var[NUM]. 1.6 else replace by eax. 2. Repeat step 1
For char replace eax by al and for float assign st(1) to var[NUM].
1. While character not ; or + or - increment index 1.1 If character be ; end process 1.2 get operand before and after the operator 1.3 depending on operator use the appropriate instruction as given above (Follow steps 1.4 to 1.6) 2. Repeat step 1