17.8k views
5 votes
write a program that runs on spim that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds.

1 Answer

5 votes

Answer:

.data

hours: .word 0

minutes: .word 0

seconds: .word 0

.text

.globl main

main:

# Prompt the user to enter the number of hours

li $v0, 4

la $a0, hours_prompt

syscall

# Read the number of hours

li $v0, 5

syscall

sw $v0, hours

# Prompt the user to enter the number of minutes

li $v0, 4

la $a0, minutes_prompt

syscall

# Read the number of minutes

li $v0, 5

syscall

sw $v0, minutes

# Prompt the user to enter the number of seconds

li $v0, 4

la $a0, seconds_prompt

syscall

# Read the number of seconds

li $v0, 5

syscall

sw $v0, seconds

# Calculate the total time in seconds

lw $t0, hours

lw $t1, minutes

lw $t2, seconds

li $t3, 3600 # number of seconds in an hour

mul $t0, $t0, $t3

li $t3, 60 # number of seconds in a minute

mul $t1, $t1, $t3

add $t0, $t0, $t1

add $t0, $t0, $t2

# Print the result

li $v0, 4

la $a0, result

syscall

li $v0, 1

move $a0, $t0

syscall

# Exit the program

li $v0, 10

syscall

hours_prompt: .asciiz "Enter the number of hours: "

minutes_prompt: .asciiz "Enter the number of minutes: "

seconds_prompt: .asciiz "Enter the number of seconds: "

result: .asciiz "The total time in seconds is: "

Step-by-step explanation:

  1. The program starts by defining three variables hours, minutes, and seconds to store the input from the user.
  2. The program then prompts the user to enter the number of hours, minutes, and seconds using the syscall instruction with $v0 set to 4 to print a message.
  3. The program reads the input using the syscall instruction with $v0 set to 5 to read an integer.
  4. The program calculates the total time in seconds by multiplying the number of hours by the number of seconds in an hour (3600) and the number of minutes by the number of seconds in a minute (60). The result is stored in the register $t0.
  5. The program then prints the result by first printing a message and then the total time in seconds using the syscall instruction with $v0 set to 4 to print a message and $v0 set to 1 to print an integer.
  6. Finally, the program exits using the syscall instruction with $v0 set to 10.
User Jonathan Bouloux
by
8.3k points