Difference between revisions of "Code Generation/Exercise 8"

From Wiki**3

< Code Generation
(Created page with "== The Original Code == Consider the following C function: <c> extern int printf(const char *format, ...); int printlist(int lo, int hi) { int ix = lo; while (ix < hi) { ...")
 
Line 1: Line 1:
== The Original Code ==
+
== The Original Code ==
  
Consider the following C function:
+
Consider the following C function:  
  
 
<c>
 
<c>
Line 13: Line 13:
 
   return ix;
 
   return ix;
 
}
 
}
</c>
+
</c>  
  
directivas: text data rodata bss align label extrn globl const str char id byte double
+
'''directivas:''' text data rodata bss align label extrn globl const str char id byte double  
  
acesso: addr addra addrv local loca locv load dload ldchr store dstore stchr alloc dup swap push pop int sp
+
'''acesso:''' addr addra addrv local loca locv load dload ldchr store dstore stchr alloc dup swap push pop int sp  
aritmética/lógica: add dadd sub dsub mul dmul div ddiv mod neg gt ge lt le eq ne cmp
 
  
bit a bit: rotl rotr shtl shtru shtrs and or not xor
+
'''aritmética/lógica:''' add dadd sub dsub mul dmul div ddiv mod neg gt ge lt le eq ne cmp
  
funções/saltos: call ret start enter leave trash jmp jz jnz branch leap
+
'''bit a bit:''' rotl rotr shtl shtru shtrs and or not xor
  
== Postfix Code ==
+
'''funções/saltos:''' call ret start enter leave trash jmp jz jnz branch leap
 +
 
 +
== Postfix Code ==
  
 
The Postfix code for the above function is as follows:  
 
The Postfix code for the above function is as follows:  
Line 30: Line 31:
 
<asm>
 
<asm>
  
</asm>
+
</asm>  
  
== Compiling and Running ==
+
== Compiling and Running ==
  
To compile the Postfix code directly, [[pf2asm]] can be used:
+
To compile the Postfix code directly, [[Pf2asm]] can be used:  
  
* pf2asm printlist.pf
+
*pf2asm printlist.pf  
* yasm -felf printlist.asm
+
*yasm -felf printlist.asm
  
[[category:Compilers]]
+
[[Category:Compilers]] [[Category:Teaching]]
[[category:Teaching]]
 

Revision as of 18:18, 2 June 2012

The Original Code

Consider the following C function:

<c> extern int printf(const char *format, ...); int printlist(int lo, int hi) {

 int ix = lo;
 while (ix < hi) {
   printf("%d\n", ix);
   ix++;
 }
 return ix;

} </c>

directivas: text data rodata bss align label extrn globl const str char id byte double

acesso: addr addra addrv local loca locv load dload ldchr store dstore stchr alloc dup swap push pop int sp

aritmética/lógica: add dadd sub dsub mul dmul div ddiv mod neg gt ge lt le eq ne cmp

bit a bit: rotl rotr shtl shtru shtrs and or not xor

funções/saltos: call ret start enter leave trash jmp jz jnz branch leap

Postfix Code

The Postfix code for the above function is as follows:

<asm>

</asm>

Compiling and Running

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

  • pf2asm printlist.pf
  • yasm -felf printlist.asm