Chapter Contents

Previous

Next
Inline Machine Code Interface

Example of the Inline Machine Code Interface

This example is a C implementation of the compare and swap example in IBM System/370 Principles of Operation. Register conventions have been changed to conform to C usage. (The original example used registers 6, 7, and 8, which may be allocated to register variables in C.)

#include <code.h>
#include <genl370.h>
#include <ctl370.h>

int word;                     /* the flag word */
char flag = 0x80;             /* the bit to be turned on */

/* put word in R0, flag in R14 and        */
/* &word in R1.  word is loaded only      */
/* once to avoid inconsistent results     */
/* if it is updated by another processor. */
retry:
   _ldregs(R0+R1+R14, word, &word, flag << 24);

   LR(15,0);                     /* copy word to R15 */
   OR(15,14);                    /* turn on the flag */
   CS(0,15,0+b(1));              /* try to update the word*/

   if (_cc() != 0)               /* try again if swap failed */
      goto retry;

This version of the example performs an unnecessary reload of registers in the unlikely event that CS returns a non-zero condition code. The unnecessary reload cannot be corrected by attaching the retry label to the LR instruction because inserting the label between the call to _ldregs and the LR instruction may cause one of the registers loaded by _ldregs to be updated. However, the following code sequence does bypass the problem for a slight performance improvement in the event that CS returns a value other than 0.

#include <code.h>
   #include <genl370.h>
   #include <ctl370.h>

   int word;                     /* the flag word */
   char flag = 0x80;             /* the bit to be turned on */

      /* Put word in R0, flag in R14 and        */
      /* &word in R1.  word is loaded only      */
      /* once to avoid inconsistent results     */
      /* if it is updated by another processor. */
   _ldregs(R0+R1+R14, word, &word, flag << 24);

   BALR(2,0);                    /* set up a base register      */
   LR(15,0);                     /* copy word to R15            */
   OR(15,14);                    /* turn on the flag            */
   CS(0,15,0+b(1));              /* try to update the word      */
   BCR(7,2);                     /* try again if swap failed    */


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.