1
\$\begingroup\$

I have just started to program some code at the lower levels.

I have been told to change any signed integers into unsigned integers and store these in the HW registers. I have done this OK but I am wondering why I need to do that? What if I would have a 32bit signed integer, How would I handle that?

The values are going to be used by the HW itself sometimes, but also by SW. When SW uses them I convert them back.

All of the HW registers are 32bit in size.

\$\endgroup\$
  • \$\begingroup\$ What level are you programming at, for what hardware? For every CPU I'm familiar with, signed and unsigned are characteristics of the instructions operating on the registers, not of the register contents. Are you on a snipe hunt? \$\endgroup\$ – Russell Borogove Nov 21 '14 at 16:43
  • \$\begingroup\$ This is for programming the registers for some custom HW. \$\endgroup\$ – user1876942 Nov 24 '14 at 6:28
4
\$\begingroup\$

When you store a value in a hardware register you are storing the bit representation of that value in the register. What effect that will have is entirely defined by the particular register.

In a lot of cases hardware registers contain either an unsigned value (counters, (pre-)scalers, A/D-result) or a set of bit fields. It is generally easier to work with such values as unsigned.

Occasionally you will find a register that has its bits in a 2s-complement form (fi. in an A/D converter with a differential input), in that case it will be more appropriate to load it from or to a signed variable.

\$\endgroup\$
  • \$\begingroup\$ In almost all cases, the 32bit register will be split so that bits 0:3 mean x and 4:7 y etc... In these cases, I will change from signed to unsigned. For example in 8bit -12 becomes 244. Is this a form of normalization? Thanks. \$\endgroup\$ – user1876942 Nov 21 '14 at 12:15
  • \$\begingroup\$ In the case that you describe, the bit fields are nearly always unsigned, so why start signed anyway? \$\endgroup\$ – Wouter van Ooijen Nov 21 '14 at 13:19
  • \$\begingroup\$ upper layer codes use that as it makes it easier to understand. e.g. -3 means shift 3 left + 3 right. \$\endgroup\$ – user1876942 Nov 24 '14 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.