nasm,64,linux,分段故障核心转储

时间:2013-06-25 07:33:02

标签: c assembly segmentation-fault nasm

这是foo.asm

extern choose;
[section .data]
num1st dq 3
num2nd dq 4
[section .text]
global main
global myprint
main:
  push qword [num2nd]
  push qword [num1st]
  call choose
  add esp,8
  mov ebx,0
  mov eax,1
  int 0x80
  ;  pop qword [num1st]
  ;  pop qword [num2nd]
myprint:
  mov edx,[esp+8]
  mov ecx,[esp+4]
  mov ebx,1
  mov eax,4
  int 0x80
  ;  pop qword [num1st]
  ;  pop qword [num2nd]
  ret

这是一个C-asm程序

这是bar.c

void myprint(char * msg ,int len);
int choose(int a,int b) 
{ 
  if (a>=b){
    myprint("the 1st one\n",13);}
  else {
    myprint("the 2nd one\n",13);}
  return 0;
}

nasm -f elf64 foo.asm

gcc -c bar.c

gcc -s -o foobar bar.o foo.o

./ foobar,它表示分段故障核心转储

我使用gdb进行调试,但是它说缺少debuginfo-install,我也试图安装它。

也许这个问题与86_64拱门有关...

Segmentation fault when pushing on stack (NASM) 在看了这个链接之后,我添加了一些“pop”#39;进入它,但它没有工作

1 个答案:

答案 0 :(得分:1)

参数不会在堆栈in 64-bit mode上传递,除非您有超过6个参数。前两个参数将位于RDIRSI

在64位模式下使用系统调用的方式也有所不同。系统调用号和参数应放在以下寄存器(source)中:

syscall nr  rax
arg 1       rdi
arg 2       rsi
arg 3       rdx
arg 4       r10
arg 5       r9
arg 6       r8

64位模式is 1, not 4中的sys_write系统调用号。而且,而不是int 0x80 you should use syscall。使用int 0x80 执行系统调用可能以64位模式工作,具体取决于内核的配置方式,但您仍需要考虑如何传递函数参数。