看内核的时候遇到一个问题,一个.S文件跟一个.c文件都有一个同名的变量,都是全局的,那么编译之后.S文件先执行,当时我以为是.S中的变量与.c中的变量是同一个变量,其实是错误的,代码验证
A=10
.section .data
output:
.asciz "this is [%d]\n"
.secion .text
.globl main
main:
call hello
pushl $A
pushl $output
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
此为 1.S文件
#include <stdio.h>
int A=5;
void hello(){
printf("hello world A=[%d]\n", A);
}
此为 2.c文件
编译命令: gcc -o test 1.S 2.c
./test
结果: A=5, A=10 //两个不同结果,说明彼此的A是不同的,那么如何才能引用c中的A呢?
代码如下: 1.S 改一下
.extern int A;
.section .data
output:
.asciz "this is [%d]\n"
.secion .text
.globl main
main:
call hello
pushl A //注意传过来的是A的地址
pushl $output
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
要注意的是 如果在 .extern int A; 的下一行 这样 A= 10 ,那么pushl A这里的A总认为是 A=10中的A,并非extern int A 中的A
335

被折叠的 条评论
为什么被折叠?



