Intel x86 下 gcc汇编结果

ptfcwnsohq 2012-06-04 03:59:15
加精
GCC的汇编语言用的是AT&T的语法。

源文件:test.c

#include <stdio.h>
#include <fcntl.h>

int swap(int *a, int *b)
{
int c ;
c = *a;
*a = *b;
*b = c;
return c;
}

int main(int argc, char *argv[])
{
int a, b, c;
a = 16; b = 32;
c = swap(&a, &b);
return 1;
}



汇编结果: gcc -S test.c

.file "test.c"
.text
.align 2
.globl swap
.type swap,@function
swap:
pushl %ebp
movl %esp, %ebp
subl $4, %esp 为整型局部变量c 在栈中分配空间
movl 8(%ebp), %eax 取第一个参数a
movl (%eax), %eax 取a地址里的内容 *a

movl %eax, -4(%ebp) 保存到局部变量c中
movl 8(%ebp), %edx 取第一个参数a
movl 12(%ebp), %eax 取第二个参数 b
movl (%eax), %eax 保存*b
movl %eax, (%edx) 将*b 保存到 a的地址里(*a)
movl 12(%ebp), %edx 再去参数b
movl -4(%ebp), %eax 将局部变量c的值(先已经是a的值)取出
movl %eax, (%edx) 将这个指保存到b的地址里(*a)
movl -4(%ebp), %eax 返回值c保存在eax中
leave 等同于 movl %ebp, %esp; popl %ebp;
ret
.Lfe1:
.size swap,.Lfe1-swap
.align 2
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $16, -4(%ebp)
movl $32, -8(%ebp)
subl $8, %esp
leal -8(%ebp), %eax
pushl %eax
leal -4(%ebp), %eax
pushl %eax
call swap
addl $16, %esp
movl %eax, -12(%ebp) 将swap的返回值(在eax中),放到局部变量c中
movl $1, %eax 将eax赋值为返回值 1
leave
ret
.Lfe2:
.size main,.Lfe2-main
.ident "GCC: (GNU) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)"

8(%ebp)是第一个参数 12(%ebp)是第二个参数

-4(%ebp)是第一个局部变量

函数返回值 保存在eax中

原文地址:http://blog.csdn.net/richardysteven/article/details/4195055
...全文
637 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiying12571 2012-06-10
  • 打赏
  • 举报
回复
学习,谢谢分享
lghuang 2012-06-08
  • 打赏
  • 举报
回复
哇塞 还在写初级语言啊
  • 打赏
  • 举报
回复
好久没有玩了 都忘记了
叶子 2012-06-07
  • 打赏
  • 举报
回复
学习,谢谢分享,建议代码还是加上代码标记比较好看。
爱知菜 2012-06-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

-masm=intel
[/Quote]在C++吧里见过这个头像
zhangsongcui 2012-06-07
  • 打赏
  • 举报
回复
-masm=intel
lzp123asd 2012-06-06
  • 打赏
  • 举报
回复
恩,有点意思
lixixixi2 2012-06-06
  • 打赏
  • 举报
回复
好好好
mudy222 2012-06-06
  • 打赏
  • 举报
回复
hi every body
hi every body
hi every body
hi every body
赵4老师 2012-06-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0)
char *p1="1" ,*p2="2" ;
char c1=1 , c2=2 ;
short s1=1 , s2=2 ;
int i1=1 , i2=2 ;
__int64 I1=1i64, I2=2i64;
float f1=1.0f, f2=2.0f;
double d1=1.0 , d2=2.0 ;
void main() {
SWAP((int)p1,(int)p2); printf("char * %5s, %5s\n",p1,p2);
SWAP(c1,c2); printf("char %5d, %5d\n",c1,c2);
SWAP(s1,s2); printf("short %5d, %5d\n",s1,s2);
SWAP(i1,i2); printf("int %5d, %5d\n",i1,i2);
SWAP(I1,I2); printf("__int64 %5I64d,%5I64d\n",I1,I2);
SWAP(*(int *)&f1,*(int *)&f2);printf("float %5g, %5g\n",f1,f2);
SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double %5lg, %5lg\n",d1,d2);

SWAP(c1,c1);
printf("%d\n",c1);
}
//char * 2, 1
//char 2, 1
//short 2, 1
//int 2, 1
//__int64 2, 1
//float 2, 1
//double 2, 1
//2

566

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧