11.07.2015 Views

ECS 50 Homework #4 - CS-CSIF

ECS 50 Homework #4 - CS-CSIF

ECS 50 Homework #4 - CS-CSIF

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>E<strong>CS</strong></strong> <strong>50</strong> <strong>Homework</strong> Assignment <strong>#4</strong> (90 points) Summer 2014Due: Monday, July 28 th . Written 4pm, Programs 11:59pm.File names: sort.asm, sorting2.c, sort3.asm (extra credit), mult.mpsWritten (30 points)Written from Chapter 11 (24 points): 1; 2 b, d, f, g; 3 b, e, g, h; 4 b, d; 5, 6, 8, 11, 13.MIPS Written: (6 points) What are the machine codes (in hexadecimal) for the instructions in the following MIPS codefragment?loop: addu $4, $8, $11lw $5, 16($sp)beq $8, 0, loopnopaddi $7, $9, -2j loopPrograms1) (25 points) Write an Intel assembly language integer sorting function for the nasm assembler that relies on insertionsort.Name: sort.asmWe will test your function by calling it from within the sorting1.c program available in ~ssdavis/<strong>50</strong>/p5. The functionwill receive two parameters: 1) The address of an array of int[25]; and 2) The number of items in the array. You shouldlook at the assembly code of sorting1.c, using gcc –masm=intel –S to determine the placement and data length ofthe parameters on the stack. Your function must be register neutral, with extensive comments similar to those inselsort.asm. Looking at selsort.asm, you will notice that the code is quite inefficient, with many registers beingunnecessarily re-copied. Your code must be efficient, and use registers instead of local variables wherever possible. Withfour general registers, as well as DI and SI, there is little need for memory storage. Besides the original parameters, yourcode should rely on only one local variable on the stack.To create your object file you will type: nasm -f elf sort.asm -o sort.oTo create your executable you will type: gcc -g -o sort1.out sorting1.c sort.oMatloff demonstrates sorting using a sort known as selection sort, which is less efficient than insertion sort. Here arethe two algorithms:Selection Sort: For each position, starting from the zeroth position1. Find the smallest value in the subarray encompassing that position and all positions with larger indices.2. Swap that smallest value with the entry in the current position.Insertion Sort: For each position, starting from the [1] position1. Store the entry in the current position in a temporary variable.2. Working from the current position towards the zeroth position, shift right one position in the array all entries thatare larger than that in the temporary variable.3. When an entry that is smaller than the temporary value is encountered, then copy the temporary value into theposition just to the right of the smaller value. Note that the entry originally in the destination position will havealready been copied one position to its right.Both algorithms rely on the fact that after each iteration, the subarray to the left of the current position is sorted. Theselection sort is slower because it must always search through the remaining array for the smallest values, while theinsertion sort continues shifting items only as long as they are larger than the current entry. This difference is mostapparent when the algorithms are attempting to sort an array of N items that are already sorted. In such a case, selectionsort will look through N items for the smallest value, then N-1 items, then N-2 items, and so on, for N(N+1)/2comparisons. On the other hand, insertion sort will stop comparing after the first comparison of each position, for a totalof N comparisons.


Lessons learned:1. C automatically prepends a '_' to function names.2. leal means "load effective address long". This operation copies an address to a register so that you can later adddisplacements to it.3. Work little by little, by writing some code, compiling, and then running. At the beginning, you will just want theprogram to print out the words as they were originally ordered. Get your pushes and pops working properly beforebothering writing your sorting code. Use the standard push ebp; mov ebp, esp; sub esp, x; to setup the framepointer and make room for your local variable needing x bytes of room.2) (10 points) Filename: sorting2.c Imbed your assembly code from #1 in the sort() function of sorting2.c. You willwant to read http://www.reversing.be/article.php?story=20051203194931893. Once you have imbedded your codeyou can use gcc –masm=intel –S sorting2.c to make sure everything looks correct.3) (10 points, extra credit) Filename: sort3.asmWrite an Intel assembly language string sorting function, named sort(), for the nasm assembler that relies on insertionsort. The function takes a two dimensional array char[][25], and an int n for parameters, and is called from sort3.c4) (25 points) Filename: mult.mpsWrite a MIPS program that multiplies the values in an array of ten unsigned integers by the values contained in the inanother array of ten unsigned integers, and print the results to the screen. The two arrays will start at 0xA00 and 0xA30.To print an integer, place it in $a0, set $v0 to 1, and make a syscall. To exit, set $v0 to 10, and make a syscall.You may assume that all of the integers are less than 0xFFFF so that the result will fit in one integer. You may not usethe MULT, nor MULTU opcodes in your program. You must multiply by shifting and adding as in the example below.Your multiplication routine should be a subroutine called from your main program. You should use the appropriateregisters, e.g. parameter passing registers, based on their role in the program. Note that hexadecimal numbers areprefaced with “0x” and not “$” in MIPS. Please do not start this until Saturday so that I can ensure that CUSP and ASIDEwork properly.In C++, you would implement:unsigned int multiply(unsigned int x, unsigned int y) {unsigned int reg = 0;while (y != 0) {if (y & 1)reg += x;x = 1;} // while y not zeroreturn reg;} // multiply()int main() {unsigned int a[10], b[10];for(int i = 0; i < 10; i++){unsigned int result = multiply(a[i], b[i]);cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!