|
|
(21 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
| == The Original Code ==
| | #REDIRECT [[ist:Code Generation/Exercise 9]] |
| | |
| Consider the following C code:
| |
| | |
| <c>
| |
| static unsigned long a = 10;
| |
| static double *v;
| |
| extern void *malloc(unsigned long);
| |
| | |
| static double *mkvec(unsigned long n) {
| |
| if (n < 1) return (double *)0;
| |
| unsigned long s = sizeof(double);
| |
| double *v = (double *)malloc(n * s);
| |
| return v;
| |
| }
| |
| | |
| double *compute() {
| |
| v = mkvec(a * 4);
| |
| for (unsigned long i = 1; i < a; i++)
| |
| if (v[i] > v[0]) v[i] = 3 * i - 1;
| |
| return v;
| |
| }
| |
| </c>
| |
| | |
| Assume that the size of pointers, '''int''', and '''unsigned long''' is 32 bits and that the size of '''double''' is 64 bits.
| |
| | |
| == Postfix Code ==
| |
| | |
| The Postfix code for the above code is as follows:
| |
| | |
| <asm>
| |
| | |
| </asm>
| |
| | |
| == Compiling and Running ==
| |
| | |
| To compile the Postfix code directly, [[pf2asm]] can be used (assuming a 32-bit architecture):
| |
| | |
| * pf2asm compute.pf
| |
| * yasm -felf compute.asm
| |
| | |
| [[category:Compilers]]
| |
| [[category:Teaching]]
| |