如何使用 or 和 sll 打包(压缩)字符串的字节?

How to pack(condensate) the bytes of a string using or and sll?

我正在编写一个程序,它读取 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, address。但是,如果是这样的话,我不需要一个一个地读取字符串的每个数字吗?

.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, [=10=], done        # zero terminator?
    addiu $t1, $t1, -10      # line feed?
    beq $t1, [=10=], done
    addiu $t1, $t1, -38      # convert digit
    sltiu $t2, $t1, 10       # was it a digit?
    bne $t2, [=10=], 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