CS224 Lab 2-principles of using stack for savings registers Solved

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

Rate this product

. Understanding preliminary principles of using stack for saving s registers, passing arguments to and receiving results from subprograms, and dynamic storage allocation. 2. Code generation for branch, jump, load address and load word.You are obliged to read this document word by word and are responsible for the mistakes you make by not following the rules. Your programs should be reasonably documented and must have a neat syntax in terms of variable names and spacing. In all labs if it is not specified assume that inputs are always correct.

Summary

Part 1 Preliminary Report/Preliminary Design Report: Learning principles of writing subprograms. Involves converting octal number to decimal. Generating object code for beq, bne, j, la, lw instructions.

Part 2  (: Learning principles of writing subprograms. Involves dynamic storage allocation for arrays, getting the elements of an integer array from user, sorting numbers, finding the min, max and mode values, and providing a user interface.

  1. Please bring and drop your hardcopy (printed copy) of the preliminary work into the box(es) provided in the lab by 10:40 on Wednesday.

Please upload your programs of Part 1 (PRELIMINARY WORK) to the Unilica by 10:40 on Wednesday. Use filename StudentID_FirstName_LastName_SecNo_PRELIM_LabNo.txt Upload only the programs. Only a NOTEPAD FILE (txt file) is accepted. Your paper submission for preliminary work must match MOSS submission. Any format other than txt gets 0 points.

  1. To get credit for preliminary work you have to submit its hard copy and upload its txt version to unilica. No late submission will be accepted.

 

DUE DATE PART 2-5: (different for each section) YOUR LAB DAY

You have to demonstrate your lab work to the TA for grade by 12:15 in the morning lab and by 17:15 in the afternoon lab. Your TAs may give further instructions on this. If you wait idly and show your work last minute, 20 points may be taken off from your grade.

At the conclusion of the demo for getting your grade, you will upload your entire program work to the Unilica Assignment, for similarity testing by MOSS. See Part 6 below for details.

Part 1. Preliminary Work / Preliminary Design Report

You have to provide a neat presentation prepared by Word or a word processor with similar output quality such as Latex as you were asked in Lab 1. At the top of the paper on left provide the following information and staple all papers. In this part provide the program listings with proper identification (please make sure that this info is there for proper grading of your work, otherwise some points will be taken off).

CS224

Section No.: Enter your section no. here

Lab No.

Your Full Name/Bilkent ID

 

  1. 1Write MIPS assembly language programs as described below.

 

  1. a. convertToDec: Write a subprogram, called convertToDec, that receives the beginning address of an asciiz string that contains a number in base 8 in the form of a string, for example, like “20”, and returns its decimal (208= 1610) equivalent in register $v0. It assumes that the number passed is a legal octal number. Be sure that the program works for longer octal numbers as well.

 

A sketch of this convertToDec is as follows.

main:

la   $a0, octalNo

jal   convertToDec

# result comes in $v0

# stop execution here by syscall

convertToDec:

jr   $ra

.data

octalNo:   .asciiz “20”

 

  1. b.  interactWithUser: Write a subprogram, called interactWithUser, that asks the user enter an octal number in the form of a string, makes sure that it is a proper octal number if not it generates an error message and ensures that a proper input is received. It passes this address to the subprogram defined above convertToDec, gets the result from it, prints it, and returns the decimal value back to the caller, i.e. the main program. How to read a string: See MIPS system calls on the web to understand how to read a string or use MARS help menu.

 

Use the $s registers during the implementation of the above subprograms. Using $s registers means that you have to save them to stack and restore them back from stack.  Make sure that you also save/restore any other register that need to be saved.

 

  1. 2.  Generating machine instructions
    (Each instruction: 2 points) Give the object code in hexadecimal for the following branch (be, bne), jump (j) and load instructions. Note that the meaning of the code segment is insignificant.

… # some other instructions

again:            add  $t0, $t1, $t2

add  $t0, $t0, $t3

add  $t0, $t0, $t4

add  $t0, $t0, $t5

beq    $t0, $t6, next

bne  $t0, $t6, again

add  $t0, $t0, $t5

next:             j               again

la      $t0, array2

lw     $t1, array2

.data

array1:          .space   100

array2:          .word    10, 20, 30

 

Assume that the label again is located at memory location 00 40 00 A016 and the array array1 starts at memory location 10 01 00 0016. If you think that you do not have enough information to generate the code explain. See slide number 117 in the new Chap 6 slides of the textbook for the MIPS memory map (available at our unilica web site, Documents folder).

 

Part 2. Lab Work: Writing MIPS assembly language programs
(70 points)

In this part when needed you have to follow the conventions of professional MIPS programmers and use stack.

 

  1. 1. (10 points) readArray: Write a subprogram, called readArray, that asks the user the size of an integer array and gets the values of array members from the user in a loop. It returns the beginning address of the array in $v0, and array size in terms of number of integers in $v1.

Hint.

li $a0, 20 #allocate enough space for 5 integers

li $v0, 9 #syscall 9 (sbrk)

syscall

# beginning address of the allocated space is returned in $v0

 

 

  1. 2bubbleSort: Write a subprogram, called bubbleSort, that sorts an integer array in decreasing/descending order using the bubble sort algorithm and using the absolute values of the numbers stored. (When -5, 1, 4, 2 is sorted after sorting the array contains -5, 4, 2, 1.) The subprogram receives the beginning address of the array in $a0, and the array size in $a1. The array size can be 0 or more.

 

  1. 3) thirdMinThirdMax: Write a subprogram, called thirdMinThirdMax, that returns the third minimum and third maximum numbers of an integer array. For example, for the array {13, 5, 1, 3, 8, 7} 3th minimum is 5, and 3th maximum is 7. You may assume that the array has at least 3 elements. The input array may or may not be sorted. Use $a registers for passing parameters and use $v0 and $v1 to return the thirdMin and thirdMax values.

 

  1. 4. (10 points) mode: Write a subprogram, called mode, to return the mode (the most frequently appearing) value of a sorted array. If there are more than one candidate it returns the minimum of them. It receives array address and array size in $a0 and $a1 registers, respectively.
  2. 5. ) print: Write a subprogram, called print, to print the content of the array. Use $a registers for passing parameters and don’t forget that array size can be 0.
  3. 6monitor: Write a monitor program that provides a user interface to use the above subprograms in an interactive manner. The main program, the one that contains __start, provides a simple user interface that will use the above subprograms in the way that you imagine. A simple interface is enough if you like you may make it fancy.
  • lab2-pgyqmi.zip