rendered paste body .data
prompt: .asciiz "Please enter an expression to be evaluated: "
equal: .asciiz " = "
buf: .space 76
spaces: .word 0,0
powers: .word 1, 10, 100, 1000, 10000, 100000, 1000000
.word 10000000, 100000000, 1000000000
.text
main:####################################################
# #
# Read expressions into input buffer #
# Evaluate expression and print result or quit for ' ' #
# Mutate for result == 13 #
# #
#########################################################
# #
# bool bMutated = false; # mutation done at the assembly level
# print "Please enter an expression to be evaluated: "; #
# String s = readLine(); #
# while(s.charAt(0)!=32) #
# { #
# String[] tokens = s.split(' '); #
# int s = str_to_num(tokens[0]); #
# int t = str_to_num(tokens[2]); #
# int d; #
# print ToString(s) + ' ' + tokens[1] + ' ' #
# + ToString(t) + " = "; #
# switch(tokens[1][0]) #
# { #
# case '+': #
# d = s+t; #
# break; #
# case '-': #
# if(bMutated) # mutation done at the assembly level
# d = s^t; #
# else #
# d = s-t; #
# break; #
# case '*': #
# d = s*t; #
# break; #
# default: #
# d = s/t; # done using integer and remainder parts
# } #
# println d; #
# if(!bMutated&&d==13) bMutated = true; #
# print "Please enter an expression "; #
# print "to be evaluated: "; #
# String s = readLine(); #
# } #
# print "\n\nExiting program..."; #
# #
#########################################################
li $v0, 4 # prompt user for input
la $a0, prompt # prompt user for input
syscall # prompt user for input
#
li $v0, 8 # read string into buffer
la $a0, buf # read string into buffer
li $a1, 74 # read string into buffer
syscall # read string into buffer
#
li $s7, 0 # $s7 is counter
la $t0, buf # $t0 is byte pointer
la $t5, spaces # $t5 is where the addresses of ' ' bytes are stored
while:
addu $t7, $s7, $t0 # $t7 = address of the current byte
lbu $t1, 0($t7) # load current byte into $t1
beq $t1, 0, endwhile # continues looping until '\0' byte is reached
beq $t1, 10, endwhile # continues looping until '\n' byte is reached
beq $t1, 32, store_space # if we hit ' ', store the address of the byte
j continue # continue looping
#
store_space:#############################################
# #
# Storing the address of the space position #
# #
#########################################################
sw $t7, 0($t5) #
addiu $t5, $t5, 4
continue:
addiu $s7, $s7, 1
j while
endwhile:
la $a0, buf
lw $a1, spaces
jal str_to_num
move $s0, $v0
lw $a0, spaces+4
addiu $a0, $a0, 1
addu $a1, $a0, $s7
jal str_to_num
move $s1, $v0
lw $t0, spaces
addiu $t0, $t0, 1
lbu $t0, 0($t0)
li $v0, 1
move $a0, $s0
syscall
li $v0, 11
li $a0, 32
syscall
move $a0, $t0
syscall
li $a0, 32
syscall
li $v0, 1
move $a0, $s1
syscall
li $v0, 4
la $a0, equal
syscall
li $v0, 1
switch:
beq $t0, 42, casemultiply # case '*'
beq $t0, 43, caseadd # case '+'
beq $t0, 45, casesubtract # case '-'
casedivide:############################################## default
# #
# #
# #
#########################################################
div $s0, $s1
mflo $a0
syscall
j switchbreak
casemultiply:
mul $a0, $s0, $s1
syscall
li $v0, 11
li $a0, 114
syscall
li $v0, 1
mfhi $a0
syscall
mflo $a0
j switchbreak
casesubtract:
sub $a0, $s0, $s1
syscall
j switchbreak
caseadd:
add $a0, $s0, $s1
syscall
switchbreak:
beq $a0, 13, mutate # this macro fills 2 words
j main
mutate:
lw $t1, casesubtract # mutate to sub to xor
ori $t1, $t1, 4 # mutate to sub to xor
sw $t1, casesubtract # mutate to sub to xor
lw $t1, switchbreak+8 # read j main
sw $t1, switchbreak # write j main over mutate branching
j main # resume loop
str_to_num:##############################################
# #
# Convert ascii number to number #
# #
# Input: #
# $a0 is the beginning of string #
# $a1 is the end of string #
# #
# Uses: #
# $t0 is first byte #
# $t3 is return address #
# #
# Output: #
# $v0 contains the 32-bit number #
# #
#########################################################
move $t3, $ra #
lbu $t0, 0($a0) # load first byte of buffer(input) into $t1
#
bne $t0, 120, str_to_num_elseif # if (firstbyte == 'x')
jal hexstr_to_num # call hexstr_to_num
j str_to_num_endif #
#
str_to_num_elseif:#######################################
# #
# Call binstr_to_num if firstbyte=='%' #
# #
#########################################################
bne $t0, 37, str_to_num_else # else if(firstbyte == '%')
jal binstr_to_num # call binstr_to_num
j str_to_num_endif # end and return
#
str_to_num_else:#########################################
# #
# Call decstr_to_num #
# #
#########################################################
jal decstr_to_num #
#
str_to_num_endif:########################################
# #
# Jump back to the main program #
# #
#########################################################
jr $t3 #
#
#########################################################
decstr_to_num:###########################################
# #
# Convert ascii decimal number to number #
# #
# Input: #
# $a0 is the beginning of string #
# $a1 is the end of string #
# #
# Uses: #
# $t0 is string length from current byte #
# $t1 is current power of 10 #
# $t2 is current byte #
# #
# Output: #
# $v0 contains the 32-bit number #
# #
#########################################################
sub $t0, $a1, $a0 #
sll $t0, $t0, 2 #
li $v0, 0 #
#
decstr_to_num_loop:######################################
# #
# Adds multiples of powers of 10 for each digit #
# #
#########################################################
la $t1, powers+-4 #
add $t1, $t0, $t1 #
lw $t1, 0($t1) #
lbu $t2, 0($a0) #
addiu $t2, $t2, -48 #
#
decstr_to_num_add:#######################################
# #
# Add power of 10 n times for current digit #
# #
#########################################################
beq $t2, 0, decstr_to_num_continue #
addu $v0, $v0, $t1 #
addiu $t2, $t2, -1 #
j decstr_to_num_add #
#
decstr_to_num_continue:##################################
# #
# Continue the loop or return if at end #
# #
#########################################################
addiu $t0, $t0, -4 #
addiu $a0, $a0, 1 #
bne $a0, $a1, decstr_to_num_loop #
jr $ra #
#
#########################################################
hexstr_to_num:###########################################
# #
# Convert ascii hexadecimal number to number #
# #
# Input: #
# $a0 is the beginning of string #
# $a1 is the end of string #
# #
# Uses: #
# $t0 is current byte #
# #
# Output: #
# $v0 contains the 32-bit number #
# #
#########################################################
addiu $a0, $a0, 1 # skip 'x'
li $v0, 0 # initialize result
#
hexstr_to_num_loop:######################################
# #
# Iterates and applies digits to $v0 #
# #
#########################################################
sll $v0, $v0, 4 # prepare resultant for next digit
lbu $t0, 0($a0) # load current byte
bgt $t0, 57, hexstr_to_num_letters # handling 'A'-'F'
addiu $t0, $t0, -48 # get numeric value represented by '0'-'9'
j hexstr_to_num_continue #
#
hexstr_to_num_letters:###################################
# #
# Handles cases 'A-F' #
# #
#########################################################
addiu $t0, $t0, -55 # get numeric value represented by 'A'-'F'
#
hexstr_to_num_continue:##################################
# #
# Continue the loop or return if at end #
# #
#########################################################
add $v0, $t0, $v0 # applying current hex digit to result
bne $a0, $a1, hexstr_to_num_loop #
jr $ra # number converted and stored in $v0
#
#########################################################
binstr_to_num:###########################################
# #
# Convert ascii binary number to number #
# #
# Input: #
# $a0 is the beginning of string #
# $a1 is the end of string #
# #
# Uses: #
# $t0 for loading current byte #
# #
# Output: #
# $v0 contains the 32-bit number #
# #
#########################################################
addiu $a0,$a0, 1 # skip '%'
li $v0, 0 # initialize resultant
#
binstr_to_num_loop:######################################
# #
# Applies all bytes as bits to $v0 #
# #
#########################################################
sll $v0, $v0, 1 # prepare resultant for next digit
lbu $t0, 0($a0) # load current byte
addiu $t0, $t0, -48 # get numeric value represented by '0'-'1'
add $v0, $t0, $v0 # applying current binary digit to resultant
bne $a0, $a1, binstr_to_num_loop #
jr $ra # number converted and stored in $v0
#
#########################################################