我有一个MIPS汇编程序,该程序从用户输入中获取负整数,找到该数字的绝对值,然后将其转换为二进制字符串。我希望能够通过翻转计算的二进制字符串来找到数字的2的补码(将所有0更改为1,将1更改为0),然后将1添加到最低有效位(同时携带1到更高的位)位(如果需要)。我不确定如何执行此操作。任何帮助将不胜感激。我在下面发布了我当前的程序以供参考
.data
#declare strings
readMsg: asciiz "Enter a negative number:"
printBinary: .asciiz "Binary Number :"
space: .asciiz " "
outOfRange: .asciiz "number is not negative!"
.text
.globl main
main:
#prompt the user to enter a negative number
li $v0, 4
la $a0, readMsg
syscall
#read integer from the user
li $v0, 5
syscall
#if user input is positive, exit. if negative, find absolute value and continue
bgt $v0, $zero, exit
sub $v0, $zero, $v0
blt $v0, $zero, continue
continue:
#move to input value to register $s0
move $s0, $v0
#print the string binary number
li $v0, 4
la $a0, printBinary
syscall
#jump and link to the calculateBinary
jal calculateBinary
#end the program
li $v0, 10
syscall
#calculate the binary string of integer
calculateBinary:
#move input $s0 into $t0
add $t0, $zero, $s0
#move 0 into $t1
add $t1, $zero, $zero
#load 1 as a mask
addi $t3, $zero, 1
#shift left the value of $t3 by 7 positions
sll $t3, $t3, 7
addi $t4, $zero, 8 #loop counter
#perform AND with input and mask
loop:
and $t1, $t0, $t3
#branch to print if 0
beq $t1, $zero, printBinValue
#move 0 into $t1
add $t1, $zero, $zero
#put a 1 in $t1
addi $t1, $zero, 1
j printBinValue
printBinValue:
#print value
li $v0, 1
move $a0, $t1
syscall
#putspace
beq $t4, 5, printSpace
j loop2
printSpace:
li $v0, 4
la $a0, space
syscall
loop2:
#shift right by 1 position
srl $t3, $t3, 1
addi $t4, $t4, -1
bne $t4, $zero, loop
#return to main
jr $ra
exit:
li $v0, 4
la $a0, outOfRange
syscall