Line 1: | Line 1: | ||
− | + | 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> | <asm> | ||
+ | ;;-- generation of _main function | ||
+ | TEXT | ||
+ | ALIGN | ||
+ | GLOBL _main, FUNC | ||
+ | LABEL _main | ||
+ | ENTER 16 ; d@-8 and e@-16 | ||
+ | |||
+ | ;;-- put double literal in RODATA | ||
RODATA | RODATA | ||
ALIGN | ALIGN | ||
Line 7: | Line 29: | ||
DOUBLE 3.3e-2 | DOUBLE 3.3e-2 | ||
+ | ;;-- load literal onto stack | ||
TEXT | TEXT | ||
− | + | ADDR _L123 | |
− | + | LOAD2 | |
− | |||
− | |||
− | + | ;;-- to perform the first assignment (to "e"), duplicate the value on the stack | |
+ | SP | ||
LOAD2 | LOAD2 | ||
+ | LOCA -16 ; write value to "e" | ||
+ | |||
+ | ;;-- to perform the second assignment (to "d"), duplicate the value on the stack | ||
SP | SP | ||
LOAD2 | LOAD2 | ||
− | DADD | + | LOCA -8 ; write value to "d" |
+ | |||
+ | ;;-- the assignments are an instruction: trash the value left on the stack | ||
+ | TRASH 8 | ||
+ | |||
+ | ;;-- now to perform the add operation | ||
+ | LOCV -8 ; load "d" | ||
+ | LOCV -16 ; load "e" | ||
+ | DADD ; leaves result on the stack | ||
EXTRN printd | EXTRN printd | ||
CALL printd | CALL printd | ||
− | TRASH 8 | + | TRASH 8 ; argument value no longer needed |
INT 0 | INT 0 | ||
Line 31: | Line 64: | ||
RET | RET | ||
</asm> | </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 | ||
[[category:Compilers]] | [[category:Compilers]] | ||
[[category:Teaching]] | [[category:Teaching]] |
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>
TEXT ALIGN GLOBL _main, FUNC LABEL _main ENTER 16 ; d@-8 and e@-16
RODATA ALIGN LABEL _L123 DOUBLE 3.3e-2
TEXT ADDR _L123 LOAD2
SP LOAD2
LOCA -16 ; write value to "e"
SP LOAD2
LOCA -8 ; write value to "d"
TRASH 8
LOCV -8 ; load "d" LOCV -16 ; load "e" DADD ; leaves result on the stack
EXTRN printd CALL printd
TRASH 8 ; argument value no longer needed
INT 0 POP LEAVE RET </asm>
Assuming that the code above is in file dbl.pf, the following commands would compile and run the program: