반응형
#################################################################
# #
# 2001-12204 이준희 #
# Problem 3. MAXMIN #
# bare mode version #
# there are some no-operation instructions #
# following branch/jump instructions #
# for delayed branch/jump #
# data segement is fixed at 0x10010000 #
# tested at junebug by spim bare mode #
# ./spim -bare -file 200112204_P3.s #
# #
# [pseudocode] #
# MAXMIN(A[], n) #
# if n < 2 #
# v0 = A[0] #
# v1 = A[0] #
# return; #
# {v0, v1} = MAXMIN(A[]+1, n-1); #
# if A[0] < v0 #
# v0 = A[0] #
# if A[0] > v1 #
# v1 = A[0] #
# return; #
# #
#################################################################
.data 0x10010000
msg1: .asciiz "MAX : " # 4097
msg2: .asciiz ", MIN : " # 4097 + 7
newline: .asciiz "\n" # 4097 + 16
#input_start
Alength: .word 13
Aarray: .word 130, 202, 30, 4440, 530, 532, 33, 204, 8, 524, 8933, 92, 10
#input_end
.text
MAXMIN:
ori $t0, $zero, 2 # t0 = 2
slt $t0, $a1, $t0
bne $t0, $zero, sp_case # if n < 2, branch to sp_case
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $ra, 8($sp) # save return address
sw $a0, 4($sp) # save A[]
addi $a0, $a0, 4 # A[] = A[] + 1
addi $a1, $a1, -1 # n = n - 1
jal MAXMIN
ori $t0, $t0, 0 # nop
lw $a0, 4($sp) # restore A[]
lw $ra, 8($sp) # restore return address
addu $sp, $sp, 8
lw $t0, 0($a0) # load A[0]
slt $t1, $t0, $v0
beq $t1, $zero, next # if not A[0] < v0, branch to next
ori $t0, $t0, 0 # nop
ori $v0, $t0, 0 # if A[0] < v0, v0 = A[0] (minimum)
next: slt $t1, $v1, $t0
beq $t1, $zero, end # if not A[0] > v1, branch to end
ori $t0, $t0, 0 # nop
ori $v1, $t0, 0 # if A[0] > v1, v1 = A[0] (maximum)
bgez $zero, end # branch to end
ori $t0, $t0, 0 # nop
sp_case: lw $v0, 0($a0)
ori $v1, $v0, 0 # v0 = v1 = A[0]
end: jr $ra
main:
lui $a0, 4097
lw $a1, 20($a0) # load length n
ori $a0, $a0, 24 # address of A[]
jal MAXMIN
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $v1, 8($sp) # save maximum value
sw $v0, 4($sp) # save minimum value
ori $v0, $zero, 4
lui $a0, 4097 # address of msg1
syscall # print string
lw $a0, 8($sp) # restore maximum
ori $v0, $zero, 1
syscall # print int
lui $a0, 4097
ori $a0, $a0, 7 # address of msg2
ori $v0, $zero, 4
syscall # print string
lw $a0, 4($sp) # restore minimum
addu $sp, $sp, 8
ori $v0, $zero, 1
syscall # print int
lui $a0, 4097
ori $a0, $a0, 16 # address of newline
ori $v0, $zero, 4
syscall # print string
# #
# 2001-12204 이준희 #
# Problem 3. MAXMIN #
# bare mode version #
# there are some no-operation instructions #
# following branch/jump instructions #
# for delayed branch/jump #
# data segement is fixed at 0x10010000 #
# tested at junebug by spim bare mode #
# ./spim -bare -file 200112204_P3.s #
# #
# [pseudocode] #
# MAXMIN(A[], n) #
# if n < 2 #
# v0 = A[0] #
# v1 = A[0] #
# return; #
# {v0, v1} = MAXMIN(A[]+1, n-1); #
# if A[0] < v0 #
# v0 = A[0] #
# if A[0] > v1 #
# v1 = A[0] #
# return; #
# #
#################################################################
.data 0x10010000
msg1: .asciiz "MAX : " # 4097
msg2: .asciiz ", MIN : " # 4097 + 7
newline: .asciiz "\n" # 4097 + 16
#input_start
Alength: .word 13
Aarray: .word 130, 202, 30, 4440, 530, 532, 33, 204, 8, 524, 8933, 92, 10
#input_end
.text
MAXMIN:
ori $t0, $zero, 2 # t0 = 2
slt $t0, $a1, $t0
bne $t0, $zero, sp_case # if n < 2, branch to sp_case
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $ra, 8($sp) # save return address
sw $a0, 4($sp) # save A[]
addi $a0, $a0, 4 # A[] = A[] + 1
addi $a1, $a1, -1 # n = n - 1
jal MAXMIN
ori $t0, $t0, 0 # nop
lw $a0, 4($sp) # restore A[]
lw $ra, 8($sp) # restore return address
addu $sp, $sp, 8
lw $t0, 0($a0) # load A[0]
slt $t1, $t0, $v0
beq $t1, $zero, next # if not A[0] < v0, branch to next
ori $t0, $t0, 0 # nop
ori $v0, $t0, 0 # if A[0] < v0, v0 = A[0] (minimum)
next: slt $t1, $v1, $t0
beq $t1, $zero, end # if not A[0] > v1, branch to end
ori $t0, $t0, 0 # nop
ori $v1, $t0, 0 # if A[0] > v1, v1 = A[0] (maximum)
bgez $zero, end # branch to end
ori $t0, $t0, 0 # nop
sp_case: lw $v0, 0($a0)
ori $v1, $v0, 0 # v0 = v1 = A[0]
end: jr $ra
main:
lui $a0, 4097
lw $a1, 20($a0) # load length n
ori $a0, $a0, 24 # address of A[]
jal MAXMIN
ori $t0, $t0, 0 # nop
subu $sp, $sp, 8
sw $v1, 8($sp) # save maximum value
sw $v0, 4($sp) # save minimum value
ori $v0, $zero, 4
lui $a0, 4097 # address of msg1
syscall # print string
lw $a0, 8($sp) # restore maximum
ori $v0, $zero, 1
syscall # print int
lui $a0, 4097
ori $a0, $a0, 7 # address of msg2
ori $v0, $zero, 4
syscall # print string
lw $a0, 4($sp) # restore minimum
addu $sp, $sp, 8
ori $v0, $zero, 1
syscall # print int
lui $a0, 4097
ori $a0, $a0, 16 # address of newline
ori $v0, $zero, 4
syscall # print string
반응형