Using Conway’s Algorithm to Determine the Doomsday
.data
prompt: .asciiz "Please enter a year: "
errmsg: .asciiz "Error: year must be between 1800 and 2199"
result: .asciiz "\nDoomsday: "
days: .asciiz "Sunday "
.asciiz "Monday "
.asciiz "Tuesday "
.asciiz "Wednesday "
.asciiz "Thursday "
.asciiz "Friday "
.asciiz "Saturday "
.text
.globl main
main:
la $a0, prompt # prompt user to give a year
li $v0, 4
syscall
li $v0, 5 # read an integer
syscall
move $s0, $v0 # save year in s0
li $t0, 100
div $s0, $t0 # divide year/100 to get lowest 2 digits
mfhi $t0 # get 2 digits of year in t0
li $t1, 12
div $t0, $t1 # divide digits / 12
mflo $s1 # s1 will be a (quotient of y/12)
mfhi $s2 # s2 wil be b (remainder of y/12)
srl $s3, $s2, 2 # divide remainder by 4 using 2 shifts right, save c in s3
add $s4, $s1, $s2 # sum d = a + b + c and save d in s4
add $s4, $s4, $s3
li $t0, 7 # divide sum / 7
div $s4, $t0
mfhi $s5 # save remainder os sum/7 in s5
blt $s0, 1800, error # if year < 1800, it's an error
ble $s0, 1899, friday # if year >= 1800 and year <=1899, use anchor friday
ble $s0, 1999, wednesday # if year >= 1900 and year <=1999, use anchor wednesday
ble $s0, 2099, tuesday # if year >= 2000 and year <=2099, use anchor tuesday
ble $s0, 2199, getDay # if year >= 2100 and year <=2199, get day, since index is 0
error:
la $a0, errmsg # else, it's an invalid year, print an error message
li $v0, 4
syscall
j exit
friday: # anchor friday has index 5
addi $s5, $s5, 5
j getDay
wednesday: # anchor wednesday has index 3
addi $s5, $s5, 3
j getDay
tuesday: # anchor tuesday has index 2
addi $s5, $s5, 2
getDay:
la $a0, result # print result
li $v0, 4
syscall
li $t0, 7 # calculate mod 7 of sum to get doomsday index
div $s5, $t0
mfhi $t0
la $a0, days # point to start of array of day names
sll $t0, $t0, 4 # multiply index * 16 to get offset in days array
add $a0, $a0, $t0 # add to start of array to get pointer to day name
li $v0, 4 # print day name
syscall
exit:
li $v0, 10 # exit the program
syscall