CS3421 Problem Set 2

Assembly Lanaguage

Do problem 3.1 from the textbook and the following:

  1. Write a segment of code that sets $t0 to 0 if the sum of all the $s registers is equal to 0, to 1 otherwise.

  2. Write a segment of code that puts the largest value of those in $s0, $s1 and $s2 into $t0.

  3. Write a segment of code that puts into $t3 the sum of the elements of the 100 element array of integers whose starting address is in $t5. Use a loop, not 100 loads and adds. Do not change $t5.

  4. Write a segment of code that reads two integers from the user (no need to prompt for input) and prints the sum of the two numbers.

  5. Write a procedure (function actually) that receives two integers in $a0 and $a1, and returns their product in $v0. Do not use the multiply instruction - instead write a loop that adds repeatedly. Assume both numbers are postive and don't worry about the product being too large to fit in 32 bits.

  6. Write a segment of code that finds the bit position of the highest order 1 bit in $s7 and puts that position in $t2. Assume $t2 is not equal to 0 (i.e., there is a one bit in there somewhere). Remember that bit position 31 is the high order (leftmost) bit and bit position 0 is the low order (rightmost) bit.

  7. Write a function that receives a 32 bit value in $a0 and returns the upper 16 bits of that number in the lower 16 bits of $v0 (set the upper 16 bits of $v0 to 0), and returns the lower 16 bits of that number in $v1 (set the upper 16 bits of $v1 to 0).

  8. Write a recursive function that computes n-factorial (i.e., n*(n-1)*...*1). Use the standard parameter passing and function conventions.

  9. Write a function that returns (in $v0) the length of the null-terminated string who's address is passed in $a0. The length is the number of actual characters in the string NOT counting the null terminator.

  10. Write a procedure that copies the null-terminated string who's address is in $a0 to memory starting at the address in $a1.

  11. Write a function that compares two null-terminated strings for equality, returning 1 if equal, 0 if not equal. Two stings are equal if they are the same length and have exactly the same characters in the same positions. Assume the addresses of the two strings are passed to the function, and use the standard parameter passing and function conventions.

  12. Show the real instructions (one or more) that correspond to the following pseudo-instructions: