Home / utk / cs130 / fa17 / lab11 / calc.c
Directory Listing
calc.c
float.c
writeup.pdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//calc.c
//Calculator lab for COSC130
//Stephen Marz
//4 Sep 2017

//DO NOT MODIFY THIS CODE!
//Compile with calc.S
//aarch64-linux-gnu-gcc -o calc calc.c calc.S
#include <stdio.h>

//The following are the prototypes that you'll need to use
//Parameter 1 goes into register x0
//Parameter 2 goes into register x1
int Add(int left, int right);
int Sub(int left, int right);
int Mul(int left, int right);
int Div(int left, int right);
int ShiftLeft(int num, int amount);
int ShiftRight(int num, int amount);
int GetGlobal(int index);
void SetGlobal(int index, int value);

int main()
{
	int result;
	int left;
	int right;
	char command[5];

	do {
		printf("Enter command: ");
		if (scanf("%5s", command) <= 0) {
			printf("\n");
			return 0;
		}
		switch (command[0]) {
			case '+':
				scanf("%d %d", &left, &right);
				printf("%d + %d = %d\n", left, right, Add(left, right));
				break;
			case '-':
				scanf("%d %d", &left, &right);
				printf("%d - %d = %d\n", left, right, Sub(left, right));
				break;
			case '*':
				scanf("%d %d", &left, &right);
				printf("%d * %d = %d\n", left, right, Mul(left, right));
				break;
			case '/':
				scanf("%d %d", &left, &right);
				printf("%d / %d = %d\n", left, right, Div(left, right));
				break;
			case 'l':
				scanf("%d %d", &left, &right);
				printf("%d << %d = %d\n", left, right, ShiftLeft(left, right));
				break;
			case 'r':
				scanf("%d %d", &left, &right);
				printf("%d >> %d = %d\n", left, right, ShiftRight(left, right));
				break;
			case 'g':
				scanf("%d", &left);
				printf("Global %d = 0x%x\n", left, GetGlobal(left));
				break;
			case 's':
				scanf("%d %d", &left, &right);
				SetGlobal(left, right);
				printf("Set global %d to 0x%x\n", left, right);
				break;
      case 'h':
        printf("\n  Commands:\n    + A B    Add A and B\n    - A B    Subtract B from A\n    * A B    Multiply A and B\n    / A B    Divide A by B\n    l A B    Shift A to the left B times\n    r A B    Shift A to the right B times\n    g A      Get the value of the global A\n    s A B    Set the value of the global A to B\n    q        Quit\n    h        Print this help\n");
        break;
			case 'q':
				return 0;
			default:
				printf("Unknown command '%c' %d\n", command[0], (int)command[0]);
				break;
		}

	} while (1);

	return 0;
}