Having trouble figuring out how to find runtime of assembly program
I’ve got a project and it’s comparing the runtime of Java vs assembly programs for mathematical algorithms like the Fibonacci sequence, Euclidean algorithm, factorial algorithm and buy and binary exponential algorithm. Tried using spim and mars but they don’t have this feature.
Any suggestions? Due dates coming up.
Ex fib code that calculates estimated clock cycles and fib given a user input.
.data
fibonacciLabel: .asciiz "Fibonacci sequence: " enterLabel: .asciiz "Enter a number to calculate its Fibonacci sequence: " cycleLabel: .asciiz "Execution time in cycles: " space: .asciiz " " # Space character newline: .asciiz "\n"
.text .globl main main: # Start measuring time - store the initial cycle counter value mflo $t4 mfhi $t5
# Prompt the user to enter a number
li $v0, 4 # Print string syscall
la $a0, enterLabel # Load address of the prompt message
syscall
# Read user input
li $v0, 5 # Read integer syscall
syscall
add $t0, $zero, $v0 # Save the user input in $t0
# Initialize variables
li $t1, 0 # First Fibonacci number (F(0))
li $t2, 1 # Second Fibonacci number (F(1))
li $t3, 2 # Counter for the Fibonacci sequence
# Print the Fibonacci label
li $v0, 4 # Print string syscall
la $a0, fibonacciLabel # Load address of the Fibonacci label
syscall
# Print the first two Fibonacci numbers (F(0) and F(1))
move $a0, $t1 # Move the first Fibonacci number (F(0)) to $a0
li $v0, 1 # Print integer syscall
syscall
# Print a space between the numbers
li $v0, 4 # Print string syscall
la $a0, space # Load address of space character
syscall
move $a0, $t2 # Move the second Fibonacci number (F(1)) to $a0
li $v0, 1 # Print integer syscall
syscall
# Calculate and print the rest of the Fibonacci sequence
loop: # Check loop condition bge $t3, $t0, endLoop # If the counter is greater than or equal to the input, exit the loop
# Count the loop instruction (branch instruction)
addi $t9, $t9, 1
# Calculate the next Fibonacci number
add $t4, $t1, $t2 # Sum of the two preceding Fibonacci numbers
# Print a space between the numbers
li $v0, 4 # Print string syscall
la $a0, space # Load address of space character
syscall
# Print the next Fibonacci number
move $a0, $t4 # Move the result to $a0
li $v0, 1 # Print integer syscall
syscall
# Store the result in $t2 for the next iteration
move $t1, $t2
move $t2, $t4
# Count the instruction inside the loop (addition and move)
addi $t9, $t9, 2
addi $t3, $t3, 1 # Increment the counter
# Count the instruction inside the loop (increment)
addi $t9, $t9, 1
j loop # Jump back to the beginning of the loop
endLoop: # Print a newline li $v0, 4 # Print string syscall la $a0, newline # Load address of newline syscall
# End measuring time - store the final cycle counter value
mflo $t6
mfhi $t7
# Calculate the cycle count difference
sub $t6, $t6, $t4
sra $t7, $t7, 1 # Shift the high part to get the accurate difference
sub $t6, $t6, $t7
# Print the cycle count label
li $v0, 4 # Print string syscall
la $a0, cycleLabel # Load address of the cycle count label
syscall
# Print the number of cycles
move $a0, $t9 # Move the cycle count to $a0
li $v0, 1 # Print integer syscall
syscall
# Print a newline
li $v0, 4 # Print string syscall
la $a0, newline # Load address of newline
syscall
# Exit the program
li $v0, 10
syscall