Jump to content
IGNORED

I guess I am missing something.....


marc.hull

Recommended Posts

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....

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 !!

Link to comment
Share on other sites

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).

 

:)

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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

 

:P

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...