There is a program that translates the expression into the reverse Polish notation and then calculates the result. I need at the time of evaluation to translate the expression into the assembler code in the output.txt file. For example:

5 - 10 + 20 * 5 - 30 + 3 

assign R0, 20

assign R4, 5

mul R0, R4, R0

assign R4, 5

assign R8, 10

sub R4, R8, R4

add R4, R0, R0

assign R4, 30

sub R0, R4, R0

assing r4,3

add R0, R4, R0

How to implement such a task so that the program correctly distributes the expression to the registers and then writes it down?

  • Do you really want to write Fortran again? - gbg
  • I don’t have homework; I just need to generate the assembler code from the expression - Artyom
  • If you need to generate for ARM, this set of macros can help. - kisssko
  • Stack can I use? Is the number of registers limited? - Qwertiy ♦
  • yes you can use stl and registers 4 - Artyom

1 answer 1

Take a list of the registers of your processor. Take a variable. Filter the list, discarding those registers that

  • already busy
  • in which this variable cannot be put,
  • those registers with which you cannot perform the required operation

If there is a non-empty list, place the variable in the first of the registers.

If the list is empty, we return to the beginning and try to stack the variables in a different order.

  • He has no variables ... - Qwertiy ♦
  • if you can easily write sample code in c ++ generateCode (leftOperand, rightOperand, operation); in this function, you need to properly distribute the operands to the registers .. you need a code that checks which register to transfer the operands and do the operation - Artyom