Question
Translate C code into Pep/9 Assembly Language : #stack-based calculator #include #include #include #define STACKSIZE 6 int stack[STACKSIZE]; int stackpointer; void push(int N) { if
Translate C code into Pep/9 Assembly Language: #stack-based calculator
#include#include #include
#define STACKSIZE 6
int stack[STACKSIZE]; int stackpointer;
void push(int N) {
if (stackpointer == STACKSIZE-1) printf("Error: stack overflow ");
else { stackpointer++;stack[stackpointer] = N;}
}
void poptop() {
if (stackpointer<0) printf("Error: stack empty "); else stackpointer--;
}
void printtop() {
if (stackpointer>-1) printf("%d ",stack[stackpointer]); else printf("Error: stack empty ");
}
void clearstack() {
stackpointer=-1; }
void showstack() {
int i=stackpointer;
while (i>=0) { printf("%d ",stack[i]); i--;} }
void add() {
if (stackpointer<1) printf("Error: not enough operands for operation "); else
{ stack[stackpointer-1]=stack[stackpointer-1]+stack[stackpointer]; stackpointer--;
} }
void subtract() {
if (stackpointer<1) printf("Error: not enough operands for operation "); else
{ stack[stackpointer-1]=stack[stackpointer-1]-stack[stackpointer]; stackpointer--;
} }
void multiply() {
if (stackpointer<1) printf("Error: not enough operands for operation "); else
{ stack[stackpointer-1]=stack[stackpointer-1]*stack[stackpointer]; stackpointer--;
} }
void divide() {
if (stackpointer<1) printf("Error: not enough operands for operation "); else if (stack[stackpointer] == 0) printf("Error: attempt to divide by 0 "); else {
}
stack[stackpointer-1]=stack[stackpointer-1]/stack[stackpointer];
stackpointer--; }
int power (int a, int b) {
// returns a to the power b int result=a, i; for (i=b; i>1; i--) result*=a; return result;
}
void exponentiate() {
if (stackpointer<1) printf("Error: not enough operands for operation "); else if (stack[stackpointer] < 0) printf("Error: negative exponent power "); else {
}
stack[stackpointer-1]=power(stack[stackpointer-1],stack[stackpointer]);
stackpointer--; }
int main() {
char linein[20]; int go_on=1, number;
stackpointer = -1;
while (go_on) {
printf(": "); scanf("%s",linein); number=atoi(linein);
if (number!=0) push(number); else
{ if (linein[0]=='q') go_on=0; else if (linein[0]=='=') printtop(); else if (linein[0]=='p') poptop(); else if (linein[0]=='c') clearstack(); else if (linein[0]=='d') showstack(); else if (linein[0]=='+') add(); else if (linein[0]=='-') subtract(); else if (linein[0]=='*') multiply(); else if (linein[0]=='/') divide(); else if (linein[0]=='^') exponentiate(); else printf("Error: unrecognized command ");
} }
printf("Goodbye ");
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started