home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16974 < prev    next >
Encoding:
Text File  |  1992-11-21  |  4.0 KB  |  135 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!titan!trlluna!bruce.cs.monash.edu.au!monu6!giaeb!s1110238
  3. From: s1110238@giaeb.cc.monash.edu.au (Lee Hollingworth)
  4. Subject: Re: Help with ASM/C interface
  5. Message-ID: <s1110238.722388720@giaeb>
  6. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  7. Organization: Monash University, Melb., Australia.
  8. References: <ad#@byu.edu>
  9. Date: Sat, 21 Nov 1992 23:32:00 GMT
  10. Lines: 123
  11.  
  12. curtisn@SAS (Curtis Nelson) writes:
  13.  
  14. >I have a question I hope one of you can help me with.  I'm using Borland C++
  15. >3.0 and I am trying to write a simple assembly language routine in my code.
  16.  
  17. Embedded assembly code in a C function?
  18.  
  19. If so there is no problem, you simply use your C variables.
  20.  
  21. Example (written in MSC format BC should not be too different);
  22.  
  23. int write_port(unsigned char ch, int port)
  24. {
  25.     _asm {
  26.         mov     ah, 01h
  27.         mov     ah, ch
  28.         mov     dx, port
  29.         int     14h
  30.     }
  31. }
  32.  
  33. The return value of the int 14h function is in AX, and is automatically
  34. returned by the asm code.  You can be more explicit by adding in a variables.
  35.  
  36. int write_port(unsigned char ch, int port)
  37. {
  38.     int status;
  39.  
  40.     _asm {
  41.         mov     ah, 01h
  42.         mov     ah, ch
  43.         mov     dx, port
  44.         int     14h
  45.         mov     status, ax
  46.     }
  47.  
  48.     return status & 0x80;   /* status of write */
  49. }
  50.  
  51. >My question is, how do I pass vaues back and forth?  For example, if I push
  52. >a value onto the stack in assembly, how con I retrieve that value from C?  I
  53. >know I can use the stack, but how do I access an item on the stack?  I've
  54. >looked at 'pop', but that appears to only work in C++, and I'm writing this
  55. >in straight C.
  56.  
  57. If you are interfacing with assembly code in a mixed language environment
  58. you access parameters passed to the asm code by calculating the offset from
  59. bp.  Generally this is equal to 2 + size of return address + total size of
  60. parameters between bp and the parameter you are trying to access.
  61.  
  62. Eg, a FAR procedure that has received one parameter with a four-byte address.
  63.  
  64.     displacement = 2 + size of return address
  65.                  = 2 + 4
  66.                  = 6
  67.  
  68. To load the parameter into a register you then would use,
  69.  
  70.         mov     bx, [bp+6]
  71.  
  72. or you can define your own parameter names in your code such as,
  73.  
  74.     foo     EQU     [bp+6]
  75.  
  76. then to access foo it is simply,
  77.  
  78.         mov     bx, foo
  79.  
  80. Returning values from asm code is simple if the return data type size is
  81. four bytes or less.
  82.  
  83.     Data Size       Returned in
  84.        1               AL
  85.        2               AX
  86.        4               High byte DX  Low byte AX
  87.  
  88. If the value to be returned is greater than 4 bytes, your C function must
  89. allocate space for the return value, and place the address of that space
  90. in DX:AX.
  91.  
  92. Here is a simple source file for some C - ASM interface code...
  93.  
  94. ;--------------------------------------------------------------------------
  95. key_ready   equ     11h             ; function K_READY
  96.  
  97.             .model small
  98.             .code
  99. public      _prnwrite
  100. ;------------------------- prnwrite ----------------------------------------
  101. ;   call    unsigned int prnwrite(ch, port);
  102. ;           unsigned char ch
  103. ;           unsigned int port
  104. ;               refer to biosibm.h for defines
  105. ;  returns  status in ah
  106. ;----------------------------------------------------------------------------
  107. _prnwrite   proc
  108.             push    bp
  109.             mov     bp, sp              ; mov stack pointer to base pointer
  110.             push    si
  111.             push    di
  112.  
  113.             mov     al, [bp+4]          ; get character to print
  114.             mov     dx, [bp+6]          ; get port number
  115.             mov     ah, 00h
  116.             int     17h
  117.             xor     al, al              ; clear al, ah returned
  118.  
  119.             pop     di
  120.             pop     si
  121.             pop     bp
  122.             ret
  123. _prnwrite   endp
  124.  
  125.             end
  126. ;-------------------------------------------------------------------------
  127.  
  128. >I would appreciate any help!
  129.  
  130. I think you would get more help if you used the comp.os.msdos.programmer
  131. newsgroup.
  132.  
  133. Lee Hollingworth
  134. s1110238@giaeb.cc.monash.edu.au
  135.