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

From Wiki**3

< Code Generation
Line 34: Line 34:
 
TEXT
 
TEXT
 
ADDR _L123
 
ADDR _L123
LOAD2
+
DLOAD
  
 
;;-- 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
SP
+
DDUP
LOAD2
 
  
 
LOCAL -16  ; write value to "e"
 
LOCAL -16  ; write value to "e"
STORE2
+
DSTORE
  
 
;;-- 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
SP
+
DDUP
LOAD2
 
  
 
LOCAL -8  ; write value to "d"
 
LOCAL -8  ; write value to "d"
STORE2
+
DSTORE
  
 
;;-- 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 55: Line 53:
 
;;-- now to perform the add operation
 
;;-- now to perform the add operation
 
LOCAL -8
 
LOCAL -8
LOAD2   ; load "d"
+
DLOAD   ; load "d"
 
LOCAL -16
 
LOCAL -16
LOAD2 ; load "e"
+
DLOAD ; load "e"
  
 
DADD  ; leaves result on the stack
 
DADD  ; leaves result on the stack
  
EXTRN printd
+
EXTERN printd
 
CALL printd
 
CALL printd
  

Revision as of 17:20, 13 May 2013

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 C equivalent would be (roughly):

<c> int main() {

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

} </c>

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

<asm>

-- generation of the "_main" function

TEXT ALIGN GLOBL _main, FUNC LABEL _main ENTER 16  ; d@-8 and e@-16

-- put double literal in RODATA

RODATA ALIGN LABEL _L123 DOUBLE 3.3e-2

-- load literal onto stack

TEXT ADDR _L123 DLOAD

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

DDUP

LOCAL -16  ; write value to "e" DSTORE

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

DDUP

LOCAL -8  ; write value to "d" DSTORE

-- the assignments are an instruction
trash the value left on the stack

TRASH 8

-- now to perform the add operation

LOCAL -8 DLOAD  ; load "d" LOCAL -16 DLOAD ; load "e"

DADD  ; leaves result on the stack

EXTERN printd CALL printd

TRASH 8  ; argument value no longer needed

INT 0 POP LEAVE RET </asm>

Compiling and Running

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

  • pf2asm dbl.pf
  • yasm -felf dbl.asm
  • ld -o dbl dbl.o -lrts
  • ./dbl