home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / PCACHSRC.ZIP / C2ASM.ASM < prev    next >
Assembly Source File  |  1991-11-09  |  3KB  |  92 lines

  1. ;C2ASM.ASM   B.Kauler 1990.
  2. ;the skeleton .ASM module below was created by compiling a skeleton C program
  3. ;of arbitrary name filename1.c, as follows;
  4. ;
  5. ; add(a,b)
  6. ; int a,b;
  7. ; { int x; x=a+b; return x; }
  8. ;
  9. ;compiled with the /Fa switch to generate .ASM output, and the /c switch to
  10. ;suppress linking --   CL /Fa /c filename1.C ,,,   (Microsoft C v6.0)
  11. ;
  12. ;Have a look at the listing below.  Note that a & b are defined as "int",
  13. ;which corresponds to MASM's "word", that is, 16 bits, so word-size values
  14. ;are passed on the stack to the .ASM routine.  By default the "add" function
  15. ;returns a word-size value via AX, to variable x.
  16. ;
  17. ;.................................
  18. INCLUDELIB    SLIBCE
  19. _TEXT    SEGMENT  WORD PUBLIC 'CODE'
  20. _TEXT    ENDS
  21. _DATA    SEGMENT  WORD PUBLIC 'DATA'
  22. _DATA    ENDS
  23. CONST    SEGMENT  WORD PUBLIC 'CONST'
  24. CONST    ENDS
  25. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  26. _BSS    ENDS
  27. DGROUP    GROUP    CONST, _BSS, _DATA
  28.     ASSUME DS: DGROUP, SS: DGROUP
  29. EXTRN    __aNchkstk:NEAR
  30. extrn _var1:word        ;added by me. see notes below.
  31. ;
  32. _TEXT      SEGMENT
  33.     ASSUME    CS: _TEXT
  34.     PUBLIC    _add
  35. _add    PROC NEAR
  36.     push    bp
  37.     mov    bp,sp
  38.     mov    ax,2
  39.     call    __aNchkstk        ;this is a compiler-supplied routine, to check that
  40.                         ;the stack has enough room (no. bytes spec. by AX).
  41.     mov    ax,WORD PTR [bp+6]    ;b
  42.     add    ax,WORD PTR [bp+4]    ;a
  43.  
  44.  adc ax,_var1           ;added by me. see notes below.
  45.  
  46.     mov    sp,bp
  47.     pop    bp
  48.     ret    
  49.     nop    
  50. _add    ENDP
  51. _TEXT    ENDS
  52. END
  53. ;......................................
  54. ;
  55. ;this basic skeleton can be used for any .ASM routine.
  56. ;here is a C program that will call the above .ASM routine --
  57. ; #include <stdio.h>
  58. ; int var1=2;
  59. ; main()
  60. ; { int x,y=3,z=5; x=add(y,z); printf("answer = %hu",x); }
  61. ;
  62. ;it defines x,y and z as 16-bit, passes y and z on the stack to the .ASM
  63. ;routine, which returns a value to x via AX register.
  64. ;x,y and z are local variables, however I have also included a global
  65. ;variable, "var1" -- look back at the .ASM routine to see how it can
  66. ;access a C global variable.
  67. ;if this C program is called filename2.c, and the .ASM program is filename1,
  68. ;this is the process of compiling & linking with MS C v6.0 --
  69. ; MASM filename1;               ;filename1.asm --> filename1.obj
  70. ; CL /c filename2.c ,,,         ;filename2.c --> filename2.obj
  71. ; LINK filename2+ filename1;    ; --> filename2.EXE
  72. ;
  73. ;what if you want to pass values other than 16-bit?
  74. ;C's "long int" is the same as MASM's "doubleword", that is, 32 bits.
  75. ;here is a C skeleton that you can compile to .ASM for passing long int's;
  76. ; long int add(a,b)
  77. ; long int a,b;
  78. ; { long int x; x=a+b; return x; }
  79. ;
  80. ;as this must also return a long integer, Microsoft C does so in DX:AX.
  81. ;A master C program that can call such a .ASM module --
  82. ; #include <stdio.h>
  83. ; main()
  84. ; { long int x,y=3,z=5; x=add(y,z); printf("answer = %lu",x); }
  85. ;
  86. ;note that "%lu" is a format specifier for x, being "long unsigned integer".
  87. ;Actually, you may need to specifiy add() as being external, and for 
  88. ;certain memory models, such as small, you will need to provide an 
  89. ;override if you want the add() module to be far --
  90. ;  extern far long int add(long int, long int);
  91. ;..............................................................
  92.