CS计算机代考程序代写 CALIFORNIA STATE POLYTECHNIC UNIVERSITY

CALIFORNIA STATE POLYTECHNIC UNIVERSITY

CALIFORNIA STATE POLYTECHNIC UNIVERSITY
Computer Science Department
CS 2640 (3) T. Nguyen/f21

PROJECT: 3
DUE DATE: October 29, 2021

Description:
Write a program that implements an array to store input lines of text. The program will then output the array
constructed. Procedure register calling conventions must be used in this project.

The following subprograms are required to be implemented:
1. int strlen(cstring source) : returns the length of source (‘\0’ not counted)
2. cstring strdup(cstring source) : returns the address of the duplicated source, must call strlen and malloc
3. address malloc(int size): Allocates a block of size bytes of dynamic memory, returns the address to the

beginning of the block. (syscall 9)
4. int main():

a. create an array named lines that can handle up to 10 cstrings.
b. prompts the user for lines of text (up to 32 characters per line), must use gets
c. stop asking for input when empty line is detected, or the array is full
d. stores these lines in the array lines, the lines are to be created using strdup
e. outputs the array lines, must use puts

5. void gets(cstring dest, int num): get string from the keyboard (syscall 8), reads characters from the keyboard
and stores them as a C-string into dest until (num-1) characters have been read or a newline is reached,
whichever happens first. The newline is *only* stored as part of the string if the number of characters
entered is less than the num – 1.

6. void puts(cstring source) : output source to the terminal (syscall 4)

Required I/O:
Lines by F. Last

Enter text? Line 1
Enter text? Line 2
Enter text? Enter

Line 1
Line 2

Turn in:
1. Submit the source code to:

/user/tvnguyen7/cs2640-002/username-lines.s

Notes:
1. The following information is required in the beginning of every source file.
#
# Name: Last, First
# Project: #
# Due: date
# Course: cs-2640-02-f21
#
# Description:
# A brief description of the project.
#

Hints:

MAXLINES = 10
LINELEN = 32
lines: array of MAXLINES addresses
inbuf: .space LINELEN # up to LINELEN byte, see syscall 8 for reference.

main:
do {
gets(inbuf, LINELEN);
if inbuf [0] == ‘\n’)
break;
s = strdup(inbuf);
add inbuf to lines
} while (still have room in the array lines);
output all the lines

void gets(cstring cs, int size)
get line into cs using syscall 8

void puts(cstring cs)
output cs

int strlen (cstring cs)
len = 0;
while (cs[len] != ‘\0’)
len++;
return len;

cstring strdup (cstring cs)
cstring d = malloc(strlen(cs) + 1);
do {
d[i] = cs[i];
} while (cs[i] != ‘\0’);

return d;

address malloc (int size)
allocate size dynamic bytes using syscall 9
return address

syscall 8 – read cstring (string with \0 termination)
$a0 – buffer (.space)
$a1 – length

Read from the keyboard until Enter \n and store the characters in buffer + \n + \0
If only Enter, buffer will have \n \0
If length – 1 characters are entered, the \n will not be part of the string. For example, if you $a1 is 5,
then after you enter the 4th character, the syscall will return without waiting for the Enter key and the
content of the buffer will be the 4 characters + \0

syscall 9 – malloc
$a0 – number of bytes
$v0 – address of the newly allocated space.

Allocate memory on the heap (dynamic memory)
$a0 needs to be multiple of 4 ((n + 3) & 0xffffffffc). For cstring, don’t forget space for \0

California State Polytechnic University