Having x= 5*x/16, how does this equal:
MOV r0, r0, LSR #4
ADD r1, r0, r0, LSL #2
Please explain thoroughly
What I understand of right now is the first line is basically x/16
. And then in the second line I get confused on what exactly is happening. I believe the r0, LSL #2
makes it 4x/16
but then I dont understand what the ADD r1, r0
completely does. I suppose it adds an x
to make it 5x/16
but I am not sure how.
CodePudding user response:
elementary algebra. 2^4 = 16. so shifting right for is an UNSIGNED divide by 16. If I have the number 3000 decimal and want to divide by 100, what do I do? shift it right two yes? 100 = 10^2 so shifting 3000 >> 2 is 30 which is 3000/100.
5 = 4 1 = 2^2 1. x*(5) = x (4 1) = (x4) (x1)
just like x >> 4 is a divide by 16 x<<2 is a times 4.
(x4) (x1) = (x<<2) x.
elementary algebra
The times five is easier to see with long multiplication (also from grade school, base two is much easier than base 10).
take the four bits abcd times five 0101.
abcd
* 0101
===========
in grade school you would take for example
abcd
* jk
=======
mmmm this is k times all of abcd
nnnn this is j times all of abcd, because j is in the tens position 10^1
it is shifted left one
With base two you are only multiplying against zero or one.
abcd
* 0101
===========
abcd
0000
abcd
==========
so if you wanted to multiply by 6 decimal then which is 0110 binary so you x*6 = (x<<2) (x<<1)
times 10, 1010 binary 10x = (x<<3) (x<<1) or....((x<<2) x)<<1) (times 5 times 2).