Blum's book only speaks of the C language Call assembly, which does not speak of assembly call C. I tried it myself.
The final test was successful, and it was written here to share with you. During the countless errors, countless anomalies, I am not drunk, but kneeling ... Fortunately, the last good.
The program implements a value-change function, defines the a=10,b=20 in the MAIN.S, and then calls the C-language function to change the value of B.
The new two files are main.s and the C language function files of pro.c.
The code for MAIN.S is as follows:
. Section. Data
A:
. int 10
B:
. int 20
. section. Text
. GLOBL Main
. type main, @function #别忘了这句 because the main assembly function is also called by Crt0.s, and main is essentially a function
Main
MOVL $a,%eax
MOVL $b,%EBX
PUSHL%EBX
PUSHL%eax
Call Swapint #不要写成 _swapint
MOVL $1,%eax
MOVL $0,%EBX
int $0x80
The code for PRO.C is as follows:
#include <stdio.h>
int swapint (int *a,int *b)
{
int C;
Char *str= "success!!";;
C=*a;
*a=*b;
*b=c;
Puts (str); #用puts可以输出
Puts ("end!"); #用puts可以输出
printf ("Output??"); #用printf会造成此句无输出, Cause: The buffer is not full #用 The empty buffer can cause the output.
return 0;
}
In the assembly function, the address of a, B is stacked, note the C function parameters from right to left in the order of the stack. That is, the first pressure stack B, after a.
After the direct call, the CPU will automatically stack the return address, and then control over the C language function, then the C language function automatically take parameters, you do not have to control. "What the C function does is that 8 (%EBP) takes out the address of a that is pressed in the stack (we are the address of the stack), then the address of the B that is pressed in the stack (%EBP) is taken out, and then the value of the operation is started ... No mystery. "
Finished, the first was printf no output tangled for a while, but previously learned Linux under the C language programming, but also specifically research buffer problem.
The compilation process is as follows:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4B/8F/wKioL1Qun_LSC_BSAAEZ6tgNhiQ953.jpg "title=" 1.JPG " alt= "Wkiol1qun_lsc_bsaaez6tgnhiq953.jpg"/>650) this.width=650; src= http://s3.51cto.com/wyfs02/M02/4B/8C/ Wkiom1qun9ax_drvaaewyanehgs883.jpg "title=" 2.JPG "alt=" wkiom1qun9ax_drvaaewyanehgs883.jpg "/>650) this.width= 650; "Src=" http://s3.51cto.com/wyfs02/M00/4B/8F/wKioL1QuoBuCZN5BAAGKbGTz5lg197.jpg "title=" capture. JPG "alt=" Wkiol1quobuczn5baagkbgtz5lg197.jpg "/>
You can then debug with GDB. "Output not exported here"?? " because I didn't empty the buffer! "
That's it.
There are questions to welcome the discussion.
This article is from the "mirage1993" blog, make sure to keep this source http://mirage1993.blog.51cto.com/2709744/1560510
Linux assembly gas Call C language function instance