Why is the code adding 7 if the number is not >= 0
Posted
by
Hugo Dozois
on Stack Overflow
See other posts from Stack Overflow
or by Hugo Dozois
Published on 2012-09-02T15:26:22Z
Indexed on
2012/09/02
15:38 UTC
Read the original article
Hit count: 203
I've got this program in MIPS assembly which comes from a C code that does the simple average of the eigth arguments of the function.
average8:
addu $4,$4,$5
addu $4,$4,$6
addu $4,$4,$7
lw $2,16($sp)
#nop
addu $4,$4,$2
lw $2,20($sp)
#nop
addu $4,$4,$2
lw $2,24($sp)
#nop
addu $4,$4,$2
lw $2,28($sp)
#nop
addu $2,$4,$2
bgez $2,$L2
addu $2,$2,7
$L2:
sra $2,$2,3
j $31
When the number is positve, we directly divided by 8 (shift by 3 bits), but when the number is negative, we first addu 7
then do the division.
My question is why do we add 7
to $2
when $2 is not >= 0
?
EDIT : Here is the C code :
int average8(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
{
return (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8) / 8;
}
note : the possible loss in the division since we are using ints instead of floats or doubles is not important in this case.
© Stack Overflow or respective owner