Starting from:

$30

CS224-Lab 2 Solved

Write MIPS assembly language programs as described below.

 

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"

 

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.

 

2.    Generating machine instructions
) 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



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

 

1. 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

 

 

2. bubbleSort: 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.

 

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.

 

4. 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.

 

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.

 

 

6. monitor: 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

More products