Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can ARM's MOV instruction work with a large number as the second operand?

I just begin to study ARM assembly language, and am not clear about how to use MOV to transfer an immediate number into a register.

From both the ARM reference manual and my textbook, it's said that range of immediate number following MOV instruction is 0-255. But when I test on my own PC in ADS 1.2 IDE, instruction

MOV     R2, #0xFFFFFFFF

performs well. Isn't number 0xFFFFFFFF out of range according to the specification?

like image 381
Summer_More_More_Tea Avatar asked Apr 12 '10 18:04

Summer_More_More_Tea


People also ask

What does MOV ARM do?

In ARM state: MOV can load any 8-bit immediate value, giving a range of 0x0 - 0xFF (0-255). It can also rotate these values by any even number. These values are also available as immediate operands in many data processing operations, without being loaded in a separate instruction.

How does a MOV instruction work?

The MOV instruction moves data bytes between the two specified operands. The byte specified by the second operand is copied to the location specified by the first operand. The source data byte is not affected.


2 Answers

A single ARM instruction can only encode an immediate constant that can be represented as an 8-bit immediate value, shifted by any even power of two.

However, there is also a MVN instruction, which is like MOV but inverts all the bits. So, while MOV R2, #0xFFFFFFFF cannot be encoded as a MOV instruction, it can be encoded as MVN R2, #0. The assembler may well perform this conversion for you.

like image 33
Matthew Slattery Avatar answered Oct 09 '22 06:10

Matthew Slattery


Remember that the ARM can perform a certain set of manipulations on the immediate value as part of the barrel shifter that is incorporated into the ARM's opcodes.

This little article has one of the clearest explanations of some of the tricks that an ARM assembler can use to fit a large immediate number into the small available space of an ARM instruction:

  • http://www.davespace.co.uk/arm/introduction-to-arm/immediates.html

The article discusses the trick likely used in your specific example of generating a MVN opcode to load the bitwise complement of the immediate value.

These kinds of manipulation can't be done with all immediate values, but the ARM assemblers are supposedly pretty smart about it (and C compilers certainly are). If no shift/complement tricks can be performed, the value will generally be loaded from a PC-relative location or maybe by 'building up' the value from several instructions.

like image 175
Michael Burr Avatar answered Oct 09 '22 06:10

Michael Burr