Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ARM7 assembly shifting a number with a signed integer

I have been trying to shift my number with the formula m*2^-n by using Logical Shift Right (LSR). for m = 8 and n=-2 I should be getting 8(2)^-2 ie; 2, but I get 0 for some unknown reason.

Here is my C code

uint16_t m = 8
int16_t n = -2
printf("%u",shift(m,n));

Machine code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   shift:
      CMP R1,#0
      BLE negetive
      MOV R0,R0,LSL R1
      BX LR
   negetive:
      MOV R0,R0,ASR R1
      BX LR

>Solution :

You need to first negate R1 before you can do ASR. Shift values are unsigned, a negative shift value is interpreted as a very large positive shift, leading to a result of zero.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading