One pass x86 compiler - lyriarte/Cm7b5 GitHub Wiki

A one pass compiler generates assembly "on the fly" code during the syntax analysis phase.

x86 32 bit assembly

Register layout

<-----------  EAX  ------------->
                <-----  AX  ---->
+---------------+-------+-------+
|               |  AH   |  AL   |
+---------------+-------+-------+
31              15       7      0

Register usage

  • All purpose: EAX, EBX, ECX, EDX, ESI, EDI
  • Stack base: EBP, pointer: ESP

Example program

Assembly code to dump 32 bit register in hex mode.

Compiling expressions

Like for a RPN machine, the generated code pops the second operand, then the first operand, does the operation and pushes the result. The second operand is stored in register ebx. The first operand and the operation result are computed in eax then pushed on the stack.

Example expression:

5 - 3

Generated assembly code:

push 5
push 3
pop ebx
pop eax
sub eax, ebx
push eax

Branches and labels

Loops

Begin:
^      +-----+
|     /       \
|    /         \
|   | Condition |-- false --+
|    \         /            |
|     \       /             |
|      +-----+              |
|         |                 |
|         | true            |
|         v                 |
|   +-----------+           |
|   |           |           |
|   |   Bloc    |           |
|   |           |           |
|   +-----------+           |
|         |                 |
+---------+                 |
                            |
    End:   <----------------+

Conditions

       +-----+
      /       \
     /         \
    | Condition |-- false --+
     \         /            |
      \       /             |
       +-----+              |
          |                 |
          | true            |
          v                 |
    +-----------+           |
    |           |           |
    |   Bloc    |           |
    |           |           |
    +-----------+           |
          |                 |
+---------+                 |
|                           |
|   End:  +-----------------+
|         |
|         v
|   +-----------+
|   |           |
|   |   Bloc    |
|   |           |
|   +-----------+
|         |
v         v
Final:

Calling convention

+-------------------------------+
|           variable 2          | <--- EBP - 8
+-------------------------------+
|           variable 1          | <--- EBP - 4
+-------------------------------+
|            saved EBP          | <--- EBP
+-------------------------------+
|         return address        |
+-------------------------------+
|           argument 1          | <--- EBP + 8
+-------------------------------+
|           argument 2          | <--- EBP + 12
+-------------------------------+
|           argument 3          | <--- EBP + 16
+-------------------------------+