In this program a clock that prints out the hours,minutes, and seconds with am/p
ID: 3835153 • Letter: I
Question
In this program a clock that prints out the hours,minutes, and seconds with am/pm (ante meridiem/post meridiem) in MIPS assembly language. So far I've got my program here printing the hours, minutes, and seconds but I don't know how to print the am/pm. Here is what I have so far:
.data
column: .asciiz ":" # to seperate hours, minutes, and seconds
space: .asciiz " " # next line to insert between numbers
.text
main:
#t0= hours
#t1=min
#t2=seconds
li $t0,0 #resets value to 0
hour:
addi $t0,$t0,1 #increment hours
bgt $t0,12,exit #clock stops when it reaches to 12
li $t1,0 #make minutes zero again
minutes:
addi $t1,$t1,1 #increment minutes
bgt $t1,59, hour #if minutes goes greater than 59 branches
li $t2,0 #make seconds zero again
seconds:
bgt $t2,59,minutes #if seconds goes greater than 59 branches
li $v0, 1 # prints the seconds
move $a0, $t0 #copies the register of hours
syscall # output string for hours
li $v0,4 # prints the clock
la $a0,column #load and print string
syscall # output string for the column in hours and minutes
li $v0, 1 # prints the minutes of the clock
move $a0, $t1 #copies the register of minutes
syscall # output string for seconds
li $v0,4 #load and print string
la $a0,column #load and print the columns of the clock
syscall # output string for the column in minutes and seconds
li $v0, 1 #iterates by loading the value and prints the counter for seconds
move $a0, $t2 #copies the register of seconds
syscall # output string of seconds
addi $v0, $zero, 4 # specify Print String service
la $a0, space
syscall # output string
addi $t2,$t2,1 # add 1 into seconds
j seconds
exit: #program exits
Explanation / Answer
.data
column: .asciiz ":" # to seperate hours, minutes, and seconds
space: .asciiz " " # next line to insert between numbers
am: .asciiz " am"
pm: .asciiz " pm"
.text
main:
#t0= hours
#t1=min
#t2=seconds
li $s0,0
li $t0,0 #resets value to 0
la $s1,am
hour:
addi $t0,$t0,1 #increment hours
bgt $t0,12,again #clock stops when it reaches to 12
li $t1,0 #make minutes zero again
minutes:
addi $t1,$t1,1 #increment minutes
bgt $t1,59, hour #if minutes goes greater than 59 branches
li $t2,0 #make seconds zero again
seconds:
bgt $t2,59,minutes #if seconds goes greater than 59 branches
li $v0, 1 # prints the seconds
move $a0, $t0 #copies the register of hours
syscall # output string for hours
li $v0,4 # prints the clock
la $a0,column #load and print string
syscall # output string for the column in hours and minutes
li $v0, 1 # prints the minutes of the clock
move $a0, $t1 #copies the register of minutes
syscall # output string for seconds
li $v0,4 #load and print string
la $a0,column #load and print the columns of the clock
syscall # output string for the column in minutes and seconds
li $v0, 1 #iterates by loading the value and prints the counter for seconds
move $a0, $t2 #copies the register of seconds
syscall # output string of seconds
addi $v0, $zero, 4 # specify Print String service
move $a0,$s1
syscall # output string
addi $v0, $zero, 4 # specify Print String service
la $a0, space
syscall # output string
addi $t2,$t2,1 # add 1 into seconds
j seconds
again:
addi $s0,$s0,1 #increment hours
beq $s0,2,exit
la $s1,pm
j hour
exit:
#program exits
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.