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 17th, 2011, 12:14 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
Assembly - Need help with mips assembly

Hello, i am a second year CS major with no programming experience before college, so I am a bit stuck at the moment. Anyone who could help me with my MIPS assembly code below would be greatly appreciated.

Here is what i'm supposed to do for my class assignment.

Crypto

Description:

Encrypt a string of characters using the linear function
y = ax + b.
Also, decrypt the encrypted string using the inverse function
x = (y-b)/a.
Input the string and the values of a and b.

So far i have figured out how to resolve the fact that the linear function could easily overflow the 127 character ascii table by finding the remander, but the problem i'm having is that when i load a byte from the input buffer, i don't know how to extend it to a word so that i can continue with the linear function, and then i also don't know how to reduce the data back to a byte to allow me to output back to an output buffer. This problem occurs in my Encrypt: procedure and in the NextChar loop structure within that.
For a few brownie points i am trying out File I/O with MARS, i hope the way i have it set up now will work.

This is my first time using a devhelp forum and my first semester using assembly, so please ignore or give friendly feedback for any annoying quirks i may have

###############################################
# csc 211
# crypto
# description. This is part one of two parts. to make it easier
# to display the program's functions, i separated the two
# functionalities. This is the first part of the program
# this code will open a text file to get an unencrypted
# string, it will then display the string to the user for
# conformation, and then below that will display the
# encryptedv ersion of the string and output the encrypted
# version to another text file to be decrypted by the
# other half of this project.
#
# 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:
# $t0: value for x
# $t1: value for b
# $t3: current char (a)
# $t4: result char (y)
#
#
############################################################

.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 $t0, $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 $t1, $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
######################################################################
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
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 buffered string

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


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

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

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

# li $a0, 1
# li $v0, 1
# syscall

# jal nameCleanOut

# li $v0, 13 # system call for open file
# la $a0, fileOut # input file name
# li $a1, 1 # flag for writing
# 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

la $t6, result

nextChar:
lbu $t3, ($t5) # load a byte off the string
beqz $t5, endEncrypt # a end on null terminator
mul $t3, $t3, $t0 # y = ax
add $t3, $t3, $t1 # y = ax + b
rem $t3, $t3, 127
sb $t3, ($t6)
add $t6, $t6, 1

# li $v0, 15 # to write file
# move $a0, $s0 # load address off file descriptor
# la $a1, result # load output buffer
# li $a2, 2 # number of chars to write
# syscall

# bltz $v0, errorWrite # error writingto file

# 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


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
  #2  
Old December 17th, 2011, 12:17 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
sorry the comments are so cramped btw.. This was formatted well in mars before i pasted it on here.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Assembly - Need help with mips assembly

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