Difference between revisions of "Code Generation/Duplication of Floating Point Numbers on the Stack"

From Wiki**3

< Code Generation
(Compiling and Running)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''NOTE: ''' this code was necessary before the addition of the DDUP instruction to the Postfix mnemonics set.
 
 
 
The following pure-Postfix program illustrates duplication of double-precision floating point numbers on the stack.
 
The following pure-Postfix program illustrates duplication of double-precision floating point numbers on the stack.
  
 
The C equivalent would be (roughly):
 
The C equivalent would be (roughly):
  
<c>
+
<source lang="c">
 
int main() {
 
int main() {
 
   extern void printd(double);
 
   extern void printd(double);
Line 13: Line 11:
 
   return 0;
 
   return 0;
 
}
 
}
</c>
+
</source>
  
 
Note that this program's code does not necessarily correspond to any particular code generator output.
 
Note that this program's code does not necessarily correspond to any particular code generator output.
 
+
{{CollapsedCode|Postfix code|
<asm>
+
<source lang="asm">
 
;;-- generation of the "_main" function
 
;;-- generation of the "_main" function
 
TEXT
 
TEXT
 
ALIGN
 
ALIGN
GLOBL _main, FUNC
+
GLOBAL _main, FUNC
 
LABEL _main
 
LABEL _main
 
ENTER 16  ; d@-8 and e@-16
 
ENTER 16  ; d@-8 and e@-16
Line 29: Line 27:
 
ALIGN
 
ALIGN
 
LABEL _L123
 
LABEL _L123
DOUBLE 3.3e-2
+
SDOUBLE 3.3e-2
  
 
;;-- load literal onto stack
 
;;-- load literal onto stack
 
TEXT
 
TEXT
 
ADDR _L123
 
ADDR _L123
DLOAD
+
LDDOUBLE
  
 
;;-- to perform the first assignment (to "e"), duplicate the value on the stack
 
;;-- to perform the first assignment (to "e"), duplicate the value on the stack
DDUP
+
DUP64
  
 
LOCAL -16  ; write value to "e"
 
LOCAL -16  ; write value to "e"
DSTORE
+
STDOUBLE
  
 
;;-- to perform the second assignment (to "d"), duplicate the value on the stack
 
;;-- to perform the second assignment (to "d"), duplicate the value on the stack
DDUP
+
DUP64
  
 
LOCAL -8  ; write value to "d"
 
LOCAL -8  ; write value to "d"
DSTORE
+
STDOUBLE
  
 
;;-- the assignments are an instruction: trash the value left on the stack
 
;;-- the assignments are an instruction: trash the value left on the stack
Line 53: Line 51:
 
;;-- now to perform the add operation
 
;;-- now to perform the add operation
 
LOCAL -8
 
LOCAL -8
DLOAD   ; load "d"
+
LDDOUBLE   ; load "d"
 
LOCAL -16
 
LOCAL -16
DLOAD ; load "e"
+
LDDOUBLE ; load "e"
  
 
DADD  ; leaves result on the stack
 
DADD  ; leaves result on the stack
Line 65: Line 63:
  
 
INT 0
 
INT 0
POP
+
STFVAL32
 
LEAVE
 
LEAVE
 
RET
 
RET
</asm>
+
</source>
 
+
}}
 
== Compiling and Running ==
 
== Compiling and Running ==
  
Assuming that the code above is in file dbl.pf, the following commands would compile and run the program:
+
Assuming that the code above is in file dbl.pf, the following commands ([[Compiladores/Projecto de Compiladores/Compiladores Exemplo|pf2asm]]) would compile and run the program:
  
* pf2asm dbl.pf
+
pf2asm dbl.pf
* yasm -felf dbl.asm
+
yasm -felf32 dbl.asm
* ld -o dbl dbl.o -lrts
+
ld -m elf_i386 -o dbl dbl.o -lrts
* ./dbl
+
./dbl
  
[[category:Compilers]]
+
[[category:Compiladores]]
[[category:Teaching]]
+
[[category:Ensino]]

Latest revision as of 13:36, 11 February 2020

The following pure-Postfix program illustrates duplication of double-precision floating point numbers on the stack.

The C equivalent would be (roughly):

int main() {
  extern void printd(double);
  double d, e;
  d = e = 3.3e-2;
  printd(d + e);
  return 0;
}

Note that this program's code does not necessarily correspond to any particular code generator output.

Postfix code
;;-- generation of the "_main" function
TEXT
ALIGN
GLOBAL _main, FUNC
LABEL _main
ENTER 16   ; d@-8 and e@-16

;;-- put double literal in RODATA
RODATA
ALIGN
LABEL _L123
SDOUBLE 3.3e-2

;;-- load literal onto stack
TEXT
ADDR _L123
LDDOUBLE

;;-- to perform the first assignment (to "e"), duplicate the value on the stack
DUP64

LOCAL -16  ; write value to "e"
STDOUBLE

;;-- to perform the second assignment (to "d"), duplicate the value on the stack
DUP64

LOCAL -8  ; write value to "d"
STDOUBLE

;;-- the assignments are an instruction: trash the value left on the stack
TRASH 8

;;-- now to perform the add operation
LOCAL -8
LDDOUBLE   ; load "d"
LOCAL -16
LDDOUBLE ; load "e"

DADD   ; leaves result on the stack

EXTERN printd
CALL printd

TRASH 8  ; argument value no longer needed

INT 0
STFVAL32
LEAVE
RET

Compiling and Running

Assuming that the code above is in file dbl.pf, the following commands (pf2asm) would compile and run the program:

pf2asm dbl.pf
yasm -felf32 dbl.asm
ld -m elf_i386 -o dbl dbl.o -lrts
./dbl