shingaridavesh
shingaridavesh

Reputation: 991

Condition bits in SWI (ARM Instruction)

In ARM SWI instruction 32 bits are divided into 3 sets: 0:23 (System Call Number), 24:27 (0b1111), and 28:31 (Condition). What is the condition?

The book 'ARM SoC Architecture' mentions that 'If condition is passed the instruction enters supervisor mode.' When i check the sample code then the example has one CMP condition before the SWI but I am still not able to understand the reason for the condition. Moreover, several presentations on 'SWI for ARM' present on the Internet don't have any CMP condition before SWI. So I am confused that whether we need it or not and if yes, then what is the need?

Please help and Thanks in advance.

Upvotes: 1

Views: 2720

Answers (1)

artless-noise-bye-due2AI
artless-noise-bye-due2AI

Reputation: 22430

This is a fundamental ARM concept. Try to google ARM conditional execution OR instructions. For instance, today the Dave's space post gives a good overview of this concept. As well, the Wikipedia article referenced from the Stackoverflow ARM wiki has information on this topic.

In brief, ARM has four condition bits or flags NZCVnote; these are standard concepts to any assembler/machine language. They are,

  1. N for negative.
  2. Z for zero.
  3. C for carry.
  4. V for overflow.

ARM has 16 conditional execution prefixes represented in a four bit field that test variations of the condition bits,

  • 0000 - EQ meaning Equal with Zero flag set.
  • 0001 - NE meaning Not equal with the Zero clear.
  • 0010 - CS meaning Carry set or HS meaning unsigned higher or same with Carry set.
  • 0011 - CC meaning Carry clear or LO meaning unsigned lower with Carry clear.
  • 0100 - MI meaning Minus or negative with the Negative flag set.
  • 0101 - PL meaning Plus (including zero) with the Negative flag clear.
  • 0110 - VS meaning Overflow with the Overflow flag set.
  • 0111 - VC meaning No overflow with the Overflow clear.
  • 1000 - HI meaning an Unsigned higher with Carry set AND Zero clear.
  • 1001 - LS meaning Unsigned lower or same with Carry clear AND Zero set.
  • 1010 - GE meaning Signed greater than or equal with Negative equal to Overflow.
  • 1011 - LT meaning Signed less than with Negative not equal to Overflow.
  • 1100 - GT meaning Signed greater than with Zero clear AND Negative equal to Overflow.
  • 1101 - LE meaning Signed less than or equal with Zero set AND Negative not equal to Overflow.
  • 1110 - AL meaning Always. If there is no conditional part in assembler this encoding is used.
  • 1111 - NV; this is historical and deprecated, but for ARMv3 it meant never. Ie a nop. For newer ARMs (ARMv5+), this extends the op-code range.

Almost all ARM instructions are prefixed with this four bit field. This does not apply to Thumb or Thumb2 instructions. In unified assembler (assembles for either Thumb2 or ARM), the IT prefix is used.

An example might be as follows. You have this 'C' code,

 if(size > 0)
    write(fd, buffer, size);

The call write() is an OS call. The following is some sample assembler,

 ; fd is in r0, buffer in r1, size in r2

 cmp  r2, #0         ; if(size > 0)
 movgt r7, #NR_write ; OS constant for write().
 swigt #0            ; call OS write() if(size > 0)

 ; code resumes here whether or not the OS was called.

There are many other uses for the ARM conditionals.

Note: All modern ARM CPUs have a Q (saturation) flag. It behaves in a different way as it is an extension to the instruction set.

Upvotes: 2

Related Questions