0

I have a subroutine and need to calculate a function that involves some multiplication and addition inside of it. The thing is that I don't understand how to store a signed integer in memory.

Do I use a normal integer and work with it in a particular way?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user3721428
  • 298
  • 1
  • 4
  • 14
  • Does this answer your question? http://stackoverflow.com/questions/21359298/how-to-do-add-sub-signed-or-unsigned-integer-correctly – Jerry101 Mar 19 '15 at 23:04
  • It definitely helps, would anyone be able to provide me an example of using those flags in addition/multiplication? (using add and mult instructions) . Would I just add "vs" to all relevant instructions? – user3721428 Mar 19 '15 at 23:10
  • 1
    The flags indicate if the value overflowed, which is useful if you want to check for that or carry into longer precision arithmetic. If you're just getting started you could defer that part. To the original question, storing a register value to RAM doesn't depend on whether you think of the value as signed or not. Use a STR instruction – Jerry101 Mar 19 '15 at 23:14
  • I would just store them using dcd because I know the particular numbers. I need to be able to handle positive and negative outcomes, in terms of precision I don't expect to use numbers above 150. Any tips for that? – user3721428 Mar 19 '15 at 23:17
  • 1
    Recommendation: Read a book or tutorial on the subject. https://www.google.com/?q=arm%20assembly%20tutorial&es_th=1#q=arm+assembly+tutorial – Jerry101 Mar 19 '15 at 23:34
  • 1
    bits is bits, the processor doesnt know an integer from a float from a pointer, etc...they are just bits, a rare clock cycle here or there the bits are used for something but even a pointer is an operand from time to time a float is just bits being moved, etc. what part of the processor documentation for the instructions you are using did you not understand? – old_timer Apr 23 '19 at 20:15

1 Answers1

3

Storing a value just copies its binary pattern into memory, so signness is irrelevant here. But one has to differentiate between signed and unsigned types when loading a value narrower than the register width, because the CPU needs to know that it should zero extend or sign extend the value to fill the register.

Besides, ARM uses two's complement, which means addition, subtraction and non-widening multiplication on two values don't care that the values are signed or unsigned because the result will be exactly the same. The same to all bitwise operations except right shift. For the basic operations, only widening multiplication, division, comparison for larger than/less than and right shift produce different result for signed, thus require a different instruction. You should read this answer for more information.

Further reading

phuclv
  • 37,963
  • 15
  • 156
  • 475