(→Postfix Code) |
(→Postfix Code) |
||
Line 41: | Line 41: | ||
; start while body | ; start while body | ||
+ | |||
+ | ; prepare arguments for calling printf | ||
+ | |||
LOCV -4 ; second printf arg: read ix | LOCV -4 ; second printf arg: read ix | ||
Line 86: | Line 89: | ||
POP | POP | ||
− | ; release stack frame | + | LEAVE ; release stack frame |
− | + | RET ; return control to caller | |
− | |||
− | ; return control to caller | ||
− | |||
</asm> | </asm> |
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>
The Postfix code for the above function is as follows:
<asm> EXTRN printf
TEXT ALIGN GLOBL printlist, FUNC LABEL printlist
ENTER 4 ; ix@-4 lo@+8 hi@+12
LOCV +8 ; read lo LOCA -4 ; write to ix
ALIGN LABEL whiletest LOCV -4 ; read ix LOCV +12 ; read hi LT JZ whileend
LOCV -4 ; second printf arg: read ix
RODATA ALIGN LABEL strlit STR "%d\n"
TEXT ADDR strlit ; first printf arg: string literal address
CALL printf
TRASH 8
PUSH
TRASH 4
LOCV -4 ; read ix DUP INT 1 ADD LOCA -4 ; ix = ix + 1
JMP whiletest
ALIGN LABEL whileend
LOCV -4 ; read ix
POP
LEAVE ; release stack frame RET ; return control to caller
</asm>
To compile the Postfix code directly, pf2asm can be used: