Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2011, 11:07 PM
ivers15 ivers15 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 5 ivers15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m 55 sec
Reputation Power: 0
Having trouble with mips program

so i have resolved a few issues since my last post, but my string encryption program is still giving me some trouble, if anyone could help, it would be very much appreciated.

here is what i have so far (with some extraneous code ommited)
the line where MARS detects the exception is marked with a frown emoticon since that will make it easy to find the line. I am getting an arithmatic overflow runtime error. Also its hard to make everything allign on here the way i need it to but i hope it is easy enough to read.



# logic:
#
# 1.) get the input file name from the user
# 2.) open the input stream using the file name
# 3.) read in characters from the file until an end line character is encountered
# 4.) close the file
# 5.) send the string to the encryptoin subprogram
# 6.) store the result from encryption subprogram
# 7.) get the output file name from user
# 8.) open the output file to write.
# 9.) append an end line character to the end of the output buffer
# 10.) determine the lenght of the output buffer
# 11.) write to the output file using the buffer address and length
# 12.) close the output file stream
# 13.) EOP
#
# register assignment:
# $s1: value for x
# $s2: value for b
#
#
#
############################################################

.text
.globl main
main:

li $v0, 4 # prompt user for file name
la $a0, fileInPrompt
syscall

li $v0, 8 # input a string
la $a0, fileIn # address of file name
li $a1, 21 # max of 21 chars for file name
syscall

la $a0, fileIn # echo file name
li $v0, 4
syscall

li $a0, 1
li $v0, 1
syscall

jal nameCleanIn # send the file name to name clean procedure to append proper terminating character

jal fileRead # send the file to the file read program to begin reading the file

li $v0, 4 # new line carrage return
la $a0, endl
syscall

li $v0, 4
la $a0, inform # to inform the user how their string will be encrypted
syscall

li $v0, 4 # prompt the user for a value for X
la $a0, promptX
syscall

li $v0, 5 # input an integer value
syscall
move $s1, $a0 # move the value of $a0 to $t0

li $v0, 4 # prompt the user for a value for B
la $a0, promptB
syscall

li $v0, 5
syscall
move $s2, $a0

jal Encrypt

la $a0, result
li $v0, 4
syscall

EOP:
li $v0 10
syscall

############################ name clean for input ###############################
# cleans the file name input by the user and replaces the \n terminating
# character with a null terminating character so it can be properly
# opened
# yes i know this procedure is very innefficient but my first concernt right now is to just make it work
# Logic
# 1) initiate a loop counter
# 2) initiate a max size
# 3) End the loop when counter reaches max
# 4) load a character from the string
# 5) if the end line character is not reached, repeat the loop
# 6) if it is found, replace it with the null terminator
# 7) loop repeat, increment counter
# 8) return to calling program,, same process for out file
######################################################################
nameCleanIn:
li $t0, 0 # loop counter
li $t1, 21 # loop end at max file name length
clean:
beq $t0, $t1, endClean # end of loop
lb $t3, fileIn($t0) # load a byte from the string
bne $t3, 0x0a, nextLoop # if the \n terminating char is not found, repeat the loop
sb $zero, fileIn($t0) # terminating char is found, replace it with a zero terminator
nextLoop:
addi $t0, $t0, 1 # increment the counter
j clean # repeat the loop

endClean:
jr $ra
#clean ouput file
nameCleanOut:
li $t0, 0 # loop counter
li $t1, 21 # loop end at max file name length
clean2:
beq $t0, $t1, endClean2 # end of loop
lb $t3, fileOut($t0) # load a byte from the string
bne $t3, 0x0a, nextLoop2 # if the \n terminating char is not found, repeat the loop
sb $zero, fileOut($t0) # terminating char is found, replace it with a zero terminator
nextLoop2:
addi $t0, $t0, 1 # increment the counter
j clean2 # repeat the loop

endClean2:
jr $ra
#####################################################################

######################### file read #################################
# opens the file, reads it to the buffer, and closes it
######################################################################

fileRead:
li $v0, 13 # system call for open file
la $a0, fileIn # input file name
li $a1, 0 # flag for reading
li $a2, 0 # mode is ignored
syscall # open the input file(file descriptor is returned)
bltz $v0, errorOpen # negative number shows an open error. jump to error
move $s0, $v0 # save the file desscriptor


li $v0, 14 # system call to read from file
move $a0, $s0 # move the file descriptor into $a0
la $a1, stringBuff # a1 contains address of buffer
li $a2, 1000 # max read of 1000 chars
syscall # read from file
bltz $v0, errorRead # negative return shows a read error. jumpt to error

li $v0, 16 # system call to close file
move $a0, $s0 # move descriptor back into a0
syscall # close the file
bltz $v0, errorClose # negative number shows error closing file jump to error

la $a0, stringBuff # echo the file contents
li $v0, 4
syscall

jr $ra # return to procedure call
########################## Exceptions
errorOpen:
la $a0, error1 # show error opening file
li $v0, 4
syscall

j EOP # end the program

errorRead:
la $a0, error2 # show error reading ifle
li $v0, 4
syscall

j EOP # end the program

errorClose:
la $a0, error3 # show error closing the file
li $v0, 4
syscall

j EOP # end the program

errorWrite:
move $a0, $v0 # show error writing backto the file
li $v0, 1
syscall
la $a0, error4
li $v0, 4
syscall

j EOP
#############################################################################


####################################### Encrypt ############################
# this procedure will take the first string apart one character(byte)
# at a time. the result will then be written to a file for decrypt to decode
# when it is loaded
# $t0: value for x
# $t1: value for b
# $t3: current char (a)
# $t4: result char (y)
# $t5: address of the input buffer string
# $t6: address of the result string
# $t7: temporary value

##############################################################################
Encrypt:
la $t5, stringBuff # load the address of the string to t1
la $t6, result

nextChar:
lb $t3, ($t5) # load a byte off the string
beqz $t5, endEncrypt # a end on null terminator
mul $t7, $t3, $s1 # y = ax
add $t4, $t7, $s2 # y = ax + b ARITHMATIC OVERFLOW EXCEPTION OCCURS HERE
rem $t4, $t7, 127 # find the remainder for encryption to make sure that the byte to be stored will be an ascii value
sb $t4, ($t6) # store the character back
add $t6, $t6, 1 # next position in result
add $t5, $t5, 1 # next position in buffer

add $t5, $t5, 1 # add next byte

j nextChar # repeat the loop


endEncrypt:
jr $ra


.data
promptX: .asciiz "Give a value for x: "
promptB: .asciiz "Give a value for b: "
inform: .asciiz "Your string will be encrypted with this formula; y = ax + b\n where y is the resulting character, and a is the starting character"
fileInPrompt: .asciiz "Input your file name: "
fileOutPrompt: .asciiz "Input the file name for output: "
fileIn: .space 22
fileOut: .space 22
error1: .asciiz "\nERROR OPENING FILE\n"
error2: .asciiz "\nERROR READING FILE\n"
error3: .asciiz "\nERROR CLOSING FILE\n"
error4: .asciiz "\nERROR WRITING FILE\n"
stringBuff: .space 1000
endl: .asciiz "\n"
result: .space 1000

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Having trouble with mips program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap