.data
prompt:.asciiz "nEnter an integer: "
add: .asciiz "n The sum in decimal is: "
bin: .asciiz "n The sum in binary is: "
sgt: .asciiz "n The second integer is greater than the first. "
fgt: .asciiz "n The first integer is greater than the second."
equal: .asciiz "n The two entered values are equal: "
.text
main:
li $v0, 4 #print string code
la $a0, prompt #argument
syscall #execute, service print stri
li $v0, 5 # read integer
syscall #v0 gets the returned value
move $s0, $v0 #set v0 -> s0
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $s1, $v0 #set v0 -> s1
add $s2, $s1, $s0 #s2 -> s0 + s1
li $v0, 4 #print string
la $a0, add #argument
syscall
move $a0, $s2
li $v0, 1 #print int
syscall
# Output "sum in binary is:".
la $a0, bin
li $v0, 4
syscall
# Output the binary number. (This is done by isolating one bit
# at a time, adding it to the ASCII code for '0', and outputting
# the character. It is important that the bits are output in
# most-to-least significant bit order.
move $t2, $a0
li $s1, 32 # Set up a loop counter
Loop:
rol $t2, $t2, 1 # Roll the bits left by one bit - wraps highest bit to lowest bit.
and $t0, $t2, 1 # Mask off low bit (logical AND with 000...0001)
add $t0, $t0, 48 # Combine it with ASCII code for '0', becomes 0 or 1
move $a0, $t0 # Output the ASCII character
li $v0, 11
syscall
subi $s1, $s1, 1 # Decrement loop counter
bne $s1, $zero, Loop # Keep looping if loop counter is not zero
slt $t0, $s0, $s1
beq $t0, $0, else
li $v0, 4
la $a0, sgt
syscall
j jump
else:
li $v0, 4
la $a0, fgt
syscall
jump:
li $v0, 10 #system call for exit
syscall
我也有相当多的麻烦得到"两个输入的数字等于工作。"我尝试使用三种情况,并有三个单独的标签:发送我的代码。但这并没有奏效。我不知道发生了什么。
这是个小错误。
查看循环前一行:
move $t2, $a0
您显然试图将总和移动到$t2
,但$a0
包含bin
的地址。
将这行改为:
move $t2, $s2
一切正常