gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Storing signed 16/8bit variables

#10007 - funkeejeffou - Sun Aug 24, 2003 5:38 pm

For loading signed value, there is "ldrsh" and "ldrsb", but for storing a signed variable, must we right shift the register?
Code:
mov r0, r0 asr#16;
strh r0, [r1]

#10011 - Lupin - Sun Aug 24, 2003 6:04 pm

I think this is for converting 32 bit values to 16 bit, because strh won't do this for you.
I don't know if this works, but maybe this would be better:

ldrh r1, [r0 ASR #16]

I'm 90% sure that I'm wrong though :(

#10012 - tom - Sun Aug 24, 2003 7:03 pm

funkeejeffou wrote:
but for storing a signed variable, must we right shift the register?


no.

#10018 - funkeejeffou - Sun Aug 24, 2003 7:51 pm

no....
Then strh converts a 32bit negative number to a 16bit number :
mov r0, 0xFFFFFFFF ;r0 = -1
strh r0, [r1] ;0xFFFF in [r1]

or should we do this :

mov r0, 0xFFFFFFFF ;r0 = -1
and r0, r0, 0xFFFF ;mask 16 low bit
strh r0, [r1] ;0xFFFF in [r1]

or something else I don't know.

Please don't post such "short" answers.
Thanks

#10027 - DekuTree64 - Sun Aug 24, 2003 10:48 pm

There's no difference between storing a signed or unsigned halfword. The only time it matters is when you're loading a 16-bit value into a 32 bit register, and it needs to be sign extended, which is why there's an instruction for it. But if your number fits into 16 bits, and it's negative, then the upper 16 will all be 1, so when you store the lower 16 bits with strh and then load it with ldrsh, it will fill the upper 16 with 1s again, so it's the same as it was.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#10028 - tom - Sun Aug 24, 2003 10:50 pm

i thought quoting the part of your message my "no" refered to was enough =)

if you do something like
Code:

strh r0,[r1]

the cpu will simply store the lower half (bits 0..15) of r0 at [r1], which is what you want, i suppose.

#10030 - funkeejeffou - Sun Aug 24, 2003 11:33 pm

Thanks, that's what I wanted to know.