-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerFile.asm
More file actions
86 lines (66 loc) · 2.1 KB
/
timerFile.asm
File metadata and controls
86 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
.data
timeString: .asciiz "\nCurrent Time: "
timeInitial: .asciiz "00:00\n"
colon: .asciiz ":"
zero: .asciiz "0"
.text
start_program:
j skipping # This label is to pretty much stop this file from running at first whenever the main file runs
# it creates a huge problem, thus I want the file to only jump to a specific label at a specific moment,
# not whenever the main file starts
initialize_time:
li $v0, 30 # syscall to get time
syscall
move $s4, $a0 # store the time in $s4
jr $ra
update_time:
addi $sp, $sp, -4 #allocate space on the stack
sw $ra, 0($sp)
# Get current time in milliseconds
li $v0, 30
syscall
move $t5, $a0 # Store current time in $t5 (changed from $v0 to $a0)
# Calculate elapsed time
sub $t5, $t5, $s4 # Current time - Start time = Elapsed time (in milliseconds)
# Convert to seconds
li $t6, 1000
div $t5, $t6 # Divide by 1000 to get seconds
mflo $t5 # Get seconds in $t5
# Calculate minutes and remaining seconds
li $t6, 60
div $t5, $t6
mflo $t8 # Minutes in $t8
mfhi $t9 # Remaining seconds in $t9
# Print minutes with leading zero if needed
li $t6, 10
bge $t8, $t6, print_minutes #branches to print_minute if the two registers are equal
li $v0, 4
la $a0, zero
syscall
print_minutes:
li $v0, 1 # prints the minutes
move $a0, $t8
syscall
# Print colon
li $v0, 4
la $a0, colon
syscall
# Print seconds with leading zero if needed
li $t6, 10
bge $t9, $t6, print_seconds #branches if the condition is true
li $v0, 4
la $a0, zero
syscall
print_seconds:
li $v0, 1 #prints the seconds
move $a0, $t9
syscall
li $v0, 4 #prints a new line
la $a0, newLine
syscall
# Restore return address and return
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
skipping:
# does nothing but skip to the end