I am trying to find the one-s complement of a binary number using MIPS in QtSpim.
Firstly I measure the string length and then check if a character is 0 or not making the necessary transformations.
The following code always returns "000..0". Any help in fixing this will be highly appreciated
.data
promptStr: .asciiz "Please enter a binary string:"
string: .space 120
.text
main:
la $a0, promptStr
li $v0, 4
syscall
la $a0, string
li $a1, 30
li $v0, 8
syscall
move $s0, $a0 #save the string in $s0 because $a0 may change
countLength:
la $t0, 0($s0)
loop:
lb $t1, 0($t0)
beq $t1, $zero end
addi $t0, $t0, 1
j loop
end:
la $t1, 0($s0)
sub $t3 $t0 $t1 #$t3 now contains the length of the string
addi $t3,$t3,-1
la $a0, string
li $t1,0
while:
beq $t1,$t3,exit #$t3 is the string length
la $a1,string
addu $a2,$a1,$t1 # $a1 = &str[x]. assumes x is in $t1
add $t7,$t1,$a0
bne $a2,'1',ELS #checks if i-th charcter is not 1
li $t0, '1' # puts 1 in places of 0
sb $t0, ($t7)
la $a0, string
ELS:
li $t0, '0' #puts 0 in places of 1
sb $t0, ($t7)
la $a0, string
addi $t1,1
j while
exit:
li $v0, 4 # syscall for string print
syscall
li $v0, 10 # end program
syscall