Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 无符号16位十六进制字符串到Dec值_Assembly_Mips_Type Conversion - Fatal编程技术网

Assembly 无符号16位十六进制字符串到Dec值

Assembly 无符号16位十六进制字符串到Dec值,assembly,mips,type-conversion,Assembly,Mips,Type Conversion,我正在尝试对长度不超过4个字符的十六进制字符的用户输入字符串执行无符号16位十六进制到十进制的转换。我有来自寄存器内存位置的字节,str缓冲区地址存储在另一个寄存器中。因此,我试图相应地处理每个字节。我注意到我可以将str的第一个字符向左移动4位,这样我就可以得到一个8位的字符串。然后,我可以获取字符串中的下一个字符,并对8位字符串执行“或”,然后将下一个字符添加到整个位字符串的末尾。我正试图找出如何实现这种关系。在我让用户输入4个字符后,我的代码挂起了。我还没有对此进行调试,但我觉得我的逻辑和

我正在尝试对长度不超过4个字符的十六进制字符的用户输入字符串执行无符号16位十六进制到十进制的转换。我有来自寄存器内存位置的字节,str缓冲区地址存储在另一个寄存器中。因此,我试图相应地处理每个字节。我注意到我可以将str的第一个字符向左移动4位,这样我就可以得到一个8位的字符串。然后,我可以获取字符串中的下一个字符,并对8位字符串执行“或”,然后将下一个字符添加到整个位字符串的末尾。我正试图找出如何实现这种关系。在我让用户输入4个字符后,我的代码挂起了。我还没有对此进行调试,但我觉得我的逻辑和注释流应该可以解释我在做什么。这里有什么我做错的吗?有没有比这更短的路

我在这些论坛上到处看看,我找不到任何关于尝试以这种方式转换的信息

Here I get Input
getStrInput:
li $v0, 8                   # $a0, holds input
li $a1, 5
syscall
li $v0, 0                   # reset reg $v0

la $t1, ($a0)               # save the address of the string input

# loading the byte
loadByteToStr:
li $t4, 0
beq $t3, 5, showConv        # If user has input 4 characters, branch to showConv 
addi $sp, $sp, -4           # push the stack
sw $ra 0($sp)               # save return address for safety
sw $t3, 4($sp)              # store the loop counter on stack
lb $t6, ($t1)               # Load byte from the string into temp register for function call
jal isDigit                 # check to see if it is a digit if not treat character accordingly
beqz $t3, adjustBits        # unsigned 16bit conversion from Hexa - Dec

endAdjustBits:
addi $t3, $t3, 1            # increment loop counter
jal u16Convert              # adjusted bitstring stored in $s5
addi $t1, $t1, 1            # increment address
lw $ra 0($sp)               # load original return address
addi $sp, $sp, 4            # pop the stack

j loadByteToStr             # loop

isDigit:
move $a0, $t6               # move the byte to $a0 for calculations
subi $a0, $a0, 48           # subtract ASCII 0 to see if it falls within digit range
blt $a0, 0, isChar          # check if byte is a character if not a digit
bgt $a0, 9, isChar
jr $ra                      # return to loadByteToStr


isChar:
addi $a0, $a0, 48           # reset input
subi $a0, $a0, 55           # get the hexa char value
jr $ra                      # return to loadByteToStr

# this block of code takes the first char, appends the second to it, takes the next and appends the last to it.
adjustBits: 
sll $a0, $a0, 4             # shift $a0 12 bits to make room for characters in str
add $t0, $a0, $zero         # save $a0 to $t0
beqz $t3, loadByteToStr     # if loop count is 0, load the next char before calling next function

# Get the next bytes in str
u16Convert:
or $t0, $t0, $a0            # insert next bitstring to total bitstring
beq $t3, 4, contU16         # if it is the last byte, convert the string to decimal
jr $ra

# continue conversion after creating the hex bitstring
contU16:
beq $t4, 4, showConv

addi $t0, $t0, 4            # incrememnt next 4 bits
beq $t4, 0, elem1           # treat next 4 bits in str accordingly
beq $t4, 1, elem2
beq $t4, 2, elem3
beq $t4, 3, elem4
addi $t4, $t4, 1            # increment loop counter
j contU16