#define STACKSIZE 1000 int stack[STACKSIZE]; int R0, R1, R2, R3, R4, R5, R6, R7; int SP=0, BP=0; void push(int element) { stack[++SP]=element; return; } int pop() { return(stack[SP--]); } int getint() { int a; scanf("%d",&a); return a; } void putint(int a) { printf("%d",a); return; } /* End of machine description */ void max() { push(BP); /* Dynamic link */ BP=SP; /* set base(BP) to new activation record */ push(R0); /* save registers */ push(R1); push(R2); push(R3); push(R4); push(R5); push(R6); push(R7); R0=stack[BP-1] > stack[BP-2]; if ( R0 ) goto L1; stack[BP-3]=stack[BP-2]; goto L3; L1: stack[BP-3]=stack[BP-1]; L3: R7=pop(); R6=pop(); R5=pop(); R4=pop(); R3=pop(); R2=pop(); R1=pop(); R0=pop(); BP=pop(); return; } void my_main() { /* STRUCTURE OF ACTIVATION RECORD on stack 1) space return value 2) parameters 3) dynamic link (address of BP) 4) space for local variables 5) save register values DURING PROCEDURE CALL CALLER'S RESPONSIBILITY 1) make space for return value 2) push parameters 3) call the procedure CALLEE'S RESPONSIBILITY 1) save dynamic link (address of BP) 2) set BP to new activation record (ie to SP) 3) make space for local variables 3) save registers on stack DURING RETURN FROM PROCEDURE CALLEE'S RESPONSIBILITY 1) restore registers 2) set BP to the activation record of calling procedure 3) return to the caller CALLER'S RESPONSIBILITY 1) restore SP to the location of containing return value */ push(BP); /* Dynamic link */ BP = SP; /* set base of current activation record */ SP = SP + 2; /* make space for local variables*/ push(R0); /* save registers */ push(R1); push(R2); push(R3); push(R4); push(R5); push(R6); push(R7); /* assign local variables */ stack[BP+1] = getint(); /* x_6 */ stack[BP+2] = getint(); /* y_7 */ SP=SP+1; /*make space for return value*/ /* t7 */ push(stack[BP+2]); /*push actual parameter*/ push(stack[BP+1]); max(); pop(); /*pop actual parameter*/ pop(); putint(stack[SP]); /*access the return value */ /* t7 */ SP=SP-1; R7=pop(); R6=pop(); R5=pop(); R4=pop(); R3=pop(); R2=pop(); R1=pop(); R0=pop(); SP=SP-2; /*restore SP*/ BP = pop(); /* restore base pointer */ return; } /* The driver main() { my_main(); } */