我正在编写一个程序,该程序读取4个十六进制数字,表示无符号整数,然后它会在$ t1中缩小这些数字,最后计算并显示十进制数。
我在理论上完全掌握了解决方案,但我使用mips有困难,因为这是我的第一个程序。目前,我无法存储字符串的字节。到目前为止,这是我的代码:
.data
msg1: .asciiz "Enter the hexadecimal : "
newline: .asciiz "\n"
.text
main:
#Print string msg1
li $v0 ,4 # print_string syscall code = 4
la $a0,msg1 #load the adress of msg
syscall
# Get input A from user and save
li $v0,8 # read_string syscall code = 8
syscall
move $t0,$v0 # syscall results returned in $v0
li $v0,10
syscall
我知道我会在某些时候使用lb Rdest,地址。但是,如果情况确实如此,我不会逐个读取字符串的每个数字吗?
答案 0 :(得分:1)
.data
msg1: .asciiz "Enter the hexadecimal : "
buffer: .space 10
.text
main:
#Print string msg1
li $v0 ,4 # print_string syscall code = 4
la $a0,msg1 #load the adress of msg
syscall
# Get input A from user and save
la $a0, buffer # address
li $a1, 10 # length
li $v0,8 # read_string syscall code = 8
syscall
li $t0, 0 # result
loop:
lb $t1, ($a0) # fetch char
beq $t1, $0, done # zero terminator?
addiu $t1, $t1, -10 # line feed?
beq $t1, $0, done
addiu $t1, $t1, -38 # convert digit
sltiu $t2, $t1, 10 # was it a digit?
bne $t2, $0, append # yes
# add validation and upper case here as needed
addiu $t1, $t1, -39 # convert lower case letter
append:
sll $t0, $t0, 4 # make room
or $t0, $t0, $t1 # append new digit
addiu $a0, $a0, 1 # next char
j loop
done:
move $a0, $t0 # print result
li $v0, 1
syscall
li $v0,10
syscall