Difference between revisions of "Code Generation/Exercise 5"

From Wiki**3

< Code Generation
(Compiling and Running)
(Compiling and Running)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
Consider the following C function:
 
Consider the following C function:
  
<c>
+
<source lang="c">
 
int gcd(int a, int b) {
 
int gcd(int a, int b) {
 
   static int temp = 0;
 
   static int temp = 0;
Line 15: Line 15:
 
   return b;
 
   return b;
 
}
 
}
</c>
+
</source>
  
 
== Postfix Code ==
 
== Postfix Code ==
  
 
The Postfix code for the above function is as follows:  
 
The Postfix code for the above function is as follows:  
 +
{{CollapsedCode|Postfix code|
 +
<source lang="asm">
  
<asm>
+
</source>
 
+
}}
</asm>
 
  
 
== Compiling and Running ==
 
== Compiling and Running ==
  
To compile the Postfix code directly, [[pf2asm]] can be used:
+
To compile the Postfix code directly, [[Compiladores/Projecto de Compiladores/Compiladores Exemplo|pf2asm]] can be used:
  
 
  pf2asm gcd.pf
 
  pf2asm gcd.pf

Latest revision as of 18:52, 6 May 2019

The Original Code

Consider the following C function:

int gcd(int a, int b) {
  static int temp = 0;
  if (temp) temp = swap(&a, &b);
  while (1) {
    a %= b;
    if (a == 0) break;
    b %= a;
    if (b == 0) return a;
  }
  return b;
}

Postfix Code

The Postfix code for the above function is as follows:

Postfix code

Compiling and Running

To compile the Postfix code directly, pf2asm can be used:

pf2asm gcd.pf
yasm -felf gcd.asm