



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The University of New Mexico
Andree Jacobson Due: 2/26/2009 @ 11:59:59pm
One reason why one might want to write in assembly code is to optimize common or expensive operations. For example, applications that perform much sorting and searching on large data-sets might benefit from having QuickSort and hash table-routines implemented directly in machine code.
In this assignment we will implement two string functions. The first one simply counts the occurences of a specific character in a string:
occur(A, c) computes the occurrences of the characther c in the null terminated character string A.
The file skeleton.S contains a skeleton program to get you started. You should download the skeleton file, make a copy of it, and rename the copy to occur.S. The occur routine have already been declared for you; and all the jumps to and from it has also been implemented. All you have to do is fill in the missing routine body under the comment as indicated in the skeleton file. You should put your solution in the file occur.s and that’s the file that will be turned in. 1
Testing:
The skeleton file also contains several test routines that call occur. Here’s the output when we run the completed program on the SGI Indy machine:
andree@on02:hw3 >./a.out occur("atest", ’a’) = 1 occur("thisisastringforthepurposeoftesting", ’a’) = 1 occur("thisisastringforthepurposeoftesting", ’F’) = 0 occur("IWANTTOCHECKIFTHEYWORKFORCAPTITALONES", ’F’) = 2 occur("thisISthethirDSTringthatwillbetested", ’t’) = 7 occur("thisISthethirDSTringthatwillbetested", ’T’) = 1
In this part you can use the same skeleton file as you did for the first part, but properly rename, and change test cases, and comments in the file to implement the functionality for the C string function called strrchr, one of the same
(^1) My solution requires less than 10 assembly code statements for the routine body. Can you do the same or better?
functions that you implemented for your previous assignment. It determines the location of the last occurence of a character in a string and returns that index, or -1 if the occurence was not found in the string we’re searching. Please call your file strrchr.S.
A Makefile for your turnin use can be found among the source files. SPIM will run occur.s directly, through the spim -f occur.s command, or you can simply type make run, and that command will be executed. The Makefile also provides a shortcut for you to turn in your assignment when you’re done. (Note that you’ll have to copy over the files to the moons.cs.unm.edu machines before running the make turnin commands.
When you have completed the program, submit your occur.s file by typing pmake turninq in the directory where the file is stored. Or, to do it manually type pturnin cs341.p1 occur.Sq. You may turn in your occur.S file as many times as you want; turnin will always replace the previously turned-in version with the new version.
Note that when you turn in the program, make sure that the skeleton file only contains the code it was distributed with, and your added source code in the right place. Also, make sure to put comments with your name at the top of the file. Failure to follow these instructions might result in point deductions.
Each routine in the string package starts with a prologue and ends with an epilogue. These consist of instructions that are necessary to begin and end a function. For this assignment you do not need to modify the prologues and epilogues. The occur routine looks like this:
.ent occur occur: .frame sp, 24, ra # Define stack size for debugger .mask 0x00000090,0 # Indicate storage of $ra and $gp on stack .set noreorder # Don’t allow hardware to reorder instructions
/* The occur function stack looks like this: N/A 24 $ra 20 $gp 16 $a3 12 $a2 8 $a1 4 $a0 0 <- $sp */
The routine test call the occur function and prints out the result:
.ent test
test: .frame sp, 24, ra .mask 0x30000090,0 # Mask $ra, $gp, $a0, $a .set noreorder
/* The test function stack looks like this: N/A 24 $ra 20 $gp 16 $a3 12 $a2 8 $a1 4 $a0 0 <- $sp */
#BEGIN Prologue subu sp, sp, 24 # Allocate stack frame sw ra, 20(sp) # Save old return address in stack frame sw a1, 4(sp) # Save the input character sw a0, 0(sp) # Save the input string .cprestore 16 # Generates "sw gp, 12(sp) #END prologue
jal occur # compute the occurrence of the letter nop
la a0, outfmt lw a1, 0(sp) # Load the address of the string to a lw t0, 4(sp) # Load the address of the character into t nop # Fill load delay slot lbu a2, 0(t0) # Load the character into a move a3, v0 # return value from occur is a jal printf nop
#BEGIN Epilogue lw ra, 20(sp) # Restore return address addiu sp, sp, 24 # Pop the stack (load delay slot) jr ra # Jump back to main nop # {Fill branch delay slot} #END Epilogue
.set reorder .end test
The main routine, finally, calls test with the different test strings as input:
.globl main .ent main
main: .frame sp, 24, ra .mask 0x00000090, 0 # Mask $ra and $gp .set noreorder
/* The main function stack looks like this: N/A 24 $ra 20 $gp 16 $a3 12 $a2 8 $a1 4 $a0 0 <- $sp */
#BEGIN prologue subu sp, sp, 24 # Allocate stack frame sw ra, 20(sp) # Save old return address in stack frame .cprestore 16 # Generates "sw gp, 16(sp) #END prologue
la a0, string la a1, chr jal test nop
...
#BEGIN epilogue lw ra, 20(sp) # Restore return address addiu sp, sp, 24 # Pop the stack (branch delay slot) jr ra # Jump back to main move v0, zero # Main returns 0 on success #END epilogue
.set reorder .end main