C language call assembly

Source: Internet
Author: User

(The sample code is downloaded online and modified and commented out)

Compile tcmain. C as follows:

/* Program function: Create an integer array, enter the element values of the array, and call the assembly code to obtain the element
Position of the prime maximum value and the maximum value in the array */
# Include <stdio. h>
Void main ()
{
Extern found (int I, int * j, int * k, int * s );
Int I, J, K, s [10];
Printf ("Enter values of array:/N ");
For (I = 0; I <10; I ++)
Scanf ("% d", & S [I]);
I = 10;
Found (I, & J, & K, S );
Printf ("/nthe max_value is: % d/N", J );
Printf ("the place is: % d/N", k );
}

Then write tcfound. ASM as follows:

Public _ found
_ Text Segment byte public 'code'
Assume Cs: _ text
_ Found proc near
Push BP
MoV bp, SP
MoV CX, [bp + 4]; obtained I value: 10
Dec CX
MoV Si, [bp + 10]; get the starting address of S Array
Lodsw; load words from Si address to ax
MoV dx, 1
MoV BX, 0
Comp: CMP ax, [Si]
Ja bigger
MoV ax, [Si]; find bigger
MoV BX, DX
Bigger: Inc Si
INC Si
INC DX
Loop comp
MoV Di, [bp + 6]; get J address
MoV [di], ax
MoV Di, [bp + 8]; get K address
MoV [di], BX
Pop BP
RET
_ Found endp
_ Text ends
End

Use MASM to compile tcfound. ASM for tcfound. OBJ, put tcmain. C and tcfound. OBJ in the directory where TC is located, and create the mytc. prj file in this directory. The content of the file is as follows:

Tcmain. c
Tcfound. OBJ

Use tc to open mytc. prj: press Alt + O --> linker --> case-sensitive link and press enter to turn on --> off. Return to the options menu and select Save options. press Alt + P to enter the project name as mytc. prj: press Alt + c --> build all.

Run mytc. EXE on the command line interface, as shown below:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.