Electrical – How to load immediate negative value in a register

assemblyavr

For example on 8-bit AVR given a register r16 I do something like ldi r16, 44 and this way I can give it positive values from 0 to 255.
But if I want negative clearly in two's complement I can get a range of -128…+127 but I can't do that like.

ldi r16, -13

And furthermore how does the microprocessor know whether I want all 8-bits for positive 0…255 or negative -128 to 127? Thank you very much for your help.

Best Answer

how does the microprocessor know whether I want all 8-bits for positive 0...255 or negative -128 to 127?

The processor doesn't know and doesn't care.

The advantage of using two's complement representation for signed numbers is that add and subtract operations produce the same results from the same inputs whether you say the inputs are unsigned or two's complement values.

Of course you will have to watch out for different overflow conditions in the two cases --- but typically the processor provides flags that can detect overflow for either type of operation.

But if I want negative clearly in two's complement I can get a range of -128...+127 but I can't do that like.

ldi r16, -13

If you can't, that's a limitation of your assembler, not of the processor.

If you use the Microchip AVR Assembler, you should be able to exactly that, according to the documentation.