所以我确定我在代码中的某个地方出了问题但是我试图将用户输入程序的整数转换为Hex。我的第一个问题是,我似乎无法让程序打印输入的整数。相反,它似乎打印整数的地址。 这是代码:
.data # Data declaration
# A+B -(C-D x E)
string1: .asciiz "Please enter an integer from range 0 to 32768: "
string2: .asciiz "Next integer: "
string3: .asciiz "Invalid input. Start over: "
string4: .asciiz "The hexadecimal value of the decimal integer "
string5: .asciiz "is: "
newline: .asciiz "\n"
.align 2
userinput: .space 100
var6: .asciiz "The result of A+B -(C-D x E) is: "
.text
main:
la $a0, string1 #load string one and print
li $v0, 4
syscall
li $s0, 1
la $a1, userinput
li $t1, 5 #set temporary variable to 5
li $t0, 0 #start of counter
input:
beq $t0, $t1, exit
li $v0, 5 # read integer
syscall
blt $v0, $zero, invalid # if input is less than zero
bgt $v0, 32768, invalid # if input is greater than 32768
beq $t0, 0, case1
beq $t0, 1, case2
beq $t0, 2, case3
beq $t0, 3, case4
beq $t0, 4, case5
case1:
sw $v0, 0($a1)
addi $t0, 1
la $a0, string2
li $v0, 4
syscall
j input
case2:
sw $v0, 4($a1)
addi $t0, 1
la $a0, string2
li $v0, 4
syscall
j input
case3:
sw $v0, 8($a1)
addi $t0, 1
la $a0, string2
li $v0, 4
syscall
j input
case4:
sw $v0, 12($a1)
addi $t0, 1
la $a0, string2
li $v0, 4
syscall
j input
case5:
sw $v0, 16($a1)
addi $t0, 1
la $a0, string2
li $v0, 4
syscall
j input
invalid:
la $a0, string3 #load string one and print
li $v0, 4
syscall
j input
exit:
la $t3, userinput # stores base address of user input array into $t3
li $t1, 1
li $t0, 0
la $a0, string4 # load string4
li $v0, 4 # print
syscall
move $a0, $t3 # move first number into a0
li $v0, 1 # print
syscall
la $a0, string5
li $v0, 4
syscall
jal convert
li $t1, 1
jal hexconvert
addi $s0, $s0, 1
slti $t7, $s0, 6
bne $t2, $zero, exit
j close
convert:
andi $t4, $t3, 15
sll $s4, $s4, 8
slti $t5, $t4, 10
bne $t5, $zero, else
addi $t4, $t4, 65
jal next
else:
addi $t4, $t4, 48
next:
add $s4, $s4, $t3
srl $t3, $t3, 4
addi $t1, $t1, 1
slti $t5, $t1, 5
bne $t5, $zero, convert
jr $ra
hexconvert:
andi $t4, $s4, 255
move $a0, $t4
li $v0, 11
syscall
srl $s4, $s4, 8
addi $s3, $s3, 1
slti $t1, $s3, 5
bne $t2, $zero, hexconvert
la $a0, newline
li $v0, 4
syscall
jr $ra
close:
li $v0, 10 # exit instruction
syscall
我希望有人能指出我正确的方向。我对MIPS仍然很陌生。 谢谢!
答案 0 :(得分:1)
我似乎无法让程序打印输入的整数。相反,它似乎打印整数的地址。
打印地址似乎正是您在这里所做的:
la $t3, userinput # stores base address of user input array into $t3
...
move $a0, $t3 # move first number into a0
li $v0, 1 # print
syscall
move $a0, $t3
只是将$t3
的值(即userinput
的地址)复制到$a0
。如果你真正想要的是从$t3
所指向的记忆中读取你应该写的lw $a0,($t3)