marc.hull Posted July 21, 2011 Share Posted July 21, 2011 I am trying to convert a signed byte to a signed word and am running into issues. The code below is what I am using but it doesn't seem to work. Any thoughts ? *R0 contains a signed number in the low byte ANDI R0,>00FF mask out unneeded data CI R0,128 is number negative ? Jl OUT no so we are done NEGNUM AI R0,-127 0-127 are positive 128-255 are negative NEG R0 turn R0 into a negative word OUT RT Doesn't seem to ever create a negative word. If someone has a better (or working) way I am all ears....er eyes.... Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted July 21, 2011 Share Posted July 21, 2011 I'm not familiar with TI assembly code but could you not just OR $FF00 into R0 if the jump isn't taken? 1 Quote Link to comment Share on other sites More sharing options...
lucien2 Posted July 21, 2011 Share Posted July 21, 2011 SLA R0,8 SRA R0,8 1 Quote Link to comment Share on other sites More sharing options...
marc.hull Posted July 21, 2011 Author Share Posted July 21, 2011 I'm not familiar with TI assembly code but could you not just OR $FF00 into R0 if the jump isn't taken? I don't believe so. The TI sees 16 bit words instead of bytes so you have (I think) to do the subtraction and the negation. Quote Link to comment Share on other sites More sharing options...
marc.hull Posted July 21, 2011 Author Share Posted July 21, 2011 SLA R0,8 SRA R0,8 You've lost me. Please explain this ... Quote Link to comment Share on other sites More sharing options...
lucien2 Posted July 21, 2011 Share Posted July 21, 2011 SLA R0,8 SRA R0,8 You've lost me. Please explain this ... If you take -2 as an exemple, R0 = 00FE You shift it 8 bits left, R0 = FE00 You shift it 8 bits right (SRA = Shift Right Arithmetic, fill vacated bit positions with the sign bit), R0 = FFFE Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted July 21, 2011 Share Posted July 21, 2011 I was just going to suggest swpb r0 sra r0,>8 much the same, just that swpb is faster than any shift. Quote Link to comment Share on other sites More sharing options...
GroovyBee Posted July 21, 2011 Share Posted July 21, 2011 Hmmmm.... You guys are all TI Jedi Masters at assembly language. I'll keep quiet until I get around to doing something for the machine. Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted July 21, 2011 Share Posted July 21, 2011 I'm not familiar with TI assembly code but could you not just OR $FF00 into R0 if the jump isn't taken? Not a bad idea. Quote Link to comment Share on other sites More sharing options...
marc.hull Posted July 21, 2011 Author Share Posted July 21, 2011 SLA R0,8 SRA R0,8 You've lost me. Please explain this ... If you take -2 as an exemple, R0 = 00FE You shift it 8 bits left, R0 = FE00 You shift it 8 bits right (SRA = Shift Right Arithmetic, fill vacated bit positions with the sign bit), R0 = FFFE EXTREEMLY useful. Thank you !! Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted July 21, 2011 Share Posted July 21, 2011 I'm not familiar with TI assembly code but could you not just OR $FF00 into R0 if the jump isn't taken? I don't believe so. The TI sees 16 bit words instead of bytes so you have (I think) to do the subtraction and the negation. I believe so. There's not an OR in 9900, but you can do a SOC (Set Ones Corresponding) instead (E/A page 187). Quote Link to comment Share on other sites More sharing options...
marc.hull Posted July 21, 2011 Author Share Posted July 21, 2011 I'm not familiar with TI assembly code but could you not just OR $FF00 into R0 if the jump isn't taken? I don't believe so. The TI sees 16 bit words instead of bytes so you have (I think) to do the subtraction and the negation. I believe so. There's not an OR in 9900, but you can do a SOC (Set Ones Corresponding) instead (E/A page 187). I see that now ! ORI R0,>FF00. Simple, small and fast. Thanks Groovey bee (and everyone else.) Quote Link to comment Share on other sites More sharing options...
Willsy Posted July 21, 2011 Share Posted July 21, 2011 I am trying to convert a signed byte to a signed word and am running into issues. The code below is what I am using but it doesn't seem to work. Any thoughts ? *R0 contains a signed number in the low byte ANDI R0,>00FF mask out unneeded data CI R0,128 is number negative ? Jl OUT no so we are done NEGNUM AI R0,-127 0-127 are positive 128-255 are negative NEG R0 turn R0 into a negative word OUT RT Doesn't seem to ever create a negative word. If someone has a better (or working) way I am all ears....er eyes.... SLA Rx,8 SRA Rx,8 Quote Link to comment Share on other sites More sharing options...
Willsy Posted July 21, 2011 Share Posted July 21, 2011 SLA R0,8 SRA R0,8 Aww bum! Beaten to it Quote Link to comment Share on other sites More sharing options...
sometimes99er Posted July 21, 2011 Share Posted July 21, 2011 I see that now ! ORI R0,>FF00. Simple, small and fast. Thanks Groovey bee (and everyone else.) Shit, guess I've never used ORI (that SOC's) ... 1 Quote Link to comment Share on other sites More sharing options...
matthew180 Posted July 21, 2011 Share Posted July 21, 2011 What you are doing is called "sign extension". When you treat binary numbers as signed, the MSb is 1 for negative numbers and 0 for positive (probably not telling anyone anything new.) When you want to expand a 2's complement number to a larger unit of storage and retain a signed value, you have to repeat the MSb in the upper part of the new storage. All the solutions present will work: SLA R1,8 SRA R1,8 This works because the 'A' in SRA means "arithmetic" and retains the MSb in each bit position as it shifts right. Contrast this to SRL, the 'L' meaning "logical" and SRL will fill the vacated bits with '0'. SWPB R1 SRA R1,8 Same as above but faster. If you *already know* if the byte you want to expand is negative or positive, you can use a single OR or AND instruction (assumes going from a 8-bit to 16-bit): ORI R1,>FF00 // Sign extend the negative least significant byte in R1 ANDI R1,>00FF // Sign extend the positive least significant byte in R1 Quote Link to comment Share on other sites More sharing options...
marc.hull Posted July 21, 2011 Author Share Posted July 21, 2011 I see that now ! ORI R0,>FF00. Simple, small and fast. Thanks Groovey bee (and everyone else.) Shit, guess I've never used ORI (that SOC's) ... SOC'S ??? Really ? OK +1 for the pun . Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.