REXX HEX Math

The purpose of the following REXX program is to demonstrate use of the PARSE and SELECT statements and the X2D (hex to decimal) and D2X (decimal to hex) REXX functions. It occurred to me that a simple REXX program to perform math operations on hex values would provide a good illustration.

Table 1 below contains a complete REXX program that will allow the user to perform one of four simple math operations on two hexadecimal numbers. The supported operations are addition, subtraction, multiplication, and division.

Users should never be trusted and for that reason the program checks to insure that the user has supplied three parameters to the program. Notice that I am not testing the validity of the hex values being input. A more thorough validity check would be one that verified that the first and third parameters were valid hex values and that the operator parameter contained a valid operator. The necessary data validation routines are beyond the scope of this tutorial so will not be addressed here.

The program is expecting the user to supply three command line parameters: the first number, the math operator, and the second number. This program uses a simple technique to see if at least three parameters are present. The ARG statement in line 9 takes the space separated values entered as parameters on the command line and moves them into the variables arg1, arg2, and arg3 in left to right order. In line 10, arg3 is tested for a null value. If the user did not supply a complete parameter list, the DO loop that starts at line 11 is executed. Lines 12 to 15 use the SAY command to display the necessary messages on the screen. Line 15 uses the SAY command to instruct the user to enter their equation. Line 16 uses the PULL command to take the line of input typed in by the user and put it in the variable ureply.

The PARSE statement in line 17 is used to take the three space separated values input by the user and place each of them into the variables arg1, arg2, and arg3 in order. This particular form of the PARSE command implements the following syntax:

PARSE variable_with_blank_delimited_values WITH variable1 variable2 ... variableN

Line 20 is used to set an initial value for the outputanswer variable which will be used in a logic operation to determine if there is valid output to print.

Lines 21 to 30 are the SELECT statement. The SELECT is used as an alternative to subsetting IF statements. The SELECT statement makes it easy to identify the individual math operation and to carry out that operation. The four WHEN substatements are used to identify the math operator contained in the arg2 variable. If arg2 contains a value other than +, -, *, or / the OTHERWISE substatement is executed.

The xd2 function used in lines 22 to 25 tells REXX to take the string that is in the variable, and assumed to be a valid hex string, and to convert it into its decimal equivalent. For example, in line 22, the string in variable arg2 is converted into a decimal number by the x2d function and the same is done for the string in variable arg3. Then the two decimal values are added together and the result stored in the variable result.

Lines 26 through 29 are executed if the character in the arg2 variable is not a +, -, *, or /. Line 27 uses the SAY command to write an error message to the screen and line 28 sets the value of the outputanswer variable to 0.

In line 32, the IF statement is used to see if the variable outputanswer has a value of 1. If it does, that means that a math operation was executed and the answer is in the variable result. The answer, along with the original equation input by the user, is written to the screen using the SAY command. Note that before the answer, contained in the variable result is written to the screen, the REXX function d2x is used to convert the decimal number back into a hex value.

This is a very basic program. You may want to consider expanding its functionality. For example, you could:

And that's it. Feel free to copy the program to your IBM host system and experiment with it.

REXX Hexmath Source Code Listing
00001 /***************************** rexx *************************/
00002 /* Name:   HEXMATH                                          */
00003 /* Author: Jim Plaxco                                       */
00004 /* Purpose: Demonstrate use of parse and select statements  */ 
00005 /*          and the x2d (hex to decimal) and                */ 
00006 /*          the d2x (decimal to hex) functions              */ 
00007 /* Parms:   Number Operator Number                          */ 
00008 /************************************************************/ 
00009 arg arg1 arg2 arg3                       
00010 if arg3 = "" then                        
00011   do                                     
00012     say "Error: You did not specify a complete equation"      
00013     say "       The equation should be X operator Y "    
00014     say "----------------------------------------------"     
00015     say 'Enter your equation: '                             
00016     pull ureply                                                
00017     parse value ureply with arg1 arg2 arg3                     
00018   end                                                          
00019                                                            
00020 outputanswer=1                                               
00021 select                                                        
00022   when arg2 = '+' then  result = x2d(arg1) + x2d(arg3)       
00023   when arg2 = '-' then  result = x2d(arg1) - x2d(arg3)       
00024   when arg2 = '*' then  result = x2d(arg1) * x2d(arg3)         
00025   when arg2 = '/' then  result = x2d(arg1) / x2d(arg3)       
00026   otherwise do                                               
00027                say "Error: " arg2 " is not a legal operator" 
00028                outputanswer=0                                
00029             end                                              
00030 end /* end of the select */                                  
00031                                                              
00032 if outputanswer then say arg1' 'arg2' 'arg3' is 'd2x(result)''

Return to the Mainframe section index.