Do not "optimize" the assembler. Give me the simple yet inefficient assembler that the compiler would return.
int b(int j) { int i; i = 10; while (i > 0) { j = j + i * i - 3; i--; } return j; }
int f2(int *x) { int j; if (x == NULL) return 48; j = *x * 2; return j; }
int b(int i, int j) { if (j <= 0) return i+1; return a(i, j-1)+1; } int a(int i, int j) { if (i <= 0) return j+1; return b(i-1, j)+b(i-1, j-1); } main(int argc, char **argv) { int i; if (argc != 3) { exit(1); } i = a(atoi(argv[1]), atoi(argv[2])); }
UNIX> a.out 0 0 UNIX> a.out 1 2 UNIX> a.out 2 1
UNIX> a.out 2 2Assume that the state of the stack and registers when main is first called looks as follows.
Note, I'm happy with arrows instead of numeric values.
Stack registers |----------------| |-----------------------| | | | | r0 | | | | r1 | ..... | | | r2 | unused | | | r3 | unused | | | r4 | unused | /-------- | | sp | unused | <------------------ | | fp | old fp | | main: | pc | old pc | |-----------------------| | argc = 3 | | argv |--\ /------ | argv[0] |<-/ | /---- | argv[1] | | | /-- | argv[2] | \-|-|-> |'a'|'.'|'o'|'u' | | | |'t'| 0 | 0 | 0 | \-|-> |'2'| 0 | 0 | 0 | \-> |'2'| 0 | 0 | 0 | |--------------- |
int a(int i, int j, int *k) { if (i == 0) { *k = 15; return j } else { return j; } } int b(int i) { int m; m = i+25; return a(i); } main(int argc, char **argv) { int i; i = b(atoi(argv[1])); }
UNIX> a.out 0 UNIX> a.out 1