Description
Part A: Global Variables and Separate Compilation
A LIFO stack data structure can be implemented using an array, as shown in the following C program:
#include <stdio.h>
#include <stdlib.h>
#define STACKSIZE 5
#define FALSE 0
#define TRUE 1
/* Function Prototypes */ void push(int value); int pop(); int stackFull(); int stackEmpty(); void display();
/* Global Variables */ int stack[STACKSIZE]; int top = -1;
int main() {
int operation, value;
do {
system(“clear”);
printf(“### Stack Operations ### “);
printf(“Press 1 – Push, 2 – Pop, 3 – Display, 4 – Exit “); printf(“Your option? “); scanf(“%d”, &operation); switch (operation) { case 1:
printf(” Enter the positive integer value to be pushed: “); scanf(“%d”, &value); push(value); break; case 2:
value = pop(); if (value != -1)
printf(” Popped value is %d “, value); break; case 3: display(); break; case 4:
printf(” Terminating program “); exit(0); default: printf(” Invalid option! Try again. “); break;
}
printf(” Press the return key to continue . . . “); getchar(); getchar();
} while (operation != 4);
return 0;
}
void push(int value)
{
if (stackFull())
printf(” Stack overflow! Cannot push value onto stack. “); else {
stack[++top] = value;
}
}
int pop() {
register int value;
if (stackEmpty()) {
printf(” Stack underflow! Cannot pop an empty stack. “); return (-1); } else { value = stack[top]; top–; return value;
}
}
int stackFull()
{
if (top == STACKSIZE – 1) return TRUE; else
return FALSE;
}
int stackEmpty()
{
if (top == -1) return TRUE; else
return FALSE;
}
void display()
{
register int i;
if (stackEmpty()) printf(” Empty stack “); else {
printf(” Current stack contents: “); for (i = top; i >= 0; i–) { printf(” %d”, stack[i]); if (i == top) {
printf(” <– top of stack”);
}
printf(” “);
}
}
}
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 printf() library routine. Be sure to handle the global variables and format strings in the appropriate way. Input will come from standard input. 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.
Part B: External Pointer Arrays and Command-Line Arguments
Given the following declarations in C:
./a5b 12 25
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. Your program should exit, printing this error message, if the user does not supply two command-line arguments:
usage: a5b mm dd
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
Submit the following:
Computing Machinery I
Assignment 5 Grading
Student:__________________________________________
Part A:
Correct use of external variable(s)
4 ______
push() function in assembly
4 ______
pop() function in assembly
4 ______
stackFull() function in assembly
4 ______
stackEmpty() function in assembly
4 ______
display() function in assembly
8 ______
Linking of separate source code modules
2 ______
Correct manipulation of stack
Part B:
4 ______
Command line arguments
4 ______
Correct use of external pointer arrays
4 ______
Calls to library functions
2 ______
Range checking
3 ______
Correct output
4 ______
2 Scripts showing I/O
4 ______
Complete documentation and commenting
4 ______
Design quality
4 ______
Total 63 ______ _____%
Reviews
There are no reviews yet.