Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17185
  • 博文数量: 6
  • 博客积分: 1481
  • 博客等级: 上尉
  • 技术积分: 55
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-31 00:24
文章分类
文章存档

2010年(6)

我的朋友
最近访客

分类:

2010-06-23 17:34:01

初始问题:为什么我输出不了Project1中的第二句话??

仔细查看user/a.c

/*
 * A simple program for testing ELF parsing and loading
 *
/* ELF_Print only prints NULL-terminated strings;
 * no formatting or other fancy features */

void ELF_Print(char* msg);


char s1[40] = "Hi ! This is the first string\n";


int main(int argc, char** argv)
{

 char s2[40] = "Hi ! This is the second string\n";


   ELF_Print(s1);
   ELF_Print(s2);

   return 0;
}




不妨反汇编下这个test.c

.file "test.c"
.globl s1
  .data
  .align 32
  .type s1, @object
  .size s1, 40
s1:
  .string "this is first string \n"
  .zero 17
  .section .rodata

.LC0:
  .string "%s"
.LC1:
  .string "end of file"
  .text
.globl main
  .type main, @function
main:
  pushl %ebp
  movl %esp, %ebp
  andl $-16, %esp
  subl $64, %esp
  movl $1936287860, 24(%esp)
  movl $544434464, 28(%esp)
  movl $1868785011, 32(%esp)
  movl $1931502702, 36(%esp)
  movl $1852404340, 40(%esp)
  movl $2663, 44(%esp)
  movl $0, 48(%esp)
  movl $0, 52(%esp)
  movl $0, 56(%esp)
  movl $0, 60(%esp)
  movl $.LC0, %eax
  movl $s1, 4(%esp)
  movl %eax, (%esp)
  call printf
  movl $.LC0, %eax
  leal 24(%esp), %edx
  movl %edx, 4(%esp)
  movl %eax, (%esp)
  call printf
  movl $.LC1, (%esp)
  call puts
  movl $0, %eax
  leave
  ret
  .size main, .-main
  .ident "GCC: (GNU) 4.4.2 20091027 (Red Hat 4.4.2-7)"
  .section .note.GNU-stack,"",@progbits


现在应该明白为什么看不到第二句话了吧??
因为局部变量根本不在数据段里啊!

出处:http://www.cnblogs.com/herso/archive/2009/03/22/1419013.html

1.     全局变量

全局变量的作用域是整个程序
全局变量的初始化:
全局变量定义中既可以指定初值,也可以只用问号预留空间。
全局变量定义.data?段中时,只能用问号预留空间,因为.data?段不能指定初始值。
定义时用问号指定的全局变量的初始值是0。
 
2.     局部变量
局部变量的好处是使程序的模块结构更加分明。
局部变量的缺点是因为空间是临时分配的,所以无法定义含有初始化值的变量,对局部变量的初始化一般子程序中由指令完成。
局部变量的作用域是单个子程序。
局部变量定义堆栈中。
 
局部变量的初始化:
局部变量无法定义的时候指定初始化值,因为local伪指令只是为局部变量留出空间。
局部变量的初始值是随机的,所以,对局部变量的值一定要初始化。
一般子程序中使用指令来初始化局部变量

3. 一般全局变量存放在数据区局部变量存放在栈区
动态变量存放在堆区,函数代码放在代码区


出处:http://student.csdn.net/space.php?uid=76660&do=blog&id=12420


阅读(1743) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~