ARM Assembler BCD to integer w/o multiply instruction
For my class I have to write an assembler function, which gets a BCD code from an specific index of a vector as a parameter and returns the equivalent integer value. I may only use a basic ARM instruction set like:
AND, EOR, SUB, RSB, ADD, CMP, CMN, TST, TEQ, ORR, MOV, BIC, MVN, LSL, LSR, ASR, ROR
and may NOT use the multiply instruction.
I know how the conversion normally works, but I don't get how it could work without multiplying the single nibbles. Does it have something to do with bit-shifting?
Every hint would be great.
And excuse me if some information are missing, I'm really new to this field. If any is missing, which could be important - please let me know and I will rich it by.
Well, the trivial way that should have occurred to you is that multiplication is repeated addition, and you can use ADD
.
A somewhat more sophisticated method is if you realize that 10*x = 8*x + x + x
and multiplication by 8
is a shift by 3
so you can do that using a shift and two additions.
10*x = 5*2*x = (4+1)*2*x = (4x+x)*2 = ((x<<2)+x)<<1;
EDIT
Yes you have to do single BCD digits.
10 = 1010b
100 = 1100100b
1000 = 1111101000b
and so on.
so as above 10 can be what Jester said (just look at the bits) (x<<3)+(x<<1). or you could look at it as 5*2 and 5 is 101b or (x<<2)+x and multiply that by 2 ((x<<2)+x)<<1
100 is (x<<6)+(x<<5)+(x<<2) or take a 2 from each ((x<<4)+(x<<3)+x)<<2
or look at this with elementary school math where k,l,m,n are arbitrary bits.
klmn
* 1010
=======
0000
klmn
0000
+klmn
=========
(klmn<<1)+(klmn<<3)
the times 100 case you can do as 10*(10*x) and maybe that is less shifting and adding, maybe not. or factor it out 5*5*2*2*x can do the 5x and then 5 times that then just shift that result. Basically all of this we learned in elementary school, it is just way simpler with base 2 since you only need to know the zeros times table and the ones times table.
链接地址: http://www.djcxy.com/p/72560.html