首页 > > 详细

辅导CS3012 Translate Assembly Language辅导data structure编程

CFIFO queueASM。

Part A: Global Variables and Separate Compilation

A FIFO queue data structure can be implemented using an array, as shown in the following C program:

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include lt;stdio.hgt;
#include lt;stdlib.hgt;

#define QUEUESIZE 8
#define MODMASK 0x7
#define FALSE 0
#define TRUE 1

/* Function Prototypes */
void enqueue(int value);
int dequeue();
int queueFull();
int queueEmpty();
void display();

/* Global Variables */
int queue[QUEUESIZE];
int head = -1;
int tail = -1;

int main()
#123;

int operation, value;

do #123;
system("clear");
printf("### Queue Operations ###\n\n");
printf("Press 1 - Enqueue, 2 - Dequeue, 3 - Display, 4 - Exit\n");
printf("Your option? ");
scanf("%d", amp;operation);

switch (operation) #123;
case 1:
printf("\nEnter the positive integer value to be enqueued: ");
scanf("%d", amp;value);
enqueue(value);
break;
case 2:
value = dequeue();
if (value != -1)
printf("\nDequeued value is %d\n", value);
break;
case 3:
display();
break;
case 4:
printf("\nTerminating program\n");
exit(0);
default:
printf("\nInvalid option! Try again.\n");
break;
#125;
printf("\nPress the return key to continue . . . ");
getchar();
getchar();
#125; while (operation != 4);

return 0;
#125;

void enqueue(int value)
#123;

if (queueFull()) #123;
printf("\nQueue overflow! Cannot enqueue into a full queue.\n");
return;
#125;

if (queueEmpty()) #123;
head = tail = 0;
#125; else #123;
tail = ++tail amp; MODMASK;
#125;
queue[tail] = value;
#125;

int dequeue()
#123;

register int value;

if (queueEmpty()) #123;
printf("\nQueue underflow! Cannot dequeue from an empty queue.\n");
return (-1);
#125;

value = queue[head];
if (head == tail) #123;
head = tail = -1;
#125; else #123;
head = ++head amp; MODMASK;
#125;
return value;
#125;

int queueFull()
#123;

if (((tail + 1) amp; MODMASK) == head)
return TRUE;
else
return FALSE;
#125;

int queueEmpty()
#123;

if (head == -1)
return TRUE;
else
return FALSE;
#125;

void display()
#123;

register int i, j, count;

if (queueEmpty()) #123;
printf("\nEmpty queue\n");
return;
#125;

count = tail - head + 1;
if (count lt;= 0)
count += QUEUESIZE;

printf("\nCurrent queue contents:\n");
i = head;
for (j = 0; j lt; count; j++) #123;
printf(" %d", queue[i]);
if (i == head) #123;
printf(" lt;-- head of queue");
#125;
if (i == tail) #123;
printf(" lt;-- tail of queue");
#125;
printf("\n");
i = ++i amp; MODMASK;
#125;
#125;

 

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:

1
2
3
4
char *month[] = #123;"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"#125;;
char *season[] = #123;“Winter”, “Spring”, “Summer”, “Fall”#125;;

 

create an ARMv8 assembly language program to accept as command line arguments two strings representing a date in the format mm dd. Your program will print the name of month, the day (with the appropriate suffix), and the season for this date. For example:

./a5b 12 25 December 25th is Winter 

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 

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 and month. Assume that Winter ranges from December 21 to March 20, Spring from March 21 to June 20, Summer from June 21 to September 20, and Fall from September 21 to December 20. 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

Submit the following:

  1. Your source code and 2 scripts via electronic submission. Use the Assignment 5 Dropbox Folder in D2L to submit electronically. Your TA will assemble and run your programs to test them. Name your files a5aMain.c and a5a.asm for Part A, and a5b.asm for Part B, and the scripts as script1.txt and script2.txt.
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!