Evaluating Arithmetic Expressions using variables in Stack
.data
prompt: .asciiz "Please enter a string (empty string to exit): "
result: .asciiz "String length: "
string: .space 100 # place to save string
.text
main:
loop:
la $a0, prompt # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $a0, string # place to save string to read
li $a1, 100 # read at most 100 characters
li $v0, 8 # syscall function to read a string
syscall # call system to read a string
li $t2, 0 # t2 will count the characters, start in zero
la $t0, string # load address of string in $t0
countChars:
lb $t1, 0($t0) # load character from string
beq $t1, $zero, endCount # if zero, end count
beq $t1, 10, endCount # if newline, end count
addi $t2, $t2, 1 # increment the number of characters
addi $t0, $t0, 1 # advance to next character in string
j countChars # keep counting the chars
endCount:
beqz $t2, exit # if the string length is zero, exit
la $a0, result # load result message string address
li $v0, 4 # use print string function
syscall # call system to print the result
addu $a0, $t2, $zero # move the char count to a0 to be printed
li $v0, 1 # function to print an integer
syscall # call system to print the integer
li $a0, 10 # load a newline character
li $v0, 11 # use print character function
syscall # call system to print the character
j loop # repeat indefinitely
exit:
li $v0, 10 # exit program function
syscall # call system to terminate program
.data
promptA: .asciiz "Enter a value for a: "
promptB: .asciiz "Enter a value for b: "
promptC: .asciiz "Enter a value for c: "
result: .asciiz "Result of (3ab - 2bc - 5a + 20ac - 16): "
.text
main:
la $a0, promptA # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $v0, 5 # function to read an integer
syscall # call system to read an integer
addu $t0, $v0, $zero # move A value to $t0
la $a0, promptB # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $v0, 5 # function to read an integer
syscall # call system to read an integer
addu $t1, $v0, $zero # move B value to $t1
la $a0, promptC # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $v0, 5 # function to read an integer
syscall # call system to read an integer
addu $t2, $v0, $zero # move C value to $t2
addi $sp, $sp, -4 # allocate space in stack to save a number
# Calculation of 3ab
mult $t0, $t1 # multiply A times B
mflo $t3 # save result in $t3
li $t4, 3 # load 3 in $t4
mult $t3, $t4 # multiply AB * 3
mflo $t3 # save result in t3
sw $t3, 0($sp) # save in stack
# Calculation of 2bc
mult $t1, $t2 # multiply B times C
mflo $t3 # save result in $t3
sll $t3, $t3, 1 #multiply by 2 using a shift left
lw $t4, 0($sp) # load 3ab from stack
sub $t3, $t4, $t3 # make subtraction 3ab - 2bc
sw $t3, 0($sp) # save in stack
# Calculation of 5a
li $t3, 5 # load 5 in $t3
mult $t0, $t3 # multiply a times 5
mflo $t0 # save result in $t0
lw $t3, 0($sp) # load previous result of (3ab - 2bc)
sub $t3, $t3, $t0 # calculate 3ab - 2bc - 5a
sw $t3, 0($sp) # save result on stack
#Calculation of 20ac
sll $t0, $t0, 2 # multiply 5a by 4 to get 20a using 2 shifts left
mult $t0, $t2 # multiply 20a * c
mflo $t0 # save result in $t0
lw $t1, 0($sp) # load previous value (3ab - 2bc - 5a)
add $t0, $t0, $t1 # calculate sum 3ab - 2bc - 5a + 20ac
addi $t0, $t0, -16 # subtract 16 to get final result: 3ab - 2bc - 5a + 20ac - 16
addi $sp, $sp, 4 # restore stack pointer
la $a0, result # load result message string address
li $v0, 4 # use print string function
syscall # call system to print the result
addu $a0, $t0, $zero # move the result to be printed
li $v0, 1 # function to print an integer
syscall # call system to print the integer
li $a0, 10 # load a newline character
li $v0, 11 # use print character function
syscall # call system to print the character
li $v0, 10 # exit program function
syscall # call system to terminate program
.data
promptU: .asciiz "Enter a value for u: "
promptV: .asciiz "Enter a value for v: "
result: .asciiz "Result of (5u^2 - 12uv + 6v^2): "
.text
main:
la $a0, promptU # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $v0, 5 # function to read an integer
syscall # call system to read an integer
addu $t0, $v0, $zero # move U value to $t0
la $a0, promptV # load prompt string address
li $v0, 4 # use print string function
syscall # call system to print the prompt
la $v0, 5 # function to read an integer
syscall # call system to read an integer
addu $t1, $v0, $zero # move V value to $t1
# Calculation of 5u^2
li $a0, 5 # A =5
addu $a1, $zero, $t0 # X = u
addu $a2, $zero, $t0 # Y = u
jal computeAXY
addu $t2, $zero, $v0 # save 5u^2 in $t2
# Calculation of -12uv
li $a0, -12 # A = -12
addu $a1, $zero, $t0 # X = u
addu $a2, $zero, $t1 # Y = v
jal computeAXY
add $t2, $t2, $v0 # calculate 5u^2 -12uv in $t2
# Calculation of 6v^2
li $a0, 6 # A = 6
addu $a1, $zero, $t1 # X = v
addu $a2, $zero, $t1 # Y = v
jal computeAXY
add $t2, $t2, $v0 # calculate total 5u^2 -12uv + 6v^2 in $t2
la $a0, result # load result message string address
li $v0, 4 # use print string function
syscall # call system to print the result
addu $a0, $t2, $zero # move the result to be printed
li $v0, 1 # function to print an integer
syscall # call system to print the integer
li $a0, 10 # load a newline character
li $v0, 11 # use print character function
syscall # call system to print the character
li $v0, 10 # exit program function
syscall # call system to terminate program
# subroutine that calculates A*X*Y
# On entry:
# $a0 = A
# $a1 = X
# $a2 = Y
# On return:
# $v0 = A*X*Y
computeAXY:
mult $a0, $a1 # multiply A*X
mflo $v0 # save result in $v0
mult $v0, $a2 # multiply (A*X) * Y
mflo $v0 # save result in $v0 for returning it
jr $ra # return to the caller