5

I am starting to look a bit more precisely at ARM assembler and I looked up some dumps from objdump. I saw a lot of instruction (add is not the only one) with an extra s at the end (adds, subs, ...).

I looked a bit to the ARM documentation and it seems to mean something significant, but I can't figure out exactly what (the documentation I found about it seemed extremely obscure to me).

Has somebody some insight on what is the meaning of this extra s added at the end of some ARM instructions ?

2 Answers 2

11

Usual ADD doesn't update flags. ADDS does. See better documentation at arm infocenter. As it wrote there:

If S is specified, these instructions update the N, Z, C and V flags according to the result.

1
  • 2
    @perror If you want another concrete example, consider a loop while(--i){...}. One way to implement it is to have sub reg, #1 followed by an explicit compare to zero (which will set the Z zero flag when reg becomes 0), then a predicated branch. With subs reg, #1 the Z flag is implicitly updated, therefore no explicit comparison is required. May 5, 2014 at 3:19
13

The extra s character added to the ARM instruction mean that the APSR (Application Processor Status Register) will be updated depending on the outcome of the instruction.

The status register (APSR) contain four flags N, Z, C and V which means the following:

  • N == 0: The result is greater or equal to 0, which is considered positive, and so the N (negative) bit is set to 0.
  • Z == 1: The result is 0, so the Z (zero) bit is set to 1.
  • C == 1: We lost some data because the result did not fit into 32 bits, so the processor indicates this by setting C (carry) to 1.
  • V = 0: From a two's complement signed-arithmetic viewpoint, 0xffffffff really means -1, so the operation we did was really (-1) + 1 = 0. That operation clearly does not overflow, so V (overflow) is set to 0.

More information about the condition flags in the ARM architecture can be found here.

6
  • One additional point: depending on the disassembler/settings, the Thumb-1 instructions (short, 16-bit encodings for a subset of instructions) may be displayed without the S suffix, even though they, in fact, do update flags. See Table 4-9 here.
    – Igor Skochinsky
    May 4, 2014 at 18:55
  • Is it because it is the default behavior of the instruction in thumb mode, or the opcodes are really different but the mnemonic does not show up the difference ?
    – perror
    May 4, 2014 at 19:02
  • 1
    The former. In Thumb-1, all ALU instructions set the flags and so I guess the creators dropped the suffix as "unnecessary". With Thumb-2, full equivalence with the ARM set was achieved and you can choose if you want to set flags or not. Thus the move to UAL and explicit S suffix.
    – Igor Skochinsky
    May 4, 2014 at 20:05
  • Shouldn't the explanation to N == 0 be The result is greater than or equal to 0 ... ? May 4, 2014 at 21:50
  • @perror There is another tricky thing about flags which is called flexible second operand: C flag can be updated in some cases even if S suffix does not exist in the command, see infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0489i/… for more details
    – w s
    May 5, 2014 at 5:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.