-
Notifications
You must be signed in to change notification settings - Fork 0
/
jw4074 (2).s
1431 lines (1182 loc) · 35.9 KB
/
jw4074 (2).s
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
count_char:
li $v0, 0
count_char_top:
lbu $t0, 0($a1) #save char of a1 in t0
addi $a1, $a1, 1
beqz $t0, count_char_exit #count_char_exit #t1 track the string
bne $t0, $a0, count_char_top #compare char saved in t0 with char pointed by a1.
addi $v0, $v0, 1
j count_char_top
count_char_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
minmax_chars:
lbu $v0, 0($a0)
lbu $v1, 0($a0)
min_max_chars_top:
addi $a0, $a0, 1
lbu $t1, 0($a0) #t1 points to the first char of t0(similar to strlen2)
beqz $t1, minmax_chars_exit
bgt $v0, $t1, update_min
j max_loop
max_loop:
blt $v1, $t1, update_max
j min_max_chars_top
update_min:
move $v0, $t1
j max_loop
update_max:
move $v1, $t1
j min_max_chars_top
minmax_chars_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
make_leaf:
sw $a0, 0($a2)
sw $a1, 4($a2)
sw $zero, 8($a2)
sw $zero, 12($a2)
sw $zero, 16($a2)
make_leaf_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
merge_roots:
sw $zero, 0($a2)
sw $zero, 8($a2)
sw $a0, 12($a2)
sw $a1, 16($a2)
lw $t1, 4($a0)
lw $t2, 4($a1)
add $t0, $t1, $t2
sw $t0, 4($a2)
li $t1, 1
sw $t1, 8($a0)
sw $t1, 8($a1)
merge_roots_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
count_roots:
li $v0, 0
beq $a0, $a1, count_roots_exit
count_roots_top:
beq $a0, $a1, count_roots_exit
lw $t0, 8($a0)
beqz $t0, add_count
addi $a0, $a0, 20
j count_roots_top
add_count:
addi $v0, $v0, 1
addi $a0, $a0, 20
j count_roots_top
count_roots_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
lightest_roots:
lbu $v0, 0($a0)
lbu $v1, 0($a1)
find_first_root:
lw $t0, 8($a0)
beqz $t0, update_first_root
addi $a0, $a0, 20
beq $a0, $a1, lightest_roots_exit
j find_first_root
update_first_root:
move $v0, $a0
j find_second_root
find_second_root:
addi $a0, $a0, 20
lw $t0, 8($a0)
beqz $t0, update_second_root
beq $a0, $a1, lightest_roots_exit
j find_second_root
update_second_root:
move $v1, $a0
j check_root_value
check_root_value:
lw $t1, 4($v0)
lw $t2, 4($v1)
blt $t1, $t2, lightest_roots_top
j swap_roots
swap_roots:
addi $sp, $sp,-4
sw $t0, 0($sp)
add $t0,$v0,$zero
add $v0,$v1,$zero
add $v1,$t0,$zero
lw $t0,4($sp)
addi $sp,$sp,4
j lightest_roots_top
lightest_roots_top:
beq $a0, $a1, lightest_roots_exit
addi $a0, $a0, 20
lw $t3, 8($a0)
beqz $t3, compare_min_roots
j lightest_roots_top
compare_min_roots:
lw $t4, 4($a0)
lw $t5, 4($v0)
lw $t6, 4($v1)
blt $t4, $t5, update_min_root
blt $t4, $t6, update_second_root
j lightest_roots_top
update_min_root:
blt $t5, $t6, update_secondmin_root
j lightest_roots_top
update_secondmin_root:
addi $sp, $sp,-4
sw $t0, 0($sp)
add $t0,$v0,$zero
add $v0,$v1,$zero
add $v1,$t0,$zero
lw $t0,4($sp)
addi $sp,$sp,4
move $v0, $a0
j lightest_roots_top
lightest_roots_top:
beq $a0, $a1, lightest_roots_exit
addi $a0, $a0, 20
lw $t3, 8($a0)
beqz $t3, compare_min_roots
j lightest_roots_top
compare_min_roots:
lw $t4, 4($a0)
lw $t5, 4($v0)
lw $t6, 4($v1)
blt $t4, $t5, update_min_root
blt $t4, $t6, update_second_root
j lightest_roots_top
update_min_root:
blt $t5, $t6, update_secondmin_root
j lightest_roots_top
update_secondmin_root:
addi $sp, $sp,-4
sw $t0, 0($sp)
add $t0,$v0,$zero
add $v0,$v1,$zero
add $v1,$t0,$zero
lw $t0,4($sp)
addi $sp,$sp,4
move $v0, $a0
j lightest_roots_top
lightest_roots_exit:
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
build_tree:
li $v0, 0
addi $sp, $sp,-24
sw $ra,0($sp)
sw $s0, 4($sp) #save a0 pointer of string
sw $s1, 8($sp) #save build_tree pointer $a1 as start of the array
sw $s2, 12($sp) #save build_tree pointer at end of the array(last node)
sw $s3, 16($sp) #save min
sw $s4, 20($sp) #save max
#initialize variable and save build tree pointers
move $s0, $a0 #$s0 saves the pointer $a0 at start of the string
move $s1, $a1 #$s1 saves the beginning pointer to the array
move $s2, $a1 #$s2 saves the end pointer to the array
#get_minmax
move $a0, $s0
jal minmax_chars
move $s3,$v0 #save returned min in s3
move $s4,$v1 #save returned max in s4
leaf_routine:
#get weight
move $a0, $s3 #pass on min char
move $a1, $s0 #pass on string
jal count_char
move $t3, $v0 #save weight in t3
#add leaf
move $a0, $s3 #pass on the char
move $a1, $t3 #pass on the weight
move $a2, $s2 #put new leaf in memory
jal make_leaf
#loop until max char
addi $s3, $s3, 1
bgt $s3, $s4, count_routine
addi $s2, $s2, 20
j leaf_routine
count_routine:
#count roots
move $a0, $s1 #pass begin of array pointer onto $a0
move $a1, $s2 #pass last node onto #a1
jal count_roots
move $t4, $v0
bgt $t4, $zero, merge_routine #if count_root returns >1, we merge
j update_result
merge_routine:
#find lightest roots
move $a0, $s1 #pass begin of array pointer onto $a0
move $a1, $s2 #pass last node onto #a1
jal lightest_roots
move $t5, $v0 #save lightest root in $t5
move $t6, $v1 #save second lightest root in $t6
#merge roots
addi $s2, $s2, 20 #move array end pointer to the next position after the last node
move $a0, $t5 #pass lightest root onto mrege_roots
move $a1, $t6 #pass second lightest root onto merge_roots
move $a2, $s2 #pass on newly merged root at $s2
jal merge_roots
j count_routine
update_result:
move $v0, $s2
j build_tree_exit
build_tree_exit:
move $a0, $s0
move $a1, $s1
lw $ra 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
addi $sp, $sp, 24
jr $ra
######################################################## DO NOT REMOVE THIS SEPARATOR
main:
# save regs
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, count_char_test1_in
la $a1, count_char_test1_out
jal count_char_tester
la $a0, count_char_test2_in
la $a1, count_char_test2_out
jal count_char_tester
la $a0, count_char_test3_in
la $a1, count_char_test3_out
jal count_char_tester
la $a0, count_char_test4_in
la $a1, count_char_test4_out
jal count_char_tester
la $a0, minmax_chars_test1_in
la $a1, minmax_chars_test1_out
jal minmax_chars_tester
la $a0, minmax_chars_test2_in
la $a1, minmax_chars_test2_out
jal minmax_chars_tester
la $a0, minmax_chars_test3_in
la $a1, minmax_chars_test3_out
jal minmax_chars_tester
la $a0, make_leaf_test1_in
la $a1, make_leaf_test1_out
jal make_leaf_tester
la $a0, make_leaf_test2_in
la $a1, make_leaf_test2_out
jal make_leaf_tester
la $a0, count_roots_test1_in
la $a1, count_roots_test1_out
jal count_roots_tester
la $a0, count_roots_test2_in
la $a1, count_roots_test2_out
jal count_roots_tester
jal print_newline
la $a0, merge_roots_test1_in
la $a1, merge_roots_test1_out
jal merge_roots_tester
la $a0, merge_roots_test2_in
la $a1, merge_roots_test2_out
jal merge_roots_tester
la $a0, lightest_roots_test1_in
la $a1, lightest_roots_test1_out
jal lightest_roots_tester
la $a0, lightest_roots_test2_in
la $a1, lightest_roots_test2_out
jal lightest_roots_tester
la $a0, build_tree_test1_in
la $a1, build_tree_test1_out
jal build_tree_tester
la $a0, build_tree_test2_in
la $a1, build_tree_test2_out
jal build_tree_tester
jal print_newline
jal print_newline
# one last test, build the abc_string tree again and decompress a tiny string
# should see 'cab' print to screen
la $a0, abc_string
la $a1, free_space
jal build_tree
move $a0, $v0
la $a1, cab_message
li $a2, 6
jal decompress
jal print_newline
# now, build the final tree, and use to decompress message
la $a0, english_frequency_string
la $a1, free_space
jal build_tree
move $a0, $v0
la $a1, final_message
li $a2, 70
jal decompress
# restore regs
lw $ra, 0($sp)
addi $sp, $sp, 4
# and return
jr $ra
count_char_tester:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, count_char_tester_msg
jal print_string
lw $a0, 0($s0)
jal print_char
jal print_comma
lw $a0, 4($s0)
jal print_string
jal print_newline
# print expected output
la $a0, tester_expecting_msg
jal print_string
lw $a0, 0($s1)
jal print_int
jal print_newline
# run test!
lw $a0, 0($s0)
lw $a1, 4($s0)
jal count_char
# check result against expected
lw $t0, 0($s1)
beq $v0, $t0, count_char_tester_pass
# error, save result
move $s0, $v0
# print error message and result
la $a0, tester_error_msg
jal print_string
move $a0, $s0
jal print_int
jal print_newline
# exit
li $v0, 10
syscall
count_char_tester_pass:
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
minmax_chars_tester:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, minmax_chars_tester_msg
jal print_string
lw $a0, 0($s0)
jal print_string
jal print_newline
# print expected result
la $a0, tester_expecting_msg
jal print_string
lw $a0, 0($s1)
jal print_char
jal print_comma
lw $a0, 4($s1)
jal print_char
jal print_newline
# run test!
lw $a0, 0($s0)
jal minmax_chars
# check result
lw $t0, 0($s1)
bne $v0, $t0, minmax_chars_tester_fail
lw $t0, 4($s1)
bne $v1, $t0, minmax_chars_tester_fail
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
minmax_chars_tester_fail:
# error, save result
move $s0, $v0
move $s1, $v1
# print error message and result
la $a0, tester_error_msg
jal print_string
move $a0, $s0
jal print_char
jal print_comma
move $a0, $s1
jal print_char
jal print_newline
# exit
li $v0, 10
syscall
make_leaf_tester:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, make_leaf_tester_msg
jal print_string
lw $a0, 0($s0)
jal print_char
jal print_comma
lw $a0, 4($s0)
jal print_int
jal print_comma
la $a0, free_space_msg
jal print_string
jal print_newline
# print expected result
la $a0, tester_expecting_msg
jal print_string
move $a0, $s1
jal print_tree
# run test!
lw $a0, 0($s0)
lw $a1, 4($s0)
la $a2, free_space
jal make_leaf
# check result
la $a0, free_space
move $a1, $s1
jal tree_match
bnez $v0, make_leaf_tester_pass
# print error
la $a0, tester_error_msg
jal print_string
la $a0, free_space
jal print_tree
jal print_newline
# exit
li $v0, 10
syscall
make_leaf_tester_pass:
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
merge_roots_tester:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, merge_roots_tester_msg
jal print_string
jal print_newline
lw $a0, 0($s0)
jal print_tree
lw $a0, 4($s0)
jal print_tree
la $a0, free_space_msg
jal print_string
jal print_newline
# print expected result
la $a0, tester_expecting_msg
jal print_string
jal print_newline
lw $a0, 0($s1)
jal print_tree
# run test!
lw $a0, 0($s0)
lw $a1, 4($s0)
la $a2, free_space
jal merge_roots
# check result
la $a0, free_space
lw $a1, 0($s1)
jal tree_match
bnez $v0, merge_roots_tester_pass
# print error
la $a0, tester_error_msg
jal print_string
la $a0, free_space
jal print_tree
jal print_newline
# exit
li $v0, 10
syscall
merge_roots_tester_pass:
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
count_roots_tester:
# save regs
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, count_roots_tester_msg
jal print_string
jal print_newline
# s2: pointer to current node in array
lw $s2, 0($s0)
count_roots_tester_loop_top:
lw $t0, 4($s0)
beq $s2, $t0, count_roots_tester_loop_exit
# print node
move $a0, $s2
jal print_tree
addi $s2, $s2, 20
b count_roots_tester_loop_top
count_roots_tester_loop_exit:
# print expected output
la $a0, tester_expecting_msg
jal print_string
lw $a0, 0($s1)
jal print_int
jal print_newline
# run test!
lw $a0, 0($s0)
lw $a1, 4($s0)
jal count_roots
# check result against expected
lw $t0, 0($s1)
beq $v0, $t0, count_roots_tester_pass
# error, save result
move $s0, $v0
# print error message and result
la $a0, tester_error_msg
jal print_string
move $a0, $s0
jal print_int
jal print_newline
# exit
li $v0, 10
syscall
count_roots_tester_pass:
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 16($sp)
addi $sp, $sp, 16
jr $ra
lightest_roots_tester:
# save regs
addi $sp, $sp, -16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case and inputs
la $a0, lightest_roots_tester_msg
jal print_string
jal print_newline
# s2: pointer to current node in array
lw $s2, 0($s0)
lightest_roots_tester_loop_top:
lw $t0, 4($s0)
beq $s2, $t0, lightest_roots_tester_loop_exit
# print node
move $a0, $s2
jal print_tree
addi $s2, $s2, 20
b lightest_roots_tester_loop_top
lightest_roots_tester_loop_exit:
# print expected result
la $a0, tester_expecting_msg
jal print_string
jal print_newline
lw $a0, 0($s1)
jal print_tree
lw $a0, 4($s1)
jal print_tree
# run test!
lw $a0, 0($s0)
lw $a1, 4($s0)
jal lightest_roots
# save returned pointers
move $s0, $v0
move $s2, $v1
# check if lightest matches expecting
move $a0, $s0
lw $a1, 0($s1)
jal tree_match
beqz $v0, lightest_roots_tester_fail
# lightest matches, check second lightest
move $a0, $s2
lw $a1, 4($s1)
jal tree_match
beqz $v0, lightest_roots_tester_fail
# passed, so print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
addi $sp, $sp, 16
jr $ra
lightest_roots_tester_fail:
# print error message and result
la $a0, tester_error_msg
jal print_string
jal print_newline
move $a0, $s0
jal print_tree
move $a0, $s2
jal print_tree
# exit
li $v0, 10
syscall
build_tree_tester:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save args
move $s0, $a0
move $s1, $a1
# print test case
la $a0, build_tree_tester_msg
jal print_string
jal print_newline
lw $a0, 0($s0)
jal print_string
jal print_newline
la $a0, free_space_msg
jal print_string
jal print_newline
# print expected output
la $a0, tester_expecting_msg
jal print_string
jal print_newline
lw $a0, 0($s1)
jal print_tree
# run test!
lw $a0, 0($s0)
la $a1, free_space
jal build_tree
move $s0, $v0
# check result
move $a0, $s0
lw $a1, 0($s1)
jal tree_match
bnez $v0, build_tree_tester_pass
# print error
la $a0, tester_error_msg
jal print_string
jal print_newline
move $a0, $s0
jal print_tree
# exit
li $v0, 10
syscall
build_tree_tester_pass:
# print pass message
la $a0, tester_pass_msg
jal print_string
# restore regs and return
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
# a0 root
# a1 other root
# return 0 if not matching, 1 otherwise
tree_match:
# save regs
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
# save arguments
move $s0, $a0
move $s1, $a1
# if both pointers null, return true
or $t0, $s0, $s1
beqz $t0, tree_match_exit_true
# know one or the other is non-null, so if either one is null, have mismatch
beqz $s0, tree_match_exit_false
beqz $s1, tree_match_exit_false
# now know both are non-null, so going to recurse
# check if left children match
lw $a0, 12($s0)
lw $a1, 12($s1)
jal tree_match
beqz $v0, tree_match_exit_false # if false, return false from whole thing
# check if right children match
lw $a0, 16($s0)
lw $a1, 16($s1)
jal tree_match
beqz $v0, tree_match_exit_false # if false, return false from whole thing
# children match, now compare contents of the node
lw $t0, 0($s0)
lw $t1, 0($s1)
bne $t0, $t1, tree_match_exit_false
lw $t0, 4($s0)
lw $t1, 4($s1)
bne $t0, $t1, tree_match_exit_false
lw $t0, 8($s0)
lw $t1, 8($s1)
bne $t0, $t1, tree_match_exit_false
tree_match_exit_true:
li $v0, 1
b tree_match_exit
tree_match_exit_false:
li $v0, 0
tree_match_exit:
# restore regs
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
addi $sp, $sp, 12
jr $ra
# a0: tree root
# a1: pointer to compressed text
# a2: num bits compressed
decompress:
addi $sp, $sp, -28
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $s5, 24($sp)
# save args
move $s0, $a0 # s0: root of tree
move $s1, $a1 # s1: curr spot in compressed words
move $s2, $a2 # s2: num bits to decompress
# s4: going to hold word of bits
move $s5, $a0 # s5: curr position in tree
# load first word of bits
lw $s4, 0($s1)
addi $s1, $s1, 4
decompress_top:
# if processed all bits, done
beqz $s2, decompress_exit
# decrement bits to decompress
addi $s2, $s2, -1
# t0: bitmask for this bit
li $t0, 1
sllv $t0, $t0, $s2
# t1: extracted bit
and $t1, $s4, $t0
# if that was last bit in word, need to load new word
li $t2, 1
bne $t2, $t0, decompress_use_extracted_bit
# load new word
lw $s4, 0($s1)
addi $s1, $s1, 4
decompress_use_extracted_bit:
# descend left or right
beqz $t1, decompress_descend_left
# descend right
lw $s5, 16($s5)
b decompress_leaf_check
decompress_descend_left:
lw $s5, 12($s5)
decompress_leaf_check:
# if child pointer, not at leaf
lw $t0, 12($s5)
bnez $t0, decompress_done_with_bit
# else at leaf, print char and reset to root