//转载 地址 http://mirage1993.blog.51cto.com/2709744/1560680
//这个不是内联,但是对理解C和汇编的混合使用提供思路
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<stdio.h>
char
*retstr(); #C程序默认函数返回值为整数值,这里要声明下,应该是字符串指针。
int
main()
{
int
a;
int
b;
int
c;
char
*str;
a=10;
b=20;
c=swapint(&a,&b);
printf
(
"The 'swapint' return:%d\n"
,c);
printf
(
"Now:a=%d;b=%d\n"
,a,b);
str=retstr();
printf
(
"The 'retstr' return:%s"
,str);
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
.section .text
.globl swapint
.type swapint,@function
swapint:
pushl %ebp
movl %esp,%ebp
pushl %ebx
movl 8(%ebp),%esi
movl 12(%ebp),%edi
movl (%esi),%ebx
xchg %ebx,(%edi)
movl %ebx,(%esi)
movl $2333,%eax
popl %ebx
movl %ebp,%esp
popl %ebp
ret
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
.section .data
msg:
.asciz
"Use syscall:The value has been swapped!\n"
msgend:
.equ len,msgend-msg
msg2:
.asciz
"Call 'printf' successfully!\n"
retvalue:
.asciz
"It's ok!\n"
.section .text
.globl retstr
.type retstr,@function
retstr:
pushl %ebp
movl %esp,%ebp
pushl %ebx
#using system call
movl $4,%eax #the number of syscall
movl $1,%ebx #fd
movl $msg,%ecx #the pointer of msg
movl $len,%edx #the length
int
$0x80 #call
"write"
#using C library
pushl $msg2
call
printf
#return a string
movl $retvalue,%eax
#leave
popl %ebx
movl %ebp,%esp
popl %ebp
ret
|
1
2
3
|
as -o swapint.o swapint.s -gstabs
as -o retstr.o retstr.s -gstabs
gcc -o a.out swapint.o retstr.o main.c -gstabs
|