• No results found

Summary of group one

The group one instructions have the following form: <op1>{cond}{S}{P} <dest>,<lhs>,<rhs>

<op2>{cond}{S}{P} <dest>,<rhs> <op3>{cond}{S}{P} <lhs>,<rhs>

where <op1> is one of ADD, ADC, AND, BIC, EOR, ORR, RSB, RSC, SBC, SUB, <op2> is one of MOV, MVN, and <op3> is one of TEQ, TST,CMN, CMP.

The following <op3>s have no <dest> field: CMN, CMP, TEQ, TST. They allow the use of the {P} option to set the <dest> field in the machine code to R15 instead of the default R0.

The following <op2>s have no <lhs> field: MOV, MVN.

? <dest> and <lhs> are registers.

? <rhs> is #<expression> where <expression> is a 12-bit shifted immediate operand, or ? <register>, or

and where <count> is #<expression> or <register> where <expression> is five-bit unsigned value, or

? <register>, RRX

3.3 Group two - load and store

We turn now to the first set of instructions concerned with getting data in and out of the processor. There are only two basic instructions in this category. LDR loads a register from a specified location and STR saves a register.

As with group one, there are several options which make the instructions very versatile. As the main difference between LDR and STR is the direction in which the data is

transferred (i.e. to or from the processor), we will explain only one of the instructions in detail - STR. Notes about LDR follow this description.

STR Store a word or byte

Addressing modes

When storing data into memory, you have to be able to specify the desired location. There are two main ways of giving the address, called addressing modes. These are known as pre-indexed and post-indexed addressing.

Pre-indexed addressing

Pre-indexed addressing is specified as below: STR{cond} <srce>,[<base>{,<offset>}]

<srce> is the register from which the data is to be transferred to memory. <base> is a register containing the base address of memory location required. <offset> is an optional number which is added to the address before the data is stored. So the address actually used to store the <srce> data is <base>+<offset>

Offset formats

The <offset> is specified in one of two ways. It may be an immediate number in the range 0 to 4095, or a (possibly) shifted register. For example:

STR R0,[R1,#20]

will store R0 at byte address R1+20. The offset may be specified as negative, in which case it is subtracted from the base address. An example is:

which stores R0 at R1-200.

(Note for alert readers. The immediate offset is stored as a 12-bit magnitude plus one bit 'direction', not as a 13-bit two's complement number. Therefore the offset range really is - 4095 to +4095, not -4096 to +4095, as you might expect.)

If the offset is specified as a register, it has the same syntax as the <rhs> of group one instructions. That is, it could be a simple register contents, or the contents of a register shifted or rotated by an immediate number.

Note: the offset register can only have an immediate shift applied to it. In this respect, the offset differs from the <rhs> of group one instructions. The latter can also have a shift which is stored in a register.

This example stores R0 in the address given by R1+R2*4: STR R0,[R1,R2,LSL#2]

Again, the offset may be negative as this example illustrates: STR R0,[R1,-R2,LSL#3]

Write-back

Quite frequently, once the address has been calculated, it is convenient to update the base register from it. This enables it to be used in the next instruction. This is useful when stepping through memory at a fixed offset. By adding a ! to the end of the instruction, you can force the base register to be updated from the <base>+<offset> calculation. An

example is:

STR R0,[R1,#-16]!

This will store R0 at address R1-16, and then perform an automatic: SUB R1,R1,#16

which, because of the way in which the ARM works, does not require any extra time. Byte and word operations

All of the examples of STR we have seen so far have assumed that the final address is on a word boundary, i.e. is a multiple of 4. This is a constraint that you must obey when using the STR instruction. However, it is possible to store single bytes which may be located at any address. The byte form of STR is obtained by adding B at the end. For example:

STRB R0,[R1,#1]!

will store the least significant byte of R0 at address R1+1 and store this address in R1 (as ! is used).

Post-indexed addressing

The second addressing mode that STR and LDR use is called post-indexed addressing. In this mode, the <offset> isn't added to the <base> until after the instruction has executed. The general format of a post-indexed STR is:

STR{cond} <srce>,[<base>],<offset>

The <base> and <offset> operands have the same format as pre-indexed addressing. Note though that the <offset> is always present, and write-back always occurs (so no ! is needed). Thus the instruction: