Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How to compile arithmetic expressions to x86?

Name: Anonymous 2015-07-10 21:16

hi prog, say I want to take as given some variables on the stack x,y,z,w and compile expressions (x+y)*(z+w) to x86 asm.

first off the instructions take registers or pointers and well x86 is weird and has instructions like this:

add x, y ;x OR y must be a register, result in x
mul x ;multiplies eax with x, result is put into eax


so this would work:

mov eax, stack0
add eax, stack1
mov ebx, stack2
add ebx, stack3
mul ebx


but how would you make an algorithm to do this in general?

Name: expert !Up3JQchEFY 2015-07-15 5:59

>>12
It is my opinion that Three Address Code would be drastically overcomplicated in this situation.
This is because simple arithmetic expressions require no concept of looping or jumping, which would serve to only complicate this endeavor.
Surely a better approach would be utilizing Dijkstra's Shunting Yard algorithm1 to translate a given infix expressoan to a Postfix2 expression.
The advantages of this are obvious, of course, in that you could allocate registers3 and generate x86 Assembly4 efficiently in one pass over the postfix expression.

As an example, let us consider the infix expression, "(3 * 4) + 2".
The tokenization of this expression is displayed in figure A.
The translation of this expression to a linear stream of assembly instructions is not immediately clear in infix form, but if we are to translate it to a postfix expression, as in figure B, the (slightly naive) translation is simple and straightforward, as displayed in figure C.

I believe it is now adaquately obvious that translation to postfix is highly preferable to three address code. There is one alternative worth mentioning, however, which is building a tree5 from the infix expression, and generating assembly by recursively iterating through it. This is approximately equivalent in difficulty, but can make traversal and register allocation slightly more complex.

(
3
*
4
)
+
2

Figure A

3
4
*
2
+

Figure B

mov $3, %eax
mov $4, %ebx
mul %ebx
mov $2, %ebx
add %ebx, %eax
; The end result of the expression is now stored in %eax.

Figure C

References:
1: https://en.wikipedia.org/wiki/Shunting_yard_algorithm
2: https://en.wikipedia.org/wiki/Reverse_Polish_notation
3: https://en.wikipedia.org/wiki/Register_allocation
4: https://en.wikipedia.org/wiki/X86_instruction_listings
5: https://en.wikipedia.org/wiki/Parse_tree

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List