CPSC355- Assignment5-Global Variables and Separate Compilation

40.00 $

Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: . zip solution files instantly, after Payment


5/5 - (1 vote)


Part A:  Global Variables and Separate Compilation

Reverse Polish (or postfix) notation can be used to represent arithmetic expressions. For example, the infix expression

(1 – 2) * (4 + 5) =

is the following using reverse Polish notation:

1 2 – 4 5 + * =

Hewlett-Packard calculators often use this form of entry. The following is a C program that emulates such a calculator:

#include <stdio.h>

#include <stdlib.h>

/*  Constants  */

#define MAXOP   20

#define NUMBER  ‘0’

#define TOOBIG  ‘9’

/*  Function prototypes  */

int push(int f);

int pop();

void clear();

int getop(char *s, int lim);

int getch();

void ungetch(int c);

int main()


int type;

char s[MAXOP];

int op2;

while ((type = getop(s, MAXOP)) != EOF) {

switch (type) {

case NUMBER:



case ‘+’:

push(pop() + pop());


case ‘*’:

push(pop() * pop());


case ‘-‘:

op2 = pop();

push(pop() – op2);


case ‘/’:

op2 = pop();

if (op2 != 0)

push(pop() / op2);


printf(“zero divisor popped\n”);


case ‘=’:

printf(“\n%d\n\n”, push(pop()));


case ‘c’:



case TOOBIG:

printf(“%.20s … is too long\n”, s);



printf(“unknown command %c\n”, type);





return 0;




#define MAXVAL  100


int sp = 0;

int val[MAXVAL];


int push(int f)


if (sp < MAXVAL)

return val[sp++] = f;

else {

printf(“error: stack full\n”);


return 0;




int pop()


if (sp > 0)

return val[–sp];

else {

printf(“error: stack empty\n”);


return 0;




void clear()


sp = 0;



int getop(char *s, int lim)


int i, c;


while ((c = getch()) == ‘ ‘ || c == ‘\t’ || c == ‘\n’)


if (c < ‘0’ || c > ‘9’)

return c;

s[0] = c;

for (i = 1; (c = getchar()) >= ‘0’ && c <= ‘9’; i++)

if (i < lim)

s[i] = c;


if (i < lim) {


s[i] = ‘\0’;

return NUMBER;

} else {

while (c != ‘\n’ && c != EOF)

c = getchar();

s[lim-1] = ‘\0’;

return TOOBIG;




#define BUFSIZE   100


char buf[BUFSIZE];

int bufp = 0;

int getch()


return bufp > 0 ? buf[–bufp] : getchar();


void ungetch(int c)


if (bufp > BUFSIZE)

printf(“ungetch: too many characters\n”);


buf[bufp++] = c;


Translate all functions except main() into ARMv8 assembly language, and put them into a separate assembly source code file called a5a.asm. These functions will be called from the main() function given above, which will be in its own C source code file called a5aMain.c. Also move the global variables into a5a.asm. Your assembly functions will call the library routines printf() and getchar(). Be sure to handle the global variables and format strings in the appropriate way. Input will come from standard input; the program is terminated by typing control-d. Run the program to show that it is working as expected, capturing its output using the script UNIX command, and name the output file script1.txt. Use a variety of input expressions to show that your program is calculating correctly.

Part B:  External Pointer Arrays and Command-Line Arguments

Given the following declaration in C:

char *month[] = {“January”, “February”, “March”, “April”, “May”,

“June”, “July”, “August”, “September”, “October”,

“November”, “December”};

create an ARMv8 assembly language program to accept as command line arguments three strings representing a date in the format mm dd yyyy. Your program will print the date with the name of month as well as the correct suffix. For example:

./a5b 9 11 1990

September 11th, 1990

Be sure to use the proper suffix for the day of the month. For example, one should distinguish the 11th from the 1st, 21st, and 31st. Don’t forget the comma after the day. Your program should exit, printing this error message, if the user does not supply three command-line arguments:

usage: a5b mm dd yyyy

You will need to call atoi() to convert strings to numbers, and printf() to produce the output. Be sure to do range checking for the day, month, and year. Name your source code file a5b.asm. Run your program three times with different input to illustrate that it works; capture the output using the script UNIX command. Name the output file script2.txt.

New Skills need for this Assignment:

  • Understanding and use of external variables in assembly
  • Separate compilation
  • Calling assembly functions from main()
  • Calling library functions from assembly routines
  • External arrays of pointers
  • Command line arguments



  • Assignment-5-submission-h7netj.zip