Chapter Contents

Previous

Next
Communication with Assembler Programs

Calling a C Function from Assembler

A function written in C can be called from an assembler function as long as general register 12 addresses the CRAB when the C function is called. If the function is a library function, the calling (assembler) function must use the CENTRY and CEXIT macros to preserve the DSA chain. Most library functions depend upon being called from a normal C framework, that is, with general register 12 addressing the CRAB and general register 13 addressing a C DSA.

Calling a C Library Function from Assembler shows the sumint function expanded to call printf to write the result to stdout . Note that the printf parameter list is created in the DSA and that the CENTRY parameter DSA now specifies a non-zero DSA size. This version of sumint is called from the C main program in Sample C main Program.


Calling a C Library Function from Assembler
EJECT PRINT ON,GEN SUMINT@ CSECT CREGS USING SPACE SUMINT CENTRY INDEP=NO,DSA=DSALEN *---------------------------------------------------------------------* * Make sure we actually got a plist address on the call. * *---------------------------------------------------------------------* SR R3,R3 Clear R3 for sum'ing LTR 1,1 Is there a plist? BZ DONE Nope, just leave w/R3=0! SPACE *---------------------------------------------------------------------* * Sum integers passed via VL-format parameter list. * *---------------------------------------------------------------------* SR R3,R3 Clear R3 for summing NEXTADD DS 0H L R4,0(R1) Load pointer A R3,0(R4) Add integer to sum TM 0(R1),X'80' End of VL-Plist? <---Note This Check BNZ DONE Yes, finish up and exit LA R1,4(R1) No, bump to next argument B NEXTADD Start again DONE DS 0H Yes, prepare to return SPACE *---------------------------------------------------------------------* * Call printf to display the sum of integers * *---------------------------------------------------------------------* ST R3,SUMINTS SUM of int's to Parmlist MVC FMTPTR,=A(FORMAT) Move format pointer to PRINTF * Parmlist L R15,=V(PRINTF) R15 -> PRINTF LA R1,PARMLIST R1 -> Parmlist Address BALR R14,R15 Call PRINTF SPACE *---------------------------------------------------------------------* * Exit with the sum of the integers provided to CEXIT in R3. * *---------------------------------------------------------------------* CEXIT RC=(R3),INDEP=NO EJECT *---------------------------------------------------------------------* * Constants * *---------------------------------------------------------------------* LTORG Area for Literal Pool FORMAT DC X'15' New Line Before Output DC C'Assembler Sum: %d' DC X'1500' New line with NULL terminator *---------------------------------------------------------------------* * Working Storage * *---------------------------------------------------------------------* COPY DSA Required for CENTRY/CEXIT PARMLIST DS 0D FMTPTR DS A Address of printf parmlist SUMINTS DS F Sum of int's DSALEN EQU *-DSA Length of DSA *---------------------------------------------------------------------* * Dsects * *---------------------------------------------------------------------* COPY CRAB Required for CENTRY/CEXIT END SUMINT


Chapter Contents

Previous

Next

Top of Page

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