Exercise - Using the Data Stack

Write an exec that puts the letters T, S, O, E on the data stack in such a way that they spell TSOE when removed. Use the QUEUED built-in function and the PULL and SAY instructions to help remove the letters and display them. To put the letters on the stack, you can use the REXX instructions PUSH, QUEUE, or a combination of the two.

ANSWER

Possible Solution 1

/******************************** REXX *****************************/
/*  This exec uses the PUSH instruction to put the letters T,S,O,E,*/
/*  on the data stack in reverse order.                            */
/*******************************************************************/

 PUSH 'E'                                /**************************/
 PUSH 'O'                                /* Data in stack is:      */
 PUSH 'S'                                /*    (fourth push)    T  */
 PUSH 'T'                                /*    (third push)     S  */
                                         /*    (second push)    O  */
 number = QUEUED()                       /*    (first push)     E  */
 DO number                               /**************************/
   PULL stackitem
   SAY stackitem
 END

Possible Solution 2

/******************************** REXX *****************************/
/* This exec uses the QUEUE instruction to put the letters T,S,O,E,*/
/* on the data stack in that order.                                */
/*******************************************************************/

 QUEUE 'T'                              /***************************/
 QUEUE 'S'                              /*  Data in stack is:      */
 QUEUE 'O'                              /*     (first queue)    T  */
 QUEUE 'E'                              /*     (second queue)   S  */
                                        /*     (third queue)    O  */
 DO QUEUED()                            /*     (fourth queue)   E  */
   PULL stackitem                       /***************************/
   SAY stackitem
 END

Possible Solution 3

/******************************** REXX *****************************/
/* This exec uses the PUSH and QUEUE instructions to put T,S,O,E   */
/* on the data stack.                                              */
/*******************************************************************/

 PUSH 'S'                               /***************************/
 QUEUE 'O'                              /*  Data in stack is:      */
 PUSH 'T'                               /*     (second push)    T  */
 QUEUE 'E'                              /*     (first push)     S  */
                                        /*     (first queue)    O  */
 DO QUEUED()                            /*     (second queue)   E  */
   PULL stackitem                       /***************************/
   SAY stackitem
 END