I am new at using MIPS and I try to calculate Hamming distance. However even the register $t3 has the correct XOR result, when I print it in decimal I get a wrong number.
word1: .word 0xffeff0ff
word2: .word 0x00000001
when I XOR these two words, the result is:
ffeff0fe
my question is how do I calculate it in decimal ? Because I tried to print it in screen and I got -1052418 instead of the correct 26
Here is my complete code
.text
.globl __start
__start:
la $a1,word1
lw $t1,0($a1)
la $a1,word2
lw $t2,0($a1)
xor $t3,$t1,$t2
la $a0,answer
li $v0,4
syscall
move $a0,$t3
li $v0,1
syscall
la $a0,endl
li $v0,4
syscall
EXIT: li $v0,10
syscall
.data
word1: .word 0xffeff0ff
word2: .word 0x00000001
answer: .asciiz "Hamming distance is : "
endl: .asciiz "\n"
You are printing the xor of the two values instead of the number of bits which have different value. what you need to do to compute the hamming distance is to count that number, for example with this code:
move $a0, $zero # $a0 will hold the result
loop:
bgez $t3, skip # test the most significative bit of $t3
sll $t3, $t3, 1 # (*) [NOTE below] shift left $t3 one bit
addiu $a0, $a0, 1 # If bit was set, increment result
skip:
bnez $t3, loop # loop while not done
nop # this nop is due to delayed branching
This snippet assumes that your MIPS processor has delayed branching. If it is not the case, you have to move instruction marked with (*) after label skip. The nop can be removed if you don't have delayed branching or if the next instruction does not have side effects.
This code should go instead of the line where you put move $a0,$t3
.text
.globl __start
__start:
lbu $t0 , B
lbu $t1 , B1
xor $s0 , $t0 , $t1
addi $s1 , $s1 , 7 # pointer for swift
add $s2 , $zero , $zero # pointer for loop
loop:
srl $t7 , $s0 , $s1 # swift
sll $t7 , $t7 , 31
slt $t7 , $t7 , -1
la $a0 , 0($t7)
li $v0 , 1
syscall
la $a0 , str
li $v0 , 4
syscall
addi $s2 , $s2 , 1 #loop
addi $s1 , $s1 , -1 #swift
beq $s2 , 8 , exit
j loop
exit: li $v0 , 10 syscall
.data
B: .byte 3
B1: .byte 1
str: .asciiz "\n"
User contributions licensed under CC BY-SA 3.0