Making a Programming Language From Scratch/Simple Expressions

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.

Basic Operations
We all know the four basic arithmetic operations that is +,-,*,/. Together they combine to form expressions such as : a*b+c

However assembly language does not recognize such expressions, but it only recognizes binary commands such as: add a,b mul b,c

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.

Instructions
There are the following instructions for each operation on integers. For operations on char replace eax by al:

Assignment Mov [operand1],[operand2]

Add add [operand1],[operand2]

Subtract sub [operand1],[operand2]

Multiplication Mov Eax,[operand1] Imul [operand2]

Division Cdq (sign extension, mandatory but only once in the program) Idiv [operand1],[operand2]

Modulus Cdq Idiv [operand1],[operand2] Mov Eax,edx

Increment Inc [operand]

Following are the corresponding instructions for floats.

Assignment Fld [operand2] Fstp [operand1]

Addition Fld [operand1] Fadd [operand2]

Subtraction Fld [operand1] Fsub [operand2]

Multiplication Fld [operand1] Fmul [operand2]

Division Fld [operand1] Fdiv [operand2]

There is no modulus for float variables.

Algorithm For Conversion
Part1 Multiplication,division,modulus

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].

Part2 Addition,subtraction

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