This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2.asm
1368 lines (1148 loc) · 27.2 KB
/
project2.asm
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.data
Prompt: .asciiz "----------Ban hay chon mot trong cac thao tac duoi day-----------\n"
Choice01: .asciiz "1. Xuat chuoi TIME theo dinh dang DD/MM/YYYY \n"
Choice02: .asciiz "2. Chuyen chuoi TIME thanh mot dinh dang khac \n"
Choice03: .asciiz "3. Cho biet ngay vua nhap la thu may trong tuan \n"
Choice04: .asciiz "4. Kiem tra nam trong chuoi TIME co phai la nam nhuan khong \n"
Choice05: .asciiz "5. Cho biet khoang thoi gian giua chuoi TIME_1 va TIME_2 \n"
Choice06: .asciiz "6. Cho biet 2 nam nhuan gan nhat voi nam trong chuoi TIME \n"
EndPrompt: .asciiz "----------------------------------------------------------------\n"
PrintOrder: .word Prompt, Choice01, Choice02, Choice03, Choice04, Choice05, Choice06, EndPrompt
AskChoice: .asciiz "Lua chon: "
AskType: .asciiz " A - MM/DD/YY;\n B - Month DD, YYYY\n C - DD Month, YYYY.\nLua chon: "
Result: .asciiz "Ket qua: "
promptDay: .asciiz "Nhap ngay Day: "
promptMonth: .asciiz "Nhap thang Month: "
promptYear: .asciiz "Nhap nam Year: "
InputNumberError: .asciiz "Loi nhap so. Xin nhap lai.\n"
InputDateError: .asciiz "Ngay khong hop le. Xin nhap lai.\n"
InputTypeError: .asciiz "Kieu dinh dang khong hop le. Xin nhap lai.\n"
MonthDayCount: .word 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
Jan: .asciiz "January"
Feb: .asciiz "February"
Mar: .asciiz "March"
Apr: .asciiz "April"
May: .asciiz "May"
Jun: .asciiz "June"
Jul: .asciiz "July"
Aug: .asciiz "August"
Sep: .asciiz "September"
Oct: .asciiz "October"
Nov: .asciiz "November"
Dec: .asciiz "December"
MonthName: .word Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
AnchorDate: .asciiz "02/04/2018"
Monday: .asciiz "Mon"
Tuesday: .asciiz "Tues"
Wednesday: .asciiz "Wed"
Thursday: .asciiz "Thurs"
Friday: .asciiz "Fri"
Saturday: .asciiz "Sat"
Sunday: .asciiz "Sun"
WeekDayName: .word Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
TempDate: .space 11
BufferDay: .space 2
BufferYear: .space 4
BufferInput: .space 50
BufferChoice: .space 50
TIME_1: .space 20 # TIME string: DD/MM/YYYY
TIME_2: .space 20
newline: .byte '\n'
.text
#------------------------------------------------------------
main:
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s4, 0($sp)
# ---- Ask for TIME_1
la $a0, TIME_1
jal GetDateInput
# ---- Print menu
la $t0, PrintOrder
addi $t1, $0, 0
addi $t2, $0, 8
Main.Loop:
beq $t1, $t2, Main.LoopEnd
add $t3, $t1, $0
sll $t3, $t3, 2
add $t3, $t0, $t3
lw $a0, 0($t3)
li $v0, 4
syscall
addi $t1, $t1, 1
j Main.Loop
Main.LoopEnd:
# ---- Ask for choice
AskChoiceInput:
la $a0, AskChoice
li $v0, 4
syscall
# Check number input error
jal GetNum
addi $t0, $0, -1
beq $v0, $t0, AskChoiceInput
# Check if choice is 1 to 6
addi $t0, $0, 1
slt $t0, $v0, $t0
bne $t0, $0, AskChoiceInput
addi $t0, $0, 6
slt $t0, $t0, $v0
bne $t0, $0, AskChoiceInput
# Save choice
addi $s0, $v0, 0
# ---- Process
addi $s4, $0, 1
beq $s0, $s4, ProcessChoice01
addi $s4, $s4, 1
beq $s0, $s4, ProcessChoice02
addi $s4, $s4, 1
beq $s0, $s4, ProcessChoice03
addi $s4, $s4, 1
beq $s0, $s4, ProcessChoice04
addi $s4, $s4, 1
beq $s0, $s4, ProcessChoice05
addi $s4, $s4, 1
beq $s0, $s4, ProcessChoice06
# ---- Choice 1
ProcessChoice01:
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
li $v0, 4
syscall
j Exit
# ---- Choice 2
ProcessChoice02:
GetConvertType:
la $a0, AskType
li $v0, 4
syscall
la $a0, BufferChoice
li $a1, 50
li $v0, 8
syscall
la $t0, BufferChoice
lb $t0, 0($t0)
addi $t1, $0, 65
slt $t1, $t0, $t1
beq $t1, $0, Check67
# Error message
la $a0, InputTypeError
li $v0, 4
syscall
j GetConvertType
Check67:
addi $t1, $0, 67
slt $t1, $t1, $t0
beq $t1, $0, CheckOnlyChar
# Error message
la $a0, InputTypeError
li $v0, 4
syscall
j GetConvertType
CheckOnlyChar:
la $t0, BufferChoice
lb $t0, 1($t0)
addi $t1, $t1, 10
beq $t0, $t1, DoneCheck
# Error message
la $a0, InputTypeError
li $v0, 4
syscall
j GetConvertType
DoneCheck:
la $t0, BufferChoice
la $a0, TIME_1
lb $a1, 0($t0)
jal Convert
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
li $v0, 4
syscall
j Exit
# ---- Choice 3
ProcessChoice03:
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
jal Weekday
la $a0, 0($v0)
li $v0, 4
syscall
j Exit
# ---- Choice 4
ProcessChoice04:
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
jal LeapYear
add $a0, $v0, $0
li $v0, 1
syscall
j Exit
# ---- Choice 5
ProcessChoice05:
la $a0, TIME_2
jal GetDateInput
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
la $a1, TIME_2
jal GetTime
add $a0, $v0, $0
li $v0, 1
syscall
j Exit
# ---- Choice 6
ProcessChoice06:
la $a0, Result
li $v0, 4
syscall
la $a0, TIME_1
jal NearestLeapYears
add $a0, $v0, $0
li $v0, 1
syscall
li $a0, ' '
li $v0, 11
syscall
add $a0, $v1, $0
li $v0, 1
syscall
j Exit
#------------------------------------------------------------
# int GetNum()
# Input: none
# Output: input number from user
GetNum:
# Get string input from user
la $a0, BufferInput
li $a1, 49
li $v0, 8
syscall
la $t1, BufferInput # store the address of BufferInput into $t1
add $t2, $0, $0 # sum
lb $t3, newline # character '\n' indicating stop
addi $t5, $0, 0 # to check ascii < '0'
addi $t6, $0, 10 # to check ascii > '9'
GetNum.Loop:
lb $t0, 0($t1)
beq $t0, $t3, GetNum.End # Done with character '\n'
addi $t0, $t0, -48
addi $t4, $0, 1000000000
slt $t7, $t2, $t4
beq $t7, $0, GetNum.Error # If sum > 1e9, raise Error
slt $t7, $t0, $t5
bne $t7, $0, GetNum.Error # If char < '0', raise Error
slt $t7, $t0, $t6
beq $t7, $0, GetNum.Error # If char > '9', raise Error
mult $t2, $t6 # Multiple by 10
mflo $t4
add $t2, $t4, $t0 # Add to sum
addi $t1, $t1, 1 # Next character
j GetNum.Loop
GetNum.Error:
addi $t2, $0, -1
GetNum.End:
add $v0, $t2, $0
jr $ra
#------------------------------------------------------------
# char* GetDateInput(char* TIME)
# Input: address to space to store result string
# Output: same address as input
GetDateInput:
# Prepare and store registers to stack
addi $sp, $sp, -20
sw $ra, 16($sp)
sw $a0, 12($sp)
sw $s1, 8($sp)
sw $s2, 4($sp)
sw $s3, 0($sp)
# Get day input
GetDate.GetDayInput:
la $a0, promptDay # load promptDay message into $a0 for syscall
li $v0, 4 # load syscall to print string
syscall # cout << promptDay
jal GetNum
addi $s1, $v0, 0
addi $t0, $0, -1
bne $v0, $t0, GetDate.GetMonthInput
# Error message
la $a0, InputNumberError
li $v0, 4
syscall
j GetDate.GetDayInput
# Get month input
GetDate.GetMonthInput:
la $a0, promptMonth # load promptMonth message into $a0 for syscall
li $v0, 4 # load syscall to print string
syscall # cout << promptMonth
jal GetNum
addi $s2, $v0, 0
addi $t0, $0, -1
bne $v0, $t0, GetDate.GetYearInput
# Error message
la $a0, InputNumberError
li $v0, 4
syscall
j GetDate.GetMonthInput
# Get year input
GetDate.GetYearInput:
la $a0, promptYear # load promptMonth message into $a0 for syscall
li $v0, 4 # load syscall to print string
syscall # cout << promptMonth
jal GetNum
addi $s3, $v0, 0
addi $t0, $0, -1
bne $v0, $t0, GetDate.CheckDate
# Error message
la $a0, InputNumberError
li $v0, 4
syscall
j GetDate.GetYearInput
# Check legal date
GetDate.CheckDate:
addi $a0, $s1, 0
addi $a1, $s2, 0
addi $a2, $s3, 0
jal CheckDate
bne $v0, $0, GetDate.Convert
# Error message
la $a0, InputDateError
li $v0, 4
syscall
j GetDate.GetDayInput
# Convert to string
GetDate.Convert:
addi $a0, $s1, 0
addi $a1, $s2, 0
addi $a2, $s3, 0
lw $a3, 12($sp)
jal Date
# Return value
addi $v0, $v0, 0
# Restore stack
lw $s3, 0($sp)
lw $s2, 4($sp)
lw $s1, 8($sp)
lw $a0, 12($sp)
lw $ra, 16($sp)
addi $sp, $sp, 20
# Return
jr $ra
#------------------------------------------------------------
# char* Date(int day, int month, int year, char* TIME)
# Input: three number day, month, year and address to space to store result string
# Output: string of date from input day, month, year
Date:
addi $t7, $a3, 0 # index: 0
addi $t0, $0, 10 # used to /10 for number
la $t3, '/'
# convert int Day to char
div $a0, $t0
mflo $t1 # Quotient
mfhi $t2 # Remainder
addi $t1, $t1, 48 # $t1 = $t1 + '0'
addi $t2, $t2, 48 # $t2 = $t2 + '0'
sb $t1, 0($t7)
addi $t7, $t7, 1 # index: 1
sb $t2, 0($t7)
addi $t7, $t7, 1 # index: 2
# Insert /
sb $t3, 0($t7)
addi $t7, $t7, 1 # index: 3
# convert int Month to char
div $a1, $t0
mflo $t1 # Quotient
mfhi $t2 # Remainder
addi $t1, $t1, 48 # $t1 = $t1 + '0'
addi $t2, $t2, 48 # $t2 = $t2 + '0'
sb $t1, 0($t7)
addi $t7, $t7, 1 # index: 4
sb $t2, 0($t7)
addi $t7, $t7, 1 # index: 5
# Insert /
sb $t3, 0($t7)
addi $t7, $t7, 5 # index: 10
# Set NULL char
sb $0, 0($t7)
addi $t7, $t7, -1
# convert int Year to char
add $t1, $a2, $0
addi $t4, $a3, 5
Date.LoopYear:
beq $t7, $t4, Date.LoopYearEnd # From index 9 to 6
div $t1, $t0
mflo $t1
mfhi $t2
addi $t2, $t2, 48
sb $t2, 0($t7)
addi $t7, $t7, -1
j Date.LoopYear
Date.LoopYearEnd:
# return
addi $v0, $a3, 0
jr $ra
#------------------------------------------------------------
# CheckDate(int day, int month, int year)
# Input: three numbers day, month, year representing time
# Output: 1 if legal date, 0 otherwise
CheckDate:
# Push to stack
addi $sp, $sp, -16
sw $ra, 12($sp)
sw $a0, 8($sp)
sw $a1, 4($sp)
sw $a2, 0($sp)
# ---- Check year
# Check year < 1900
lw $t0, 0($sp)
addi $t1, $0, 1900
slt $t1, $t0, $t1
bne $t1, $0, CheckDate.Error
# Check year > 9999
addi $t1, $0, 9999
slt $t1, $t1, $t0
bne $t1, $0, CheckDate.Error
# ---- Check month
# Check month < 1
lw $t0, 4($sp)
addi $t1, $0, 1
slt $t1, $t0, $t1
bne $t1, $0, CheckDate.Error
# Check month > 12
addi $t1, $0, 12
slt $t1, $t1, $t0
bne $t1, $0, CheckDate.Error
# ---- Check day
# Check day < 1
lw $t0, 8($sp)
addi $t1, $0, 1
slt $t1, $t0, $t1
bne $t1, $0, CheckDate.Error
# Check leap year
lw $a0, 0($sp)
jal LeapYearUtil
lw $t2, 8($sp)
beq $v0, $0, CheckDate.EndCheckLeapYear
# If leap year, check Feb
lw $t3, 4($sp)
addi $t3, $t3, -2
bne $t3, $0, CheckDate.EndCheckLeapYear
# If Feb, decrease day count (check d <= 29 <=> d - 1 <= 28 = MonthDayCount[2])
addi $t2, $t2, -1
CheckDate.EndCheckLeapYear:
# Get month's day count
la $t0, MonthDayCount
lw $t1, 4($sp)
sll $t1, $t1, 2
add $t1, $t0, $t1
lw $t1, 0($t1)
# Check if d < MonthDayCount[m]
slt $t1, $t1, $t2
bne $t1, $0, CheckDate.Error
# If all okay, return 1 (TRUE)
addi $v0, $0, 1
j CheckDate.End
CheckDate.Error:
# If error, return 0 (FALSE)
addi $v0, $0, 0
CheckDate.End:
# Restore stack and return
lw $a2, 0($sp)
lw $a1, 4($sp)
lw $a0, 8($sp)
lw $ra, 12($sp)
addi $sp, $sp, 16
jr $ra
#------------------------------------------------------------
# Convert(char* TIME, char type)
# Input: string of date to convert and conversion type
# Output: string of date after conversion
Convert:
# Prepare stack and store registers
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $a0, 4($sp)
sw $a1, 0($sp)
# Get conversion type
lw $a0, 4($sp)
# Convert based on type
addi $t0, $0, 65
beq $a1, $t0, Convert.ChoiceA
addi $t0, $0, 66
beq $a1, $t0, Convert.ChoiceB
addi $t0, $0, 67
beq $a1, $t0, Convert.ChoiceC
Convert.ChoiceA:
jal ConvertA
j Convert.End
Convert.ChoiceB:
jal ConvertB
j Convert.End
Convert.ChoiceC:
jal ConvertC
Convert.End:
# Return value
addi $v0, $v0, 0
# Restore stack
lw $a1, 0($sp)
lw $a0, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
# Return
jr $ra
# ConvertA(char* TIME)
# Input: string of date to convert to type A
# Output: string of date after conversion
ConvertA:
add $t1, $0, $a0 # the address of TIME array ---- Index: 0
lb $t2, 0($t1) # t2 saves the first value of day
lb $t3, 1($t1) # t3 saves the second value of day
lb $t4, 3($t1) # t4 saves the first value of month
lb $t5, 4($t1) # t5 saves the second value of month
sb $t2, 3($a0)
sb $t3, 4($a0)
sb $t4, 0($a0)
sb $t5, 1($a0)
add $v0, $a1, $0
jr $ra
# ConvertB(char* TIME)
# Input: string of date to convert to type B
# Output: string of date after conversion
ConvertB:
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
# Get Month
lw $a0, 0($sp)
jal Month
add $t1, $v0, $0
lw $t0, 0($sp)
# Save day
la $t5, BufferDay
lb $t6, 0($t0)
sb $t6, 0($t5)
lb $t6, 1($t0)
sb $t6, 1($t5)
# Save year
la $t5, BufferYear
lb $t6, 6($t0)
sb $t6, 0($t5)
lb $t6, 7($t0)
sb $t6, 1($t5)
lb $t6, 8($t0)
sb $t6, 2($t5)
lb $t6, 9($t0)
sb $t6, 3($t5)
# Month name
la $t2, MonthName
addi $t1, $t1, -1
sll $t1, $t1, 2
add $t1, $t2, $t1
lw $t1, ($t1)
add $t3, $0, $0
ConvertB.Loop:
add $t4, $t1, $t3
add $t2, $t0, $t3
lb $t4, ($t4)
beq $t4, $0, ConvertB.LoopEnd
sb $t4, ($t2)
addi $t3, $t3, 1
j ConvertB.Loop
ConvertB.LoopEnd:
# Add Spacebar
add $t2, $t0, $t3
addi $t4, $0, 32
sb $t4, ($t2)
addi $t3, $t3, 1
# Add day
la $t1, BufferDay
add $t2, $t0, $t3
lb $t4, ($t1)
sb $t4, ($t2)
addi $t3, $t3, 1
add $t2, $t0, $t3
lb $t4, 1($t1)
sb $t4, ($t2)
addi $t3, $t3, 1
# Add ,
add $t2, $t0, $t3
addi $t4, $0, 44
sb $t4, ($t2)
addi $t3, $t3, 1
# Add Spacebar
add $t2, $t0, $t3
addi $t4, $0, 32
sb $t4, ($t2)
addi $t3, $t3, 1
# Add year
la $t1, BufferYear
addi $t5, $0, 0
addi $t6, $0, 4
ConvertB.LoopYear:
beq $t5, $t6, ConvertB.LoopYearEnd
add $t2, $t0, $t3
add $t4, $t1, $t5
lb $t4, ($t4)
sb $t4, ($t2)
addi $t3, $t3, 1
addi $t5, $t5, 1
j ConvertB.LoopYear
ConvertB.LoopYearEnd:
add $v0, $t0, $0
lw $a0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
# ConvertC(char* TIME)
# Input: string of date to convert to type C
# Output: string of date after conversion
ConvertC:
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $a0, 0($sp)
# Get Month
lw $a0, 0($sp)
jal Month
add $t1, $v0, $0
lw $t0, 0($sp)
# Save year
la $t5, BufferYear
lb $t6, 6($t0)
sb $t6, 0($t5)
lb $t6, 7($t0)
sb $t6, 1($t5)
lb $t6, 8($t0)
sb $t6, 2($t5)
lb $t6, 9($t0)
sb $t6, 3($t5)
addi $t3, $0, 2
# Add Spacebar
add $t2, $t0, $t3
addi $t4, $0, 32
sb $t4, ($t2)
addi $t3, $t3, 1
# Month name
la $t2, MonthName
addi $t1, $t1, -1
sll $t1, $t1, 2
add $t1, $t2, $t1
lw $t1, ($t1)
addi $t5, $0, 0
ConvertC.Loop:
add $t4, $t1, $t5
add $t2, $t0, $t3
lb $t4, ($t4)
beq $t4, $0, ConvertC.LoopEnd
sb $t4, ($t2)
addi $t3, $t3, 1
addi $t5, $t5, 1
j ConvertC.Loop
ConvertC.LoopEnd:
# Add ,
add $t2, $t0, $t3
addi $t4, $0, 44
sb $t4, ($t2)
addi $t3, $t3, 1
# Add Spacebar
add $t2, $t0, $t3
addi $t4, $0, 32
sb $t4, ($t2)
addi $t3, $t3, 1
# Add year
la $t1, BufferYear
addi $t5, $0, 0
addi $t6, $0, 4
ConvertC.LoopYear:
beq $t5, $t6, ConvertC.LoopYearEnd
add $t2, $t0, $t3
add $t4, $t1, $t5
lb $t4, ($t4)
sb $t4, ($t2)
addi $t3, $t3, 1
addi $t5, $t5, 1
j ConvertC.LoopYear
ConvertC.LoopYearEnd:
add $v0, $t0, $0
lw $a0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 8
jr $ra
#------------------------------------------------------------
# int DaysFromYearStart(char* TIME);
# Input: String DD/MM/YYYY representing a date
# Output: Number of days from the start of the year (ex: 01/01/1998 -> 0)
DaysFromYearStart:
# ---- Push to stack
addi $sp, $sp, -28
sw $ra, 24($sp) # Push $ra to stack
sw $a0, 20($sp) # Push $a0 (TIME) to stack
# Store saved registers
sw $s0, 16($sp)
sw $s1, 12($sp)
sw $s2, 8($sp)
sw $s3, 4($sp)
sw $s4, 0($sp)
# ---- Prepare
# Create array of day count months, store address in $s0
la $s0, MonthDayCount
# Get year
lw $a0, 20($sp) # Load TIME to $a0
jal Year # Year(TIME)
add $s1, $0, $v0 # $s1 = Year(TIME)
# Get month
lw $a0, 20($sp) # Load TIME to $a0
jal Month # Month(TIME)
add $s2, $0, $v0 # $t1 = Month(TIME)
# Get day
lw $a0, 20($sp) # Load TIME to $a0
jal Day # Day(TIME)
add $s3, $0, $v0 # $t1 = Day(TIME)
# Return value
add $s4, $0, $0
# ---- Consider LeapYear case
# Check leap year
add $a0, $0, $s1 # Load year to $a0
jal LeapYearUtil # LeapYear(year)
# If not leap year, do nothing
beq $v0, $0, DaysFromYearStart.EndCheckLeapYear
# If leap year,
addi $t0, $t0, 2
slt $t0, $t0, $s2
# If month <= 2, do nothing
beq $t0, $0, DaysFromYearStart.EndCheckLeapYear
# If month > 2, add 1 to return
addi $s4, $s4, 1
DaysFromYearStart.EndCheckLeapYear:
# ----- Month
# Iterate month, increasing day count, from previous month
DaysFromYearStart.LoopMonth:
addi $s2, $s2, -1 # From previous month
beq $s2, $0, DaysFromYearStart.EndLoopMonth # If month == 0, break
# Get day count of that month
sll $t0, $s2, 2 # $t0 = $t0 * 4
add $t0, $s0, $t0 # $t0 = &MonthDayCount[$t0]
lw $t0, 0($t0) # $t0 = *$t0
# Add to return value
add $s4, $s4, $t0 # $s4 = $s4 + $t2
j DaysFromYearStart.LoopMonth
DaysFromYearStart.EndLoopMonth:
# ----- Day
addi $s3, $s3, -1 # $t1-- (previous day)
add $s4, $s4, $s3 # $s4 = $s4 + $s3
# ----- Return
add $v0, $0, $s4
# ----- Restore stack
lw $s4, 0($sp)
lw $s3, 4($sp)
lw $s2, 8($sp)
lw $s1, 12($sp)
lw $s0, 16($sp)
lw $a0, 20($sp)
lw $ra, 24($sp)
addi $sp, $sp, 28
jr $ra
#------------------------------------------------------------
# int DaysBetweenYearStart(int year1, int year2)
# Input: two years year1 < year2
# Output: Number of days between two years
DaysBetweenYearStarts:
# ---- Push to stack
addi $sp, $sp, -24
sw $ra, 20($sp) # Push $ra to stack
sw $a0, 16($sp) # Push $a0 (year1) to stack
sw $a1, 12($sp) # Push $a1 (year2) to stack
# Store saved registers
sw $s0, 8($sp)
sw $s1, 4($sp)
sw $s2, 0($sp)
# Return value
add $s0, $0, $0
# ---- Iterate year
lw $s1, 16($sp)
lw $s2, 12($sp)
DaysBetweenYearStarts.LoopYear:
beq $s1, $s2, DaysBetweenYearStarts.EndLoopYear
addi $s2, $s2, -1
# Check leap year
add $a0, $0, $s2 # Load $t1 (year) to $a0
jal LeapYearUtil # LeapYear(year)
# If not leap year, do nothing
beq $v0, $0, DaysBetweenYearStarts.EndCheckLeapYear
# If leap year, add 1 to return value
addi $s0, $s0, 1
DaysBetweenYearStarts.EndCheckLeapYear:
addi $s0, $s0, 365
j DaysBetweenYearStarts.LoopYear
DaysBetweenYearStarts.EndLoopYear:
# ----- Return
add $v0, $0, $s0
# ----- Restore stack
lw $s2, 0($sp)
lw $s1, 4($sp)
lw $s0, 8($sp)
lw $a1, 12($sp)
lw $a0, 16($sp)
lw $ra, 20($sp)
addi $sp, $sp, 24
jr $ra
#------------------------------------------------------------
# int IsBefore(int days1, int year1, int days2, int year2);
# Input: two number from each date, the first is the number of days from the
# start of that year, the latter is the year
# Output: 1 if TIME1 < TIME2, 0 otherwise
IsBefore:
# If year1 < year2 return true
addi $v0, $0, 1
slt $t0, $a1, $a3
bne $t0, $0, IsBefore.End
# Else, if year1 > year2 return false
addi $v0, $0, 0
slt $t0, $a3, $a1
bne $t0, $0, IsBefore.End
# Else (year1 == year2), if days1 < days2 return true
addi $v0, $0, 1
slt $t0, $a0, $a2
bne $t0, $0, IsBefore.End
# Else (year1 == year2, days1 >= days2) return false
addi $v0, $0, 0
IsBefore.End:
jr $ra
#------------------------------------------------------------
# int GetTime(char* TIME1, char* TIME2);
# Input: two strings DD/MM/YYYY representing two dates
# Output: number of days between two dates
GetTime:
# ---- Push to stack
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $a0, 4($sp)
sw $a1, 0($sp)
# ---- Call util
lw $a0, 4($sp)
lw $a1, 0($sp)
jal GetTimeUtil
# ---- Get only the difference
add $v0, $v0, $0
# ----- Restore stack
lw $a1, 0($sp)
lw $a0, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12