Computer Science 281
Computer Organization

Denison
CS281 Homework 2


Homework 2

Book Exercises

From Patterson and Hennesey, Chapter 2:

    1. Exercise 2.3, all parts
    2. Exercise 2.4, all parts
    3. Exercise 2.5, all parts
    4. Exercise 2.6, all parts

Programs

Overall Asssembly Language Guidelines:

Program grading will not be based solely on whether or not the programs "works." It will also be based on the organization, structure, and documentation (in the form of program comments) for the submitted work.

Be sure and document your code well. Include:

  1. Your name in a comment block at the top of every source file.
  2. A high level description of the problem solved by the program.
  3. Descriptive comments for each functional block.
  4. Inline comments that guide the reader and relate the assembly language to the higher level C++ or algorithm being implemented.
  5. For functions/entry points defined in the text section, give the entry and exit assumptions, including a description of all parameters and return values.

Assembly Programs

  1. Write an assembly language program to compute:

    y = (a + b) * (a - b) / c;

    where a = 17, b = -3, and c = 3. The variables a, b, c, and y should all be defined in the data segment and by the end of the program, the result should be stored in memory in y and also should be written to the console with an appropriate message. Name the source file for this program compute.s.

  2. Write a short program that asks for your height in feet and inches and your weight in pounds. (Use three variables in the data section to store the information.) Have the program report your BMI (Body Mass Index). To calculate the BMI, first convert your height in feet and inches to your height in inches. Then, convert your height in inches to your height in meters by multiplying by 0.0254. Then, convert your weight in pounds into your mass in kilograms by dividing by 2.2. Finally, compute your BMI by dividing your mass in kilograms by the square of your height in meters. Name your source file calcBMI.s.

  3. Assume you want to get a multidigit decimal value from the user, but are limited to the input of characters and strings. (i.e. system calls 8 and 12 may be used, but not 5, 6, or 7). For an initial version of this program, you may also assume that you know the input will be _exactly_ 3 characters, representing a 3-digit decimal number. Write a program that inputs a three character (ascii) string of decimal digits and converts them into the correct-valued integer, and prints the integer out to the console. Name this program atoi.s.

  4. Write a MIPS assembly language program that adds together the first, last, and “[x, y]th ” elements of a two dimensional array of words (32 bits each) whose first coordinate is [0,0]. Store the result in the memory word labelled sum. The number of rows and columns, and x and y are given in main memory. Your program needs to work for any values you put in there. In other words, don’t treat them as constants but rather as “variables”.

    Assume the matrix is stored in row major order, that is, all the words in row 0 are stored sequentially before the words in row 1, etc. (This is in contrast to column major order in which all the words in column 0 would be stored sequentially before the words in column 1, etc.) You will need to do some multiplication to solve this problem. The real multiplication instruction (mult) is a little too complicated to introduce right now, so you can use a pseudoinstruction, which looks like
    mul rdest, rsrc1, rsrc2
    where rdest is the register that will contain the result, and rsrc1 and rsrc2 are registers containing the operands. Note that the product of two 32 bit words can be 64 bits long. The real multiplication instruction handles this by putting the result into two special 32 bit registers (more later). The pseudoinstruction, in addition to having a more
    familiar syntax, gives you only the least significant 32 bits of the product in a 32 bit wide register, which is enough for reasonably sized numbers. Use the following program skeleton, which contains declarations and skeleton code. The labels rows and cols point to the size of the matrix. The labels x and y point to the coordinates of the matrix element referred to above. This program should be named matrixsum.s.

# sum = matrix[0,0] + matrix[rows - 1,cols - 1] + matrix[x,y]
# Put your name and the date here.

        .data
rows:   .word 4                            # number of rows in the matrix
cols:   .word 7                            # number of columns in the matrix
matrix: .word 23, 12, 24, 82, 13, 17, 90   # the 4 X 7 matrix
        .word 10, 91, 63, 24, 12, 14, 33
        .word 99, 71, 60, 55, 11, 40, 81
        .word 17, 10, 82, 68, 38, 32, 10
sum:    .word 0                            # the sum
x:      .word 2                            # row of the element to add
y:      .word 5                            # column of the element to add

        .text

        # YOUR CODE GOES HERE

        li $v0, 10
        syscall                            # exit