Shop OBEX P1 Docs P2 Docs Learn Events
2's complement to signed int — Parallax Forums

2's complement to signed int

pgbpsupgbpsu Posts: 460
edited 2008-11-15 23:47 in Propeller 1
I've got a 24-bit 2's complement data sample sitting in a 32-bit register (long). I'm doing the following to get the sample written out as a signed integer. Is this correct? Can it be this simple?

shl   sample, #8                  ' move MSbit to bit 31
sar  sample, #8                  ' shift back down to where it belongs; preserving the sign
wrlong sample, HUB_mem_ptr  ' place this signed int into HUB memory for later...





I've run this and things look good, but I want to be sure that's not accidental.

Thanks,
pgb

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-15 01:12
    Thats odd, I thought there was an assembly instruction for sign extend, oh well. Yes what you've done is mathmatically correct for sign extending a 24bit value to a 32 bit value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • pgbpsupgbpsu Posts: 460
    edited 2008-11-15 01:20
    Hi Paul-

    As always, thanks for your help.

    If there is an assembly instruction for sign extend I didn't see it. Let me know if you find one ; )

    To verify that what I'm doing is working it dawned on me that I could simply feed a sine wave into my ADC to make sure I'm getting back what I should. That will verify that I'm not making any mistakes and that I handle zero crossings correctly. So, off to write some more Prop code to help me verify the prop code I've already written...

    Have a great weekend.
    p
  • hippyhippy Posts: 1,981
    edited 2008-11-15 19:38
    No sign extend in PASM I know of and SHL/SAR is what I've used.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-15 23:47
    If the state of the sign bit (23) is already reflected in one of the condition bits (from a prior operation, say), you can do it in one instruction. This example assumes that bit 23 of value is reflected in the carry bit:

            if_c movi    value,#$1f
    
    
    


    Of course, this works only with 23- or 24-bit signed numbers that need extending! The MUXx instructions can also be used with an auxiliary long:

                 muxc    value,_0xff00_0000
                 ...
    _0xff00_0000 long    $ff00_0000
    
    
    


    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
Sign In or Register to comment.