I am trying to add 2 strings of bits in a function and i cannot get it to work... any suggestions? here is my code below:
$a0, "11111111111100000001111111111110" #= -4064
$a1, "00000000001000111000000000000010" # = 9088
$a2, resultSpace
$a3, representation #one's or two's complement
l开发者_开发问答i $t4, 0 #carry flag
lb $t0, 32($a0)
lb $t1, 32($a1)
move $t5, $a0
loop:
lb $t0, 32($a0)
lb $t1, 32($a1)
add $t3, $t0, $t1
sb $t3, 32($a2)
sub $a2, $a2, 1
sub $a0, $a0, 1
sub $a1, $a1, 1
addi $t4, $t4, 1 # increments
bne $t4, 32, loop
Here are a few pointers.
You're confusing bits with ASCII characters '0' and '1'. If you add '0' and '1' you get not '1' but 'a' (because '0' is character 48, '1' is character 49, and 'a' is character 97).
You're starting by looking at offset 32 in each string. But the strings are 32 characters long, so the valid offsets into them are 0..31, so the very first thing you do is to read off the end of the input data.
You're not using the carry flag in the addition. Neither are you doing anything to check for carries when you add bits.
You are, however, using the register that you claim is the carry flag as a loop counter.
I'm very far from being a MIPS assembler expert so these two may be wrong:
Are you missing some instructions at the start of your code? I mean, can you really say just
$a2, resultSpace
or do you needla $a2, resultSpace
or something of the kind?Is there a bne-immediate pseudoinstruction? Because I'm pretty sure there isn't an actual bne-immediate instruction; the
bne
instruction in the MIPS instruction set expects to be given two registers rather than a register and an immediate value.
精彩评论