Shop OBEX P1 Docs P2 Docs Learn Events
sign extension in assembly — Parallax Forums

sign extension in assembly

pgbpsupgbpsu Posts: 460
edited 2008-02-14 14:22 in Propeller 1
How do I extend the sign of a 24-bit 2's complement number to 32-bits in assembly? I know that assembly has commands for doing signed and unsigned addition, but that's not quite the whole problem. I need to get my high bit dragged all the way out to 32. I'm adding 8 signed 24-bit numbers then dividing (shifting by 3) to get the average.

It took me a long time to figure out why the output was screwed up when the signal crossed zero. I've traced the problem to the sign bit not being the highest bit in the variable's register but don't know how to solve it.

Do any of the seasoned programmers here have suggestions. And of course, since I'm doing all this in assembly it needs to be fast.

Thanks,
Peter

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-02-14 03:48
    To sign-extend a 24-bit integer, SHL 8 bits, then SAR 8 bits. (I can't take credit for this one. It's been posted before, but I can't find it.)

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-14 08:42
    What Phil describes is the STANDARD way to do it... looking for credits will take you more than 50 years to the past smile.gif
    There are other possibilities... Look how Beau did it (faster if you had no barrel shifter).: http://forums.parallax.com/showthread.php?p=707546
  • pgbpsupgbpsu Posts: 460
    edited 2008-02-14 14:22
    Gentleman thank you both. That's exactly what I wanted. I didn't understand what SAR (shift value arithmetically right) did. Now I do.

    Finally a question NOT answered by deSilva's tutorial! [noparse];)[/noparse]

    Thanks,
    pb
Sign In or Register to comment.