(→Postfix Code) |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== The Original Code == | == The Original Code == | ||
− | + | Consider the following C function: | |
− | <c> | + | <source lang="c"> |
int main() { | int main() { | ||
int n = 45, i = 0, j = 1, *seg = &i; | int n = 45, i = 0, j = 1, *seg = &i; | ||
Line 11: | Line 11: | ||
return 0; | return 0; | ||
} | } | ||
− | </ | + | </source> |
== Postfix Code == | == Postfix Code == | ||
− | <asm> | + | The Postfix code for the above function is as follows: |
+ | {{CollapsedCode|Postfix code| | ||
+ | <source lang="asm"> | ||
TEXT | TEXT | ||
ALIGN | ALIGN | ||
− | + | GLOBAL "main",FUNC | |
LABEL "main" | LABEL "main" | ||
ENTER 16 ; n@-4, i@-8, j@-12, seg@-16 | ENTER 16 ; n@-4, i@-8, j@-12, seg@-16 | ||
Line 34: | Line 36: | ||
;-- while cycle: | ;-- while cycle: | ||
LABEL "while" | LABEL "while" | ||
− | + | LOCAL -4 ; &n | |
− | + | LDINT | |
+ | DUP32 ; n (because of n--) | ||
INT 1 | INT 1 | ||
SUB ; n-1 | SUB ; n-1 | ||
− | + | LOCAL -4 | |
+ | STINT ; n = n-1 | ||
INT 0 | INT 0 | ||
GT ; n > 0 (n before decrement) | GT ; n > 0 (n before decrement) | ||
JZ "endwhile" | JZ "endwhile" | ||
− | + | LOCAL -8 ; &i | |
− | + | LDINT ; i | |
+ | LOCAL -12 ; &j | ||
+ | LDINT ; j | ||
ADD ; i+j | ADD ; i+j | ||
− | + | DUP32 ; same value (for assignment) | |
− | + | LOCAL -16 ; &seg | |
− | + | LDINT ; seg | |
+ | STINT ; *seg = i+j | ||
CALL "print" ; we assume that "print" does not return any value | CALL "print" ; we assume that "print" does not return any value | ||
TRASH 4 ; trash argument (int) | TRASH 4 ; trash argument (int) | ||
;-- compute trenary operator's value | ;-- compute trenary operator's value | ||
− | + | LOCAL -16 ; &seg | |
+ | LDINT ; seg | ||
LOCAL -8 ; &i | LOCAL -8 ; &i | ||
EQ ; seg == &i | EQ ; seg == &i | ||
Line 58: | Line 66: | ||
LOCAL -12 ; &j | LOCAL -12 ; &j | ||
JMP "end3" ; jump to end of trenary operator | JMP "end3" ; jump to end of trenary operator | ||
+ | |||
LABEL "false3" | LABEL "false3" | ||
LOCAL -8 ; &i | LOCAL -8 ; &i | ||
+ | |||
LABEL "end3" | LABEL "end3" | ||
− | + | DUP32 ; trenary operator's value (duplicated because of assignment) | |
− | + | LOCAL -16 | |
+ | STINT ; seg = (seg == &i) ? &j : &i | ||
TRASH 4 ; trash assignment's value, since we used it as an instruction | TRASH 4 ; trash assignment's value, since we used it as an instruction | ||
JMP "while" ; restart while cycle | JMP "while" ; restart while cycle | ||
+ | |||
LABEL "endwhile" | LABEL "endwhile" | ||
;-- rest of function | ;-- rest of function | ||
INT 0 | INT 0 | ||
− | + | STFVAL32 ; return value is 0 | |
LEAVE | LEAVE | ||
RET | RET | ||
− | </ | + | </source> |
+ | }} | ||
− | [[category: | + | [[category:Compiladores]] |
− | [[category: | + | [[category:Ensino]] |
Consider the following C function:
int main() {
int n = 45, i = 0, j = 1, *seg = &i;
while (n-- > 0) {
print(*seg = i + j);
seg = (seg == &i) ? &j : &i;
}
return 0;
}
The Postfix code for the above function is as follows:
[Expand] Postfix code |
---|