📄 05945125d.s
字号:
#
# Procedure:
# 1. Print message: "Input base: "
# 2. Read the input-base from the user
# 3. Print message: "Input number in base X:"
# (X is the input in procedure 3)
# 4. Read the number from the user as a string
# 5. Print message: "Convert to base: "
# 6. Read the output base from the user
# 7. Print message: "Base Y equivalent"
# (Y is the input in procedure 6)
# 8. Decide the length of the number
# 9. Convert the string to decimal integer
# 10. Convert the decimal to coresponding number
# with the output-base to a string
# 11. Print the equivalent number as a string
# 12. Print message: "Want a base 2 32-bit floating
# point equivalent? "
# 13. Read the user's input and decide what to do
# (Y/y - yes, else - no, terminate the program)
# 14. Store the sign bit
# 15. Store the exponent
# 16. Store the significand
# 17. Print message: "Base 2 floating point
# equivalent: "
# 18. Print out the 32-bit floating point equivalent
# as a string
# 19. Terminate the program
#
# Declare variable
.data
str0: .asciiz "\nBye!"
str1: .asciiz "\nInput base: "
str2: .asciiz "Input number in base "
str3: .asciiz ": "
str4: .asciiz "Convert to base: "
str5: .asciiz "Base "
str6: .asciiz " equivalent: "
str7: .asciiz "\nWant a base 2 32-bit floating point equivalent? "
str8: .asciiz "\nBase 2 floating point equivalent: "
str9: .asciiz "\n"
input: .byte 0,0,0,0,0,0,0,0,0
output: .space 33
.byte 0
chi: .byte 0,0
.globl main
# Main
.text
main:
# Step 1: Ask for input
la $a0, str1 # print str1 "Input base: "
li $v0, 4
syscall
li $v0, 5 # read the in-base from user
syscall
move $s0, $v0 # $s0=$v0, which holds the in-base value
la $a0, str2 # print str2 "\nInput number in base "
li $v0, 4
syscall
move $a0, $s0 # print the in-base $s0
li $v0, 1
syscall
la $a0, str3 # print str3 ": "
li $v0, 4
syscall
la $a0, input # read the number as a string
li $a1, 9 # save it to the address "input"
li $v0, 8 # save the length of the string in to a1
syscall
la $a0, str4 # print str4 "\nConvert to base: "
li $v0, 4
syscall
li $v0, 5 # read the to-base from user
syscall
move $s1, $v0 # $s1=$v0, which holds the to-base value
la $a0, str5
li $v0, 4 # print str5 "\nBase "
syscall
move $a0, $s1 # print the to-base $s1
li $v0, 1
syscall
la $a0, str6 # print str6 " equivalent: "
li $v0, 4
syscall
# Step 2: Decide the length of the number(str)
li $t0, 0 # initialize the counter $t0=0
la $t1, input # load the address "input" to $t1
step2:
beq $t0, 8, s2_d # if ($t0==8), jump to "s2_d"
lb $t2, ($t1) # load byte
beq $t2, 10, s2_d # if there is a break (ascii=10),
# jump to "s2_d"
addi $t0, $t0, 1 # counter: $t0++
addi $t1, $t1, 1 # address: $t1++
j step2 # jump back to "step2"
s2_d:
# Step 3: Convert the string to decimal integer
move $t3, $t0 # $t3=$t0, $t0 is the loop counter
la $t1, input # load the address "input"
la $t7, output # load the address "output"
li $s2, 0 # initialize corresponding int. $s2=0
step3:
lb $t2, ($t1) # load the byte stored in $t1
blt $t2, 65, s3_b # if ($t2<65), jump to "s3_b"
addi $t2, $t2, -7 # may be A-F, should decrease by 7
blt $t2, 65, s3_b # if ($t2<65), jump to "s3_b"
addi $t2, $t2, -32 # a-f, should decrease by 32
s3_b:
sub $t2, $t2, 48 # convert from ascii to int
beq $t0, $t3, s3_d # at the first loop, escape multiply
mul $s2, $s2, $s0 # $s2*=Ss0
s3_d:
add $s2, $s2, $t2 # $s2+=$t2, $s2 hold the int. value
add $t1, $t1, 1 # increase address by 1: $t1++
add $t0, $t0, -1 # decrease loop counter by 1: $t0--
bnez $t0, step3 # if ($t0!=0), jump to "step3"
# Step 4: Get the length of the result
li $t0, 0 # initialize the loop counter $t0=0
move $t1, $s2 # $t1=$s2
step4:
div $t1, $s1
mflo $t1 # quotient is stored in $t1
addi $t0, $t0, 1 # loop counter: $t0++
beqz $t1, s4_d # if ($t1==0), jump to "s4_d"
j step4 # jump to "step4"
s4_d:
move $s3, $t0 # $s3=$t0
# Step5: Convert the result to string
move $t0, $s3 # initialize the loop counter $t0=$s3
move $t1, $s2 # $t1=$s2 (to-base)
la $t7, output # load the first address of output to $t7
step5:
div $t1, $s1
mflo $t1 # quotient $t1
mfhi $t2 # reminder $t2
add $t3, $t7, $t0 # $t3=$t7+$t0
sub $t3, $t3, 1 # $t3--
blt $t2, 10, s5_b # if $t2<10, jump to s5_b
addi $t2, $t2, 7 # $t2+=7
s5_b:
add $t2, $t2, 0x30 # transfer to ascii
sb $t2, ($t3) # store the $t2 to the address $t3
addi $t0, $t0, -1 # $t0--
beqz $t1, s5_d # if $t1==0, jump to s5_d
j step5 # jump to "step5"
s5_d:
beq $s3, 32, s5_p
la $t7, output # add a padding "/0" to terminate the str
add $t7, $t7, $s3
sb $zero, ($t7)
s5_p:
la $a0, output # print the output
li $v0, 4
syscall
# Step 6: Decide whether to convert to base-2
# 32-bit floating point equivalent
la $a0, str7 # print str7 "Want a base 2 32-bit
li $v0, 4 # floating point equivalent? "
syscall
la $a0, chi # read the input
li $a1, 2 # save the string to the address "input"
li $v0, 8 # save the length of the string in to a1
syscall
la $t0, chi # read the input
lb $t1, ($t0)
beq $t1, 89, step7 # is Y
beq $t1, 121, step7 # is y
j end
# Step 7: Find out the length of exponent
step7:
move $t0, $s2 # $t0=$s2, which holds the integer value
li $t1, 0 # initialize the loop counter $t1=0
s7_b:
rol $t0, $t0, 1 # rotate one bit to left
and $t2, $t0, 1 # remain rightmost bit
addi $t1, $t1, 1 # loop counter $t1++
beqz $t2, s7_b # if ($t2!=0), jump to s7_b
li $t2, 32 # $t2=32
sub $t2, $t2, $t1 # $t2=$t2-$t1
move $s4, $t2 # $s4 hold the exponent value
# Step 8: Store the sign bit
la $t7, output
move $t3, $zero
add $t3, $t3, 0x30
sb $t3 ($t7)
# Step 9: Store the exponent
li $t3, 127 # load bias $t3=127
add $t3, $t3, $s4
li $t4, 8 # loop counter: $t4=8
step9:
beq $t4, 8, s9_b # first loop, no shift
srl $t3, $t3, 1 # shift to right by one bit
s9_b:
and $t5, $t3, 1
add $t5, $t5, 0x30
move $t6, $t7
add $t6, $t6, $t4
sb $t5, ($t6)
addi $t4, $t4, -1
bnez $t4, step9
# Step 10: Store the significand
blt $t2, 23, stp10_2
li $t3, 23
la $t4, output
# Condition 10.1: the remaining bits are greater or equal to 23,
# do stp10_1
stp10_1:
rol $t0, $t0, 1
and $t5, $t0, 1
add $t5, 0x30
sb $t5, 9($t4)
addi $t3, $t3, -1
addi $t4, $t4, 1
bnez $t3, stp10_1
j step11
# Condition 10.2: the remaining bits are less than 23, do stp10_2
stp10_2:
li $t3, 23
sub $t3, $t3, $t2
la $t4, output # if the remaining bit is 0, jump
beq $t2, $zero, s10_b2 # to s10_b2 directly
s10_b1: # add value
rol $t0, $t0, 1
and $t5, $t0, 1
add $t5, 0x30
sb $t5, 9($t4)
addi $t2, $t2, -1
addi $t4, $t4, 1
bnez $t2, s10_b1
s10_b2: # add zero
li $t5, 48
sb $t5, 9($t4)
addi $t3, $t3, -1
addi $t4, $t4, 1
bnez $t3, s10_b2
# Step 11: Print the base-2 32-bit floating point equivalent
step11:
la $a0, str8
li $v0, 4
syscall
la $a0, output
li $v0, 4
syscall
end:
la $a0, str0
li $v0, 4
syscall
li $v0, 10 # end the program
syscall
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -