Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago