[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [f-cpu] an instruciton



On Sun, Dec 07, 2003 at 01:28:24AM +0200, Martin Vahi wrote:
> 
>   OK, may be I'm spamming, haven't read the F-CPU doc's yet,

Me too :-(
I already read YG's reply, but I lurk here for several years, and
that's the first time I had something nice to say :-)

> but does F-CPU have an instruction, that just COUNTS THE
> BITS IN A CERTAIN REGISTER? For instance, let's say register
> X has the following contents: 0110011010 and
> the sugested instruction just counts the "1"-bits in that
> register. The result in this example is 5.
> 
[snip]
> And in order to know, if I have a match, I have
> to count, how many "1"-bits do I have. Well, generally
> it's no problem, it can be done by shifting, but as

That's not the only way.
The following loop counts the number of '1's in r0:
n <-0
l: r1 <- neg r0 (the negation of r0, in two's complement)
 r1 <- not r1
 r0 <- r0 and r1
 n <- n+1
 cmp r0 0
 jnz l
The number of loops here is the number of '1's. Not as good as a good
instruction, but at least for your needs better than shifting.
An anecdote: This code fragment was given to me as a question in a
test in assembler, around 10 years ago, during my undergraduate
studies. The question was to tell what the fragment does. I was so
impressed by it that I still remember it today, one of the very few
things I still remember from my studies.
-- 
Didi

*************************************************************
To unsubscribe, send an e-mail to majordomo@seul.org with
unsubscribe f-cpu       in the body. http://f-cpu.seul.org/