Title: Computer Organization
1Computer Organization
- Project 3
- Prof. Jerry Breecher
- CSCI 140
- Spring 2002
2What You Will Do In This Lab.
- The purpose of this lab is to eventually get your
calculator program to the point where you can
utter something like - 3 5 234
- To do this requires that you be able to take in
all the input on one line, as an ASCII string,
and then parse that string in order to separate
out the different components or fields. - Your purpose this week is to do some of the
background work that will prepare you for this
parsing.
3What You Will Do In This Lab.
- So the overall assignment looks like this
- Week 1
- Write four subroutines (defined later) that will
be used as parsing tools later. - Use the testing code provided to ensure that your
code is working. - Week 2
- A. Use the code youve written, the code from
your original calculator, and other subroutines I
will provide, to build a parsing calculator.
4What You Will Do In This Lab.
- For this week, there are FOUR routines you are to
write. These are described in detail later in
this document, but for right now, here they are
briefly - strncpy( dest_string, src_string, num ) Move
num characters from src_string to
dest_string. - atoi( string ) Convert the ASCII representation
of an integer to a variable of type integer. - strchr( string, char ) Look for an instance of
char within the string string. - strpbrk( string, accept ) Look for ANY of the
characters in the string accept in the target
string string
5What You Will Do In This Lab.
- Where You Will Find Stuff
- Your life can be made easier by starting with the
file having path - /home/jbreecher/public/comp_org/labs/project3_you_
code.s - But then youll need to test your code. As
usually happens, the code for testing is longer
than the code youve written. It can be found
at - /home/jbreecher/public/comp_org/labs/project3_test
.s - Use your editor to combine the two pieces of
code. The test code contains the main so it
should probably go first.
6What You Will Do In This Lab.
- Groundrules
- Credit for Labs and Projects comes in two
flavors - Showing up for lab (at the appointed hour) gets
you credit for lab. - Doing the project (whether during the lab hour or
at some other time), and satisfying me that it
works gets you some of the project credit. - Showing properly documented code gets you the
remainder of the project credit. - You will get checkoff credit when we see that
it works and produces the results we know its
supposed to have. - Working independently
- Same as last week.
7SPIM Project 3
An Example Run
- Run the test program
- This program calls YOUR routines as a way of
testing your code. - This is a good challenge can you defeat my test
program!
Welcome to Project 3. This program checks your
code. Testing strncpy 88 88 88 88 88 88 88 88
88 88 88 88 88 88 88 88 Testing strchr First
String abcd Second String a Your value
-1 Expected value 268501287 Sorry - your
result was not correct Testing strpbrk First
String The Quick Brown Fox Second String
abcd Your value 268501302 Expected value
268501309 Sorry - your result was not
correct Testing atoi First String
123456 Your value 268501364 Expected
value 123456 Sorry - your result was not
correct All done. Goodby
8SPIM Project 4
- Write a calculator with the following properties
- Program is capable of implementing the following
operations - Add two or more numbers and print result
- Subtract two or more numbers and print result
- Multiple two or more numbers and print result
- Divide two or more numbers and print results
- Quit
- User Writes the operation
- Program prints out the answer.
- Program runs until quit operation is selected.
- Integer arithmetic only.
An Example Run
ltrungt This program was written by XXX Enter an
operation 3 7 - 4 Result is
6 Enter an operation -7 / 3 - 2 Result is
-4 Enter an operation quit Thank you
for playing.
9Routine Definitions
- STRCPY(3) Linux Programmer's Manual
STRCPY(3) - NAME
- strncpy - copy a string
- SYNOPSIS
- char strcpy(char dest, const char src)
- char strncpy(char dest, const char src,
size_t n) - DESCRIPTION
- The strcpy() function copies the
string pointed to by src (including the
terminating \0' character) to the array pointed
to by dest. The strings may not overlap, and the
destination string dest must be large enough to
receive the copy. - The strncpy() function is similar,
except that not more than n bytes of src are
copied. Thus, if there is no null byte among the
first n bytes of src, the result will not be
null-terminated. - In the case where the length of src is
less than that of n, the remainder of dest will
be padded with nulls. - RETURN VALUE
- The strcpy() and strncpy() functions
return a pointer to the destination string dest.
10Routine Definitions MIPS Implementation
-
-
-
- STRNCPY - Copy bytes from a source location to
a destination location - until N bytes have been copied or
until a ltNULgt has been - found. The ltNULgt is copied if it's within
the N bytes, - but otherwise the destination string
is not terminated. - In the case where the length of
source is less than that - of N, the remainder of destination is
padded with ltNULgts. -
- Here's the code in C
- i 0
- bzero( destination_string, N )G
- while ( source_bufferi '\0' i lt N )
-
- destination_stringi source_stringi
- i
-
-
11Routine Definitions
- ATOI(3) Linux Programmer's Manual
ATOI(3) - NAME
- atoi, - convert a string to an integer.
- SYNOPSIS
- int atoi(const char nptr)
-
- DESCRIPTION
- The atoi() function converts the initial
portion of the string pointed to by nptr to int.
- The string may begin with an arbitrary amount of
white space followed by a single optional '
or -' sign. - The remainder of the string is converted to
a long int value in the obvious manner, stopping
at the first character which is not a valid
digit. - RETURN VALUE
- The converted value. No error is detected.
12Routine Definitions MIPS Implementation
-
-
-
- ATOI - Convert an ASCII string to an integer.
-
- The ascii string has the following format
- 1. zero or more leading white-space (space or
tab) characters - 2. An optional or - sign
- 3. 1 or more digits in the range 0 - 9.
- 4. A non-digit terminates the numerical
value. - If there is no numerical value, then a
numerical 0 is returned -
- in a0 - address of string to be converted
- returned v0 contains the integer that was
converted, or o if - no conversion could be done.
- destroyed
-
-
- atoi
13Routine Definitions
- STRCHR(3) Linux Programmer's Manual
STRCHR(3) - NAME
- strchr, - locate character in string
- SYNOPSIS
- include ltstring.hgt
- char strchr(const char s, int c)
- DESCRIPTION
- The strchr() function returns a pointer to
the first occurrence of the character c in the
string s. -
- RETURN VALUE
- The strchr() function returns a pointer to
the matched character or NULL if the character is
not found.
14Routine Definitions MIPS Implementation
-
-
- STRCHR - Findout if the character 'c' exists in
the string 'matching'. - Search through the whole string
'matching' in order to find - the FIRST occurence of 'c'. If it exists,
return the - address where that character is. If
it does NOT exist, - return a ltNULgt.
-
- Here's the code in C
- i 0
- while ( matchingi ! '\0' matchingi
! 'c' ) -
- i
-
- if ( matchingi '\0' )
- return( NUL )
- else
- return( (matchingi )
-
15Routine Definitions
- STRPBRK(3) Linux Programmer's Manual
STRPBRK(3) - NAME
- strpbrk - search a string for any of a set
of characters - SYNOPSIS
- include ltstring.hgt
- char strpbrk(const char s, const char
accept) - DESCRIPTION
- The strpbrk() function locates the first
occurrence in the string s of any of the
characters in the string accept. - RETURN VALUE
- The strpbrk() function returns a pointer
to the character in s that matches one of the
characters in accept, or NULL if no such
character is found.
16Routine Definitions MIPS Implementation
-
-
-
- STRPBRK - search a string for any of a set of
characters -
- char strpbrk(const char s, const char
accept) -
- The strpbrk() function locates the
first occurrence - in the string s of any of the
characters in the string accept. - The strpbrk() function returns a
pointer to the - character in s that matches one of the
characters in - accept, or NULL if no such character
is found.
Here's the code in C i 0 while
( si ! '\0' strchr( si, accept ) NUL
) i if ( si '\0'
) return( NUL ) else
return( (si ) in a0 - address of
string 's' in which we're looking for a match
in a1 - address of the 'accept' string.
returned v0 contains the address of the match,
or ltNULgt if no match is found.
destroyed
strpbrk YOU WRITE THIS
CODE move v0,a0 jr ra