Share via


Branching in SHx Inline Assembly

A branch instruction may be given a fixed displacement, or as a jump to a targeted label. If a label is used, the label must be defined within an __asm string in the current function. __asm labels are not visible outside of __asm strings. The __asm label must be isolated in its own __asm block. The distance from a label reference to its definition must fit within the displacement field of the corresponding branch instruction.

Although it is possible to branch between different __asm statements, it is not recommended. This could result in undefined behavior, especially when compiled with optimizing options.

The following example demonstrates how to use displacement branch instructions within __asm statements.

  __asm(
    "sett      ; set T bit\n"
    "bf 6      ; branch to print FAIL if not set\n"
    "jsr @R6   ; print PASS\n"
    "nop       ; delay slot\n"
    "bra 4     ; branch using displacement\n"
    "nop       ; delay slot\n"
    "jsr @R6   ; print PASS\n"
    "nop       ; delay slot\n"
    ,"PASS\n"  // argument #1 (R4)
    ,"FAIL\n"  // argument #2 (R5)
    ,printf    // argument #3 (R6)
  );

The following example demonstrates how to use labels in branch instructions within __asm statements.

#include <stdio.h>

void main()
{
  __asm(
    "sett      ; set T bit\n"
    "bf lab1   ; branch to print FAIL if not set\n"
    "jsr @R5   ; print PASS\n"
    "nop       ; delay slot\n"
    "bra lab2  ; branch using label\n"
    "nop       ; delay slot"
    ,"PASS\n"
    ,printf
    );
  __asm(
    "lab1:     ; isolate label"
    );
  __asm(
    "jsr @R5   ; print FAIL\n"
    "nop       ; delay slot"
    ,"FAIL\n"
    ,printf
    );
  __asm(
    "lab2:"
    );
}

See Also

Elements of the SHx __asm Block | The __asm Keyword in SHx Inline Assembly | __asm Restrictions in SHx Inline Assembly | Constants in SHx Inline Assembly | Symbols and Labels in SHx Inline Assembly | SHx Inline Assembly Parameters | SH-4 Mode Bits

 Last updated on Thursday, April 08, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.