Immediately after the previous post, the previous article was written using gas assembly to call the C language, this time speaking in C language Call gas assembly. The main use in the kernel writing is the former, but in the daily program optimization, the main use is the latter.
The same function, to achieve the exchange of two values, the parameters from the C language into the assembly, and then the C language to receive the return from the assembly function return value, the return value is divided into ordinary integer return value and string return value.
Set up three files, respectively main.c, RETSTR.S, Swapint.s. Where MAIN.C is the C language file, which is the main function, SWAPINT.S is the assembly function, used to exchange two variable values and return an integer value to the C language function, RETSTR.S is also an assembly language function, used to demonstrate assembly language output ScreenTip, using the direct write stdout and C library two ways, and then Returns a string to the C language function.
The code for MAIN.C is as follows:
#include <stdio.h>char *retstr (); #C程序默认函数返回值为整数值, here to declare, it should be a string pointer. int main () {int a;int b;int C;char *str;a=10;b=20;c=swapint (&a,&b);p rintf ("The ' Swapint ' return:%d\n", c); printf ("now:a=%d;b=%d\n", A, B), Str=retstr ();p rintf ("The ' Retstr ' return:%s", str); return 0;}
The code for SWAPINT.S is as follows:
. section. Text.globl swapint.type swapint, @functionswapint:p USHL%ebpmovl%esp,%ebppushl%ebxmovl 8 (%EBP),%ESIMOVL 12 ( %EBP),%EDIMOVL (%esi),%ebxxchg%ebx, (%edi) Movl%ebx, (%esi) movl $2333,%eaxpopl%ebxmovl%ebp,%esppopl%ebpret
The code for RETSTR.S is as follows:
. section. Datamsg:.asciz "Use syscall:the value have been swapped!\n" msgend:.equ len,msgend-msgmsg2:.asciz "call ' printf ' successfully!\n "Retvalue:.asciz" It ' s ok!\n ". Section. Text.globl retstr.type retstr, @functionretstr:p USHL%ebpmovl% ESP,%EBPPUSHL%ebx#using System CALLMOVL $4,%eax#the number of SYSCALLMOVL $1,%ebx#fdmovl $msg,%ecx#the pointer of MSGMOVL $len,%edx#the lengthint $0x80#call "Write" #using C librarypushl $msg 2call printf#return a stringmovl $retvalue,%eax#leav EPOPL%EBXMOVL%EBP,%ESPPOPL%ebpret
Write a script debug-auto.sh automatically compile these files, along with debugging information, convenient GDB debugging.
As-o swapint.o swapint.s-gstabsas-o retstr.o retstr.s-gstabsgcc-o a.out swapint.o retstr.o main.c-gstabs
After that, the compilation runs with the following results:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4B/AF/wKiom1QwwYixh5bQAARP3o5_N-E807.jpg "title=" capture. JPG "alt=" Wkiom1qwwyixh5bqaarp3o5_n-e807.jpg "/>
This is the synthetic invocation of the gas assembler function in C language.
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/1560680
Using C language to call gas compilation under Linux--a comprehensive example