在理解如何将二进制字符串(例如“ 0x0123beef”)转换为其十六进制形式(在mips中仅为0x0123beef)时,我有些麻烦。
我了解如何解决该问题,但是我对如何实际解决问题感到非常困惑。实际上,我设法将一些代码放在一起
.data
Hex: .word 0x0123b5ef
ans: .asciiz "\nHexadecimal equivalent: "
result: .space 10
.text
main:
la $t2, Hex
la $a0, ans
li $v0, 4
syscall
li $t0, 8 # counter
la $t3, result
Loop:
beqz $t0, Exit # branch to exit if counter is equal to zero
rol $t2, $t2, 4 # rotate 4 bits to the left
and $t4, $t2, 0xf # mask with 1111
ble $t4, 9, Sum # if less than or equal to nine, branch to sum
addi $t4, $t4, 0x37 # if greater than nine, add 55
b End
Sum:
addi $t4, $t4, 0x30 # add 48 to result
End:
sb $t4, 0($t3) # store hex digit into result
addi $t3, $t3, 1 # increment address counter
sub $t0, $t0, 1 # decrement loop counter
j Loop
Exit:
la $a0, result
li $v0, 4
syscall
la $v0, 10
syscall
我正在努力使它实际上首先显示0123beef。每次运行它都会产生10010000。解决此问题后,我想知道如何将“ 0x”部分放在开头。
谢谢!
答案 0 :(得分:-1)
这是我基本上是通过一些数学计算并不断打印字符的方式
.data
message: .asciiz "Enter your binary number: "
numbin: .space 255 #255 bytes nom
quatri: .space 255 #255 dyte
hexai: .space 255
.text
main:
li $v0, 4
la $a0, message
syscall
la $a0, numbin
li $a1, 255
li $v0, 8
syscall
li $t1,0
la $t0,numbin
li $t3,1
li $t4,4
li $t7, 48
li $t5, 0
li $t6, 2
li $s3, 3
li $s4, 0
li $s5, 0
li $s6, 0
li $s7, 0
li $t2, 0
loop:
lb $a0,0($t0)
beqz $a0,done
addi $t0,$t0,1
addi $t1,$t1,1
j loop
done:
sub $t1, $t1, $t3 #per tenir el numero de caracters entrats
add $t2, $t1, $zero #fem aquesta suma per desplazar el valor al registre t2
#fem una divisio per trovar quants zeros tenim a devant al primer paquet
div $t2, $t4
mfhi $s1
sub $t5, $t4, $s1
li $t1,0
beq $t5, 1, unze
beq $t5, 2, dosze
beq $t5, 3, treze
beq $t5, 4, reto
reto:
li $t5, 0
seg:
beq $t5, 4, reto
beq $t2, $t1, fi
add $s0, $zero, $t1
la $a1,numbin
addu $a1,$a1,$s0 # $a1 = &str[x]. assumes x is in $s0
lbu $a0,($a1) # read the character
sub $a0, $a0, $t7 #test temporal
addi $t1, $t1, 1
addi $t5, $t5, 1
beq $t5, 1, muli1
beq $t5, 2, muli2
beq $t5, 3, muli3
beq $t5, 4, muli4
b seg
unze:
li $s4, 0
li $t5, 2
add $s0, $zero, $t1
la $a1,numbin
addu $a1,$a1,$s0 # $a1 = &str[x]. assumes x is in $s0
lbu $a0,($a1) # read the character
sub $a0, $a0, $t7 #test temporal
addi $t1, $t1, 1
b muli2
dosze:
li $s4, 0
li $s5, 0
li $t5, 3
add $s0, $zero, $t1
la $a1,numbin
addu $a1,$a1,$s0 # $a1 = &str[x]. assumes x is in $s0
lbu $a0,($a1) # read the character
sub $a0, $a0, $t7 #test temporal
addi $t1, $t1, 1
b muli3
treze:
li $t5, 4
li $s4, 0
li $s5, 0
li $s6, 0
add $s0, $zero, $t1
la $a1,numbin
addu $a1,$a1,$s0 # $a1 = &str[x]. assumes x is in $s0
lbu $a0,($a1) # read the character
sub $a0, $a0, $t7 #test temporal
addi $t1, $t1, 1
b muli4
muli1:
li $s4, 0
mul $s4, $a0, 8
b seg
muli2:
li $s5, 0
mul $s5, $a0, 4
b seg
muli3:
li $s6, 0
mul $s6, $a0, 2
b seg
muli4:
li $s7, 0
mul $s7, $a0, 1
trasforma:
li $t8, 0
add $t8, $s4, $s5
add $t8, $t8, $s6
add $t8, $t8, $s7
bgt $t8, 9, last
add $a0, $t8, $t7
b escriu
last:
beq $t8, 10, A
beq $t8, 11, B
beq $t8, 12, C
beq $t8, 13, D
beq $t8, 14, E
beq $t8, 15, F
A:
li $a0, 'A'
b escriu
B:
li $a0, 'B'
b escriu
C:
li $a0, 'C'
b escriu
D:
li $a0, 'D'
b escriu
E:
li $a0, 'E'
b escriu
F:
li $a0, 'F'
b escriu
escriu:
li $v0, 11
syscall
b seg
fi:
li $v0,10
syscall