user659745
user659745

Reputation: 25

Adding bits x32 mips assembly

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

li $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 

Upvotes: 0

Views: 1467

Answers (1)

Gareth McCaughan
Gareth McCaughan

Reputation: 19981

Here are a few pointers.

  1. 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).

  2. 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.

  3. You're not using the carry flag in the addition. Neither are you doing anything to check for carries when you add bits.

  4. 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:

  1. Are you missing some instructions at the start of your code? I mean, can you really say just $a2, resultSpace or do you need la $a2, resultSpace or something of the kind?

  2. 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.

Upvotes: 1

Related Questions