From 19d88bfaa9c303c21e307577dbe19f55b0311f0e Mon Sep 17 00:00:00 2001 From: unlockable Date: Fri, 3 May 2024 23:57:11 +0800 Subject: [PATCH] Better format. --- 1/homework/exp1/exp1_1.asm | 72 +++++++++++------------ 1/homework/exp1/exp1_2.asm | 39 +++++++------ 1/homework/exp1/exp1_3.asm | 102 ++++++++++++++++----------------- 1/homework/exp1/exp1_4.asm | 54 ++++++++--------- 1/homework/exp2/merge_sort.asm | 2 +- 5 files changed, 136 insertions(+), 133 deletions(-) diff --git a/1/homework/exp1/exp1_1.asm b/1/homework/exp1/exp1_1.asm index cf589eb..a006adf 100644 --- a/1/homework/exp1/exp1_1.asm +++ b/1/homework/exp1/exp1_1.asm @@ -1,42 +1,42 @@ -.data -file_buff: .space 8 # A space of 2 bytes -.align 3 -input_file_name: .asciiz "a.in" -.align 3 -output_file_name: .asciiz "a.out" -.align 3 -.text +.data +file_buff: .space 8 # A space of 2 bytes + .align 3 +input_file_name: .asciiz "a.in" + .align 3 +output_file_name: .asciiz "a.out" + .align 3 +.text -la $a0, input_file_name -li $a1, 0 # set mode as read -li $a2, 0 # meaning nothing, really -li $v0, 13 # open file -syscall #now $v0 is the file handler +la $a0, input_file_name +li $a1, 0 # set mode as read +li $a2, 0 # meaning nothing, really +li $v0, 13 # open file +syscall #now $v0 is the file handler -move $a0, $v0 #move file handler to a0 as first syscall arg -la $a1, file_buff -li $a2, 8 # read in 8 bytes -li $v0, 14 +move $a0, $v0 #move file handler to a0 as first syscall arg +la $a1, file_buff +li $a2, 8 # read in 8 bytes +li $v0, 14 syscall -li $v0, 16 -syscall # close file +li $v0, 16 +syscall # close file -la $a0, output_file_name -li $a1, 1 # set mode as write -li $a2, 0 # meaning nothing, really -li $v0, 13 # open file -syscall +la $a0, output_file_name +li $a1, 1 # set mode as write +li $a2, 0 # meaning nothing, really +li $v0, 13 # open file +syscall -move $a0, $v0 -la $a1, file_buff -li $a2, 8 # write 8 bytes -li $v0, 15 # write file -syscall -li $v0, 16 -syscall +move $a0, $v0 +la $a1, file_buff +li $a2, 8 # write 8 bytes +li $v0, 15 # write file +syscall +li $v0, 16 +syscall -li $v0 5 # read integer -syscall -addi $a0, $v0, 10 # a0 = v0 + 10 -li $v0 1 # print integer -syscall +li $v0, 5 # read integer +syscall +addi $a0, $v0, 10 # a0 = v0 + 10 +li $v0, 1 # print integer +syscall diff --git a/1/homework/exp1/exp1_2.asm b/1/homework/exp1/exp1_2.asm index a07ab41..fd16023 100644 --- a/1/homework/exp1/exp1_2.asm +++ b/1/homework/exp1/exp1_2.asm @@ -1,26 +1,29 @@ .data .text -li $v0, 5 # read in i -syscall -move $s1, $v0 # use s1 to store i + li $v0, 5 # read in i + syscall + move $s1, $v0 # use s1 to store i -li $v0, 5 # read in j -syscall -move $s2, $v0 # use s2 to store j + li $v0, 5 # read in j + syscall + move $s2, $v0 # use s2 to store j -sub $s1, $zero, $s1 # i = -i + sub $s1, $zero, $s1 # i = -i -bgez $s2, no_neg_j # if >= 0, then no neg -sub $s2, $zero, $s2 # j = -j + bgez $s2, no_neg_j # if >= 0, then no neg + sub $s2, $zero, $s2 # j = -j -no_neg_j: move $s0, $zero # use s0 as temp, temp = 0 -count_up_j: bge $s0, $s2, done_count_up # if temp >= j, stop iteration -addi $s1, $s1, 1 # i = i + 1 -addi $s0, $s0, 1 # ++temp -j count_up_j +no_neg_j: + move $s0, $zero # use s0 as temp, temp = 0 +count_up_j: + bge $s0, $s2, done_count_up # if temp >= j, stop iteration + addi $s1, $s1, 1 # i = i + 1 + addi $s0, $s0, 1 # ++temp + j count_up_j -done_count_up: move $a0, $s1 -li $v0 1 -syscall +done_count_up: + move $a0, $s1 + li $v0, 1 + syscall -move $v0, $s1 # set return value \ No newline at end of file + move $v0, $s1 # set return value \ No newline at end of file diff --git a/1/homework/exp1/exp1_3.asm b/1/homework/exp1/exp1_3.asm index 3f05bcb..3b1ecc9 100644 --- a/1/homework/exp1/exp1_3.asm +++ b/1/homework/exp1/exp1_3.asm @@ -1,66 +1,66 @@ .data .text -li $v0 5 # read in num -syscall + li $v0, 5 # read in num + syscall -move $s0, $v0 # $s0 is n -move $a0, $v0 -li $v0, 9 # malloc -syscall -move $s1, $v0 # $s1 = a + move $s0, $v0 # $s0 is n + move $a0, $v0 + li $v0, 9 # malloc + syscall + move $s1, $v0 # $s1 = a -move $t0, $zero # a0 = 0 + move $t0, $zero # a0 = 0 -move $t0, $zero # i = 0 -move $t1, $s1 #t1 = a -read_in_num: - bge $t0, $s0, done_read_in_num # i >= n, break - li $v0, 5 # read in num - syscall + move $t0, $zero # i = 0 + move $t1, $s1 #t1 = a +read_in_num: + bge $t0, $s0, done_read_in_num # i >= n, break + li $v0, 5 # read in num + syscall - sw $v0, 0($t1) # a[i] = v0 - addi $t0, $t0, 1 # i++ - addi $t1, $t1, 4 # t1 += 4 - j read_in_num + sw $v0, 0($t1) # a[i] = v0 + addi $t0, $t0, 1 # i++ + addi $t1, $t1, 4 # t1 += 4 + j read_in_num -done_read_in_num: -srl $s2, $s0, 1 # s1 = n / 2 -move $t0, $zero # i = 0 -move $t1, $s1 +done_read_in_num: + srl $s2, $s0, 1 # s1 = n / 2 + move $t0, $zero # i = 0 + move $t1, $s1 -subi $t2, $s0, 1 # t2 = n - 1 -sll $t2, $t2, 2 # t2 *= 4 -add $t2, $t2, $s1 -reverse: - bge $t0, $s2, done_reverse # i >= n / 2, break - lw $t3, 0($t1) # t3 = a[i] - addi $t3, $t3, 1 # t3 += 1 + subi $t2, $s0, 1 # t2 = n - 1 + sll $t2, $t2, 2 # t2 *= 4 + add $t2, $t2, $s1 +reverse: + bge $t0, $s2, done_reverse # i >= n / 2, break + lw $t3, 0($t1) # t3 = a[i] + addi $t3, $t3, 1 # t3 += 1 - lw $t4, 0($t2) # t4 = a[n - i - 1] - addi $t4, $t4, 1 # t4 += 1 + lw $t4, 0($t2) # t4 = a[n - i - 1] + addi $t4, $t4, 1 # t4 += 1 - sw $t4, 0($t1) - sw $t3, 0($t2) + sw $t4, 0($t1) + sw $t3, 0($t2) - addi $t0, $t0, 1 - addi $t1, $t1, 4 - subi $t2, $t2, 4 - j reverse + addi $t0, $t0, 1 + addi $t1, $t1, 4 + subi $t2, $t2, 4 + j reverse -done_reverse: -move $t0, $zero -move $t1, $s1 +done_reverse: + move $t0, $zero + move $t1, $s1 -output: - bge $t0, $s0 done_output - lw $a0, 0($t1) - li $v0 1 - syscall +output: + bge $t0, $s0, done_output + lw $a0, 0($t1) + li $v0, 1 + syscall - addi $t0, $t0, 1 - addi $t1, $t1, 4 - j output + addi $t0, $t0, 1 + addi $t1, $t1, 4 + j output -done_output: -li $v0 17 -syscall \ No newline at end of file +done_output: + li $v0, 17 + syscall \ No newline at end of file diff --git a/1/homework/exp1/exp1_4.asm b/1/homework/exp1/exp1_4.asm index e2c91f6..48d9240 100644 --- a/1/homework/exp1/exp1_4.asm +++ b/1/homework/exp1/exp1_4.asm @@ -2,34 +2,34 @@ .text main: - li $v0, 5 # read in - syscall - move $s0, $v0 # s0 is the n - move $a0, $s0 # set the first call arg as n - jal hanoi # call proc - move $a0, $v0 # print out - li $v0, 1 - syscall - li $v0, 17 - syscall + li $v0, 5 # read in + syscall + move $s0, $v0 # s0 is the n + move $a0, $s0 # set the first call arg as n + jal hanoi # call proc + move $a0, $v0 # print out + li $v0, 1 + syscall + li $v0, 17 + syscall hanoi: - li $t0, 1 - ble $a0, $t0, hanoi_bottom # if n <= 1, return - j hanoi_recur + li $t0, 1 + ble $a0, $t0, hanoi_bottom # if n <= 1, return + j hanoi_recur hanoi_bottom: - li $v0, 1 # return 1 - jr $ra + li $v0, 1 # return 1 + jr $ra hanoi_recur: - subi $sp, $sp, 8 # move sp down by 2 word - sw $s0, 4($sp) # first word is the n in caller - sw $ra, 0($sp) # second word is the return addr of this callee - move $s0, $a0 - subi $a0, $s0, 1 - jal hanoi - sll $v0, $v0, 1 # hanoi(n - 1) * 2 - ori $v0, $v0, 1 # hanoi(n - 1) * 2 + 1 - lw $s0, 4($sp) # reverse s0 - lw $ra, 0($sp) # reverse ra - addi $sp, $sp, 8 - jr $ra + subi $sp, $sp, 8 # move sp down by 2 word + sw $s0, 4($sp) # first word is the n in caller + sw $ra, 0($sp) # second word is the return addr of this callee + move $s0, $a0 + subi $a0, $s0, 1 + jal hanoi + sll $v0, $v0, 1 # hanoi(n - 1) * 2 + ori $v0, $v0, 1 # hanoi(n - 1) * 2 + 1 + lw $s0, 4($sp) # reverse s0 + lw $ra, 0($sp) # reverse ra + addi $sp, $sp, 8 + jr $ra diff --git a/1/homework/exp2/merge_sort.asm b/1/homework/exp2/merge_sort.asm index 4ff8716..936f530 100644 --- a/1/homework/exp2/merge_sort.asm +++ b/1/homework/exp2/merge_sort.asm @@ -181,7 +181,7 @@ find_left_merge_pos: lw $s0, 4($s0) j find_left_merge_pos direct_add_right_part: - sw $s1, 4($s0) + sw $s1, 4($s0) j done_do_merge_list no_direct_add_right_part: move $s2, $s1 # p_right_temp