[SOLVED] CMSC 216 Exercise 4

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (2 votes)

Context: Use of Assembly in Practice

Assembly language is used in several key situations. First, when a machine fifirst starts up, the code that reads

in the operating system is written in assembly. You might call this the “boot loader”. Second, when software

needs to interact with hardware (eg, IO adaptors), it uses special instructions (eg, in, out) or special registers

that aren’t exposed to C code. Third, “hand tuned” assembly is used when a routine needs to be optimized for

size, speed, or to ensure specifific timing. Only extremely important or frequently used code gets this treatment,

but on small microcontrollers without much memory for instructions, optimization for size is common.

In practice, assembly routines are called from C, but we won’t do that here (because our testing platforms

currently don’t have a C compiler that generates MIPS assembly).

IMPORTANT: We will look at your code. You will lose most of the points if you use a C-compiler to generate

MIPS code, or if you do not follow conventions on stack accessing and function calling (see section 7).

4

Academic integrity statement

Please carefully read the academic honesty section of the course syllabus. We take academic integrity matters

seriously. Please do not post assignment solutions online (e.g., Chegg, github) where others can see your

work. Posting code online can lead to an academic case where you will be reported to the Offifice of Student

Conduct.

5

Starter fifiles

This description is in the 216public exercise_descriptions directory. Copy the directory assembly_basic from

the 216public exercises directory to your 216 directory. The .submit fifile for this exercise is in that folder.

6

Specififications

You will implement three functions in MIPS assembly. Their prototypes, in C syntax, are given below.

  • int max(int x, int y): Parameters x and y are passed in registers. Returns the max of x and y in a

register. Eg, max(8, 9) returns 9.

  • int mash(int x, int y): Parameters x and y are passed in the stack. Returns the value of 10*x + y in

a register. Eg, mash(8, 9) returns 89. Assume inputs do not cause overflflow.

  • int strlen(char* str): Parameter str is passed in a register. Assume that str points to a nul-terminated

string. Returns the length of the string in a register.

1We supply template fifiles max.s, mash.s and strlen.s, one for each function. We also supply drivers for these

functions, in fifiles max_driver.s, mash_driver.s and strlen_driver.s. Consult the drivers to determine the

precise locations of arguments and return values, ie, which registers and/or where in stack.

The key reference documentation for the SPIM instruction set is HP_AppA.pdf, pages 48–72 (also available in

the class page resources).

7

Important

  1. To run spim and qtspim in a shell in Grace, you must fifirst run the command “module load spim” in

that shell. To avoid typing this for each new shell, add a line with this command to your ~/.cshrc.mine

fifile (and then run “source ~/.cshrc.mine” or logout and login).

  1. Your code must be written in the template fifiles max.s, mash.s and strlen.s.
  2. Labels representing the functions are already included in the template fifiles. You may need additional

labels for branch targets. Do not have any label starting with “main”. (Each driver has a main function.)

  1. Do not remove the existing comments in the template fifiles. Add comments to your code as you see fifit.
  2. Do not modify the driver fifiles.
  3. To test your function, concatenate the driver fifile and your fifile and execute that resulting fifile. For exam

ple, do

cat max_driver.s max.s > max_prog.s

and execute fifile max_prog.s in qtspim or in command-line spim. The latter is illustrated below (see

max_test1); the fifirst three lines are typed in, the last line is output.

% spim -file max_prog

2

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.