我用AT&T语法编写了一个程序,用于GNU汇编程序:
.data
format: .ascii "%d\n"
.text
.global main
main:
mov $format, %rbx
mov (%rbx), %rdi
mov $1, %rsi
call printf
ret
Run Code Online (Sandbox Code Playgroud)
我使用GCC来组装和链接:
gcc -o main main.s
我用这个命令运行它:
./主要
当我运行程序时,我得到一个seg错误.通过使用gdb,它说printf没有找到.我试过".extern printf",但是没有用.有人建议我应该printf在RET之前存储堆栈指针并在RET之前恢复,我该怎么做?
#include <stdio.h>
int main(void){
int* p = NULL;
int y = 1;
p = &y;
printf("%p\n",p);
*(p+1) = 10;
printf("%p\n",p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
0x7ffe2368f2e4
0x7ffe0000000a
我不知道为什么p在这里被改变了,第二个有"0000000a",最后是10,你能帮助我吗?谢谢.我用linux中的gcc编译它.
我有四个文件 list.h list.c test_list.c Makefile
list.h
#ifndef List_H
#define List_H
#endif
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
list.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
test_list.c
#include "list.h"
#include <stdio.h>
int main(){
return 0;
}
/*nothing else*/
Run Code Online (Sandbox Code Playgroud)
Makefile文件
CC=cc
CXX=CC
CCFLAGS= -g -std=c99 -Wall -Werror
all: list test_list
%.o : %.c
$(CC) -c $(CCFLAGS) $<
test_list: list.o test_list.o
$(CC) -o test_list list.o test_list.o
test: test_list
./test_list
clean:
rm -f core *.o test_list
Run Code Online (Sandbox Code Playgroud)
当我在shell中输入make时,出现错误:
/ usr/bin/ld:/usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_line):重定位0具有无效的符号索引2/usr/lib/gcc/i686-linux- gnu/4.8 /../../../ i386-linux-gnu/crt1.o:在函数
_start':(.text+0x18): undefined reference tomain'collect2:error:ld返回1退出状态make:***[list]错误1 …