【正文】
mov cx,[bp+6] looptop: add word ptr [di],10 inc di inc di loop looptop pop bp ret _arrayproc endp end Example with arrays in Borland C continued: the main include extern void arrayproc(int [],int)。 void main(){ int i,x[20]。 //fill array anyhow for(i=0。i20。i++)x[i]=i*5。 arrayproc(x,20)。 for(i=0。i20。i++)printf(%d\n,x[i])。 } Running …first two pieces of output were generated in the sub W:\assembly\turboC\BINarray 65486 20 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 Another borland c example…using coprocessor extern C funct(double)。 include double a,b。 void main(){ a=。 funct(a)。 printf(answer %f,b)。 } What does “C” mean? ? Cpp pilers use “name mangling” to handle function overloads. Internally, those overloaded functions wind up with names distinguished by their argument lists. ? To turn off name mangling, you can ask the piler to use “C” piler conventions for piling functions. The function .model small .data extrn _b:qword .code public _funct _funct proc near push bp mov bp,sp fld qword ptr [bp+4] fstp _b fwait pop bp ret _funct endp end Borland example 2, using coprocessor run W:\assembly\turboC\BINcopro answer Another coprocessor example ? include ? extern C funct(double,double *)。 ? double a,b。 ? void main(){ ? a=。 ? funct(a,amp。b)。 ? printf(answer %f,b)。 ? } function .model small .code public _funct _funct proc near push bp mov bp,sp fld qword ptr [bp+4] fld qword ptr [bp+4] fadd st,st(1) mov si,[bp+12] fstp qword ptr [si] fwait pop bp ret _funct endp end Run of copro2 W:\assembly\turboC\BINcopro2 answer W:\assembly\turboC\BIN An assembly main .model small .stack 100h dataseg val dw ? num dw ? prompt db 39。enter39。,0ah,0dh,39。$39。 p2 db 39。value is39。,0ah,0dh,39。$39。 crlf db 0ah,0dh,39。$39。 include codeseg EXTRN _addnums:proc include include main proc near mov ax, @data mov ds,ax message prompt call dec_in push bx message crlf message prompt call dec_in push bx message crlf call _addnums message p2 call dec_out mov ax,4c00h int 21h main endp end main The c function ? extern C int addnums(int,int)。 ? int addnums(int a,int b){ ? return a+b。} run W:\assembly\turboC\BINasmtoc enter 123 enter 456 value is 579 W:\assembly\turboC\BIN Linking assembly and C ? You can use assembly inline ? You can link c functions to an assembly main ? Or visa versa ? Some versions of Borland C are available in the labs ? I used the text’s VC++ examples. ? As mentioned earlier, you need 32 bit TASM (Borland assembler) to properly configure assembly with borland c programs. main extern C void asm_main()。 // asm startup proc void main() { asm_main()。 } Directory listing .586 .MODEL flat,C 。 Standard C library functions: system PROTO, pCommand:PTR BYTE printf PROTO, pString:PTR BYTE, args:VARARG scanf PROTO, pFormat:PTR BYTE,pBuffer:PTR BYTE, args:VARARG fopen PROTO, mode:PTR BYTE, filename:PTR BYTE fclose PROTO, pFile:DWORD BUFFER_SIZE = 5000 .data str1 BYTE cls,0 str2 BYTE dir/w,0 str3 BYTE Enter the name of a file: ,0 str4 BYTE %s,0 str5 BYTE cannot open file,0dh,0ah,0 str6 BYTE The file has been opened and closed,0dh,0ah,0 modeStr BYTE r,0 fileName BYTE 60 DUP(0) pBuf DWORD ? pFile DWORD ? Asm code part .code asm_main PROC 。 clear the screen, display disk directory INVOKE system,ADDR str1 INVOKE system,ADDR str2 。 ask for a filename INVOKE printf,ADDR str3 INVOKE scanf, ADDR str4, ADDR fileName 。 try to open the file INVOKE fopen, ADDR fileName, ADDR modeStr mov pFile,eax .IF eax == 0 。 cannot open file? INVOKE printf,ADDR str5 jmp quit .ELSE INVOKE printf,ADDR str6 .ENDIF 。 Close the file INVOKE fclose, pFile quit: ret 。 return to C++ main asm_main ENDP END Directory listing: running exe in debug Creating your own obj modules Assemble directory to obj C:\Program Files\Microsoft Visual Studio 8\VCML /nologo Zi c Fl Sg coff Assembling: Compile to obj (/c means pile only) C:\Program Files\Microsoft Visual Studio 8\VCcl /c Microsoft (R) 32bit C/C++ Optimizing Compiler Version for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. Create the Exe /EHsc is error handling and /Gy enables function linkage C:\Program Files\Microsoft Visual Studio 8\VCcl /EHsc /Gy Running from VC++ dos prompt public and external ? public, in an assembly module, declares that a symbol defined in this module should be made available (by linker) to other modules being linked. ? extern indicates that a symbol used in this module is not defined here, but in another module (Borland C) // // Calls the external LongRandom function, written in // assembly language, that returns an unsigned 32bit // random integer. Compile in the Large memory model. include extern C unsigned long LongRandom()。 //C indicates to the piler not to use name mangling but standard C naming //long will return long (32 bit) int in dx:ax const int ARRAY_SIZE = 500。 int main() { // Allocate array storage