0

I am trying to learn ARM assembly and I wanted to store a signed number in a register, it has to be something like -

MOV R0, #-10

But this stores the number normally and not as a signed number. I tried using LDRSB as -

LDRSB R0, =0x0000000A, but it gave me errors.

Kruti Deepan Panda
  • 324
  • 1
  • 4
  • 16
  • 1
    What do you mean _"stores the number normally"_? When I assemble `MOV R0, #-10` in ARM mode using the GNU assembler, the assembler converts it into `MVN R0, #9`. That instruction will load `R0` with `NOT 9` == `0xFFFFFFF6` == `-10`. – Michael Jan 31 '21 at 09:44
  • As for the second example; `LDRSB` doesn't negate the source operand (it won't turn 10 into -10), it simply sign-extends the operand from 8 to 32 bits. So if you wanted -10 you have to say so: `LDRSB R0, =-10`. That's a bit of an odd way of doing this though, since you could just use `MVN/MOV`. In fact, GAS will convert that `LDRSB` into an `MVN`. – Michael Jan 31 '21 at 09:52
  • which instruction set? arm or thumb? did you mean armv7 not arm7 adding in thumb2 to the mix? – old_timer Jan 31 '21 at 14:27
  • what part of the documentation do you not understand? – old_timer Jan 31 '21 at 14:27
  • Are you unhappy about how the number is encoded as an immediate for `mvn`? Like that the machine code doesn't contain a negative 2's complement integer anywhere? Why is that a problem for your use-case? Are you writing a JIT code-generator and want to use a `signed int immediate:8` bitfield member or something? That's not how ARM works; pick a different ISA like MIPS or x86 if you want simple sign-extended immediates. – Peter Cordes Jan 31 '21 at 19:15
  • what assembler are you using? (gas, armasm, etc?) – old_timer Feb 01 '21 at 14:56
  • Yeah sorry, it was a dumb question on my part. I realized that if I want signed bits then I have to write the code accordingly on my part taking the MSB into consideration. – Kruti Deepan Panda Feb 02 '21 at 14:27

1 Answers1

1

Assembly language is specific to the assembler. Did you mean arm7 or armv7?

This is gnu assembler

mov r0,#-10
ldr r0,=-10

.thumb

mov r0,#-10
ldr r0,=-10

.syntax unified

mov r0,#-10
ldr r0,=-10

so.s: Assembler messages:
so.s:12: Error: cannot honor width suffix -- `mov r0,#-10'
so.s:7: Error: invalid immediate: -10 is out of range

try again

mov r0,#-10
ldr r0,=-10

.thumb

@mov r0,#-10
ldr r0,=-10

.syntax unified

@mov r0,#-10
ldr r0,=-10


00000000 <.text>:
   0:   e3e00009    mvn r0, #9
   4:   e3e00009    mvn r0, #9
   8:   4800        ldr r0, [pc, #0]    ; (c <.text+0xc>)
   a:   4800        ldr r0, [pc, #0]    ; (c <.text+0xc>)
   c:   fffffff6    .word   0xfffffff6

so were you looking at (full sized) ARM then?

   0:   e3e00009    mvn r0, #9
   4:   e3e00009    mvn r0, #9

Which is the easiest one of the three to encode. And you can easily see the encoding in the ARM documentation. And the tools have already done half the work for you (0xfffffff6).

If you meant armv7 then

mov r0,#-10
ldr r0,=-10

.thumb

@mov r0,#-10
ldr r0,=-10

.syntax unified

mov r0,#-10
ldr r0,=-10

00000000 <.text>:
   0:   e3e00009    mvn r0, #9
   4:   e3e00009    mvn r0, #9
   8:   f06f 0009   mvn.w   r0, #9
   c:   f06f 0009   mvn.w   r0, #9
  10:   f06f 0009   mvn.w   r0, #9
old_timer
  • 69,149
  • 8
  • 89
  • 168