I'm trying to move a negative value in thumb.
In ARM its easy,
mvn r0, #1
How can I move negative values to registers in Thumb?
Thanks.
In ARM its easy
Actually, it is not so easy. For instance, if you want to emulate return -1;
you need the following,
mvn r0, #0
The reason is that mvn
is not (or bits flipped) and negative with twos complement needs you to not with subtraction of one. More information is found on the ARM site.
We can see examples at god bolt,
The thumb1 version is,
mov r0, #1
neg r0, r0
The neg
or negate is doing the twos-complement arithmetic for us. Newer versions of the thumb instruction set (often called thumb2) have the ability to do a version of the mvn
instruction. You can simply write mov r0, #-1
and let the compiler/assembler figure out the arithmetic for you. It will examine the constant and select the mvn
encoding with the source being a little more humon friendly.
Obviously, if you only have thumb1 and no mvn
instruction exists, the mov r0, #-1
will not assemble and you need some two (16 bit) instructions/opcodes to achieve the same thing. There are probably a variety of ways to get a negated value using two instructions (depending on constant value and/or surrounding code).
If you really intended mvn r0, #1
, I would suggest a comment of ; -2 to 'r0'
.
If you intended a bit mask, then ldr r0, =0xfffffffe
can be used and it will pick mvn r0,#1
if it is available. However, your question title said negative which usually implies twos-complement.