aspire29
aspire29

Reputation: 461

How to get the time taken for a calculation using MIPS assembly language?

So currently I want to calculate the time taken for a calculation. I am using 30 in $v0, but the problem is every time print the time taken, a huge number is shown which is not related. so how can I fix this problem? any help is appreciated

An example code

.data
inputNumber:    .asciiz "Input an integr: "
newLine:    .asciiz "\n"
done1:      .asciiz "loop completed\n"
.text
main:
#print inputNmber string
li $v0, 4
la $a0, inputNumber
syscall

#read integer
li $v0, 5
syscall

#move int to another register
move $t0, $v0

# get starting time
li $v0, 30
syscall

# store it in another place
move $a0, $v0

#while loop counter
addi $t1, $zero, 1

# while loop
while:
    bgt $t1, $t0, done
    #print inputNmber int
    li $v0, 1
    move $a0, $t0
    syscall

    #print new line
    li $v0, 4
    la $a0, newLine
    syscall

    addi $t1, $t1, 1

    j while


#Exit the program
li $v0, 10
syscall

done:
    # get finishing time
    li $v0, 30
    syscall

    # store it in another place
    move $a1, $v0

    #printing done1
    li $v0, 4
    la $a0, done1
    syscall

    # Subtract time values
    sub $t2, $a1, $a0

    #print time taken
    li $v0, 1
    move $a0, $t2
    syscall

    # exit program
    li $v0, 10
    syscall

Upvotes: 1

Views: 1669

Answers (1)

Omar
Omar

Reputation: 973

First of all, after returning from the syscall which gives you the system time, you store the result in $a0. However, inside the loop, you erase the value of $a0 :

#print inputNmber int
li $v0, 1
move $a0, $t0

Moreover, by looking at the syscall table, you can see that this syscall put the time value like this :

$a0 = low order 32 bits of system time
$a1 = high order 32 bits of system time

And not in $v0. So you should adapt your move instructions and your subtraction taking this in consideration

NOTE: If you are using an emulator, this sycall is NOT compatible with SPIM, only MARS

Source for syscalls: https://courses.missouristate.edu/KenVollmar/mars/Help/SyscallHelp.html

Upvotes: 1

Related Questions