On 6/1/23 11:39:34, John McKown wrote:
I asked ChatGPT for an example HLASM program. Here's what I got:
BALR R9,0 Set the base register
USING *,R9 Establish a base register
LR R3,R9 Load the base register into R3
LA R4,ARRAY Load the address of the array into R4
LA R5,SIZE Load the size of the array into R5
L R6,0(R4) Load the first element of the array into
R6
L R7,=F'0' Initialize the sum to zero
LOOP AR R7,R6 Add the current element to the sum
A R4,=F'4' Move to the next element of the array
LA R8,1(0,R4) Load the address of the next element into
R8
L R6,0(R8) Load the next element into R6
CR R4,R5 Compare the current element to the end of
the array
BNE LOOP Branch back to LOOP if not at the end of
the array
BR R14 Return to the caller
ARRAY DC F'1' Array of numbers
DC F'2'
DC F'3'
DC F'4'
DC F'5'
SIZE DC F'5' Size of the array
Of course, there are a lot of problems with this, such as no CSECT and no
END. But I was amazed it had anything at all.It's fun to play with.
What happens if you ask not to translate the HLASM to C, but to write
a C program to compute the sum of the elements in an array?
Here's what I might do, based on the above:
#include <stdio.h>
long ARRAY[] = { 1, 2, 3, 4, 5 }; /* Array of numbers */
int main( void ) {
long R7 = 0;
long *R8;
for ( R8 = ARRAY; /* Address first element */
/* (Is there any way to avoid the magic number "5"?) */
R8 < ARRAY + 6; /* Test for end of ARRAY */
++ R8 ) /* Load the address of the next element into R8
*/
R7 += *R8;
printf( "Sum is %ld\n", R7 );
return( 0 ); }; /* Return to the caller */
--
gil