0

this is 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

it is a C-asm-program

this is 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 ,it says segmentation fault core dumped

I use gdb to debug ,but it says missing debuginfo-install, I am also trying to install it.

maybe the problem has sth to do with the 86_64 arch...

Segmentation fault when pushing on stack (NASM) after watched this link,I add some 'pop' into it but it doesn't work

4
  • Your functions don't seem to obey any sensible calling conventions?! They also don't seem to set up the stack frame.
    – Kerrek SB
    Jun 25, 2013 at 7:37
  • what is in myprint(..)?
    – Dayal rai
    Jun 25, 2013 at 7:38
  • thank you for reading !!! I'm sorry I will edit it !!!! I miss the way to compile the two!!! Jun 25, 2013 at 7:40
  • myprint() is in the C-code Jun 25, 2013 at 7:45

1 Answer 1

1

Arguments are not passed on the stack in 64-bit mode, unless you have more than 6 of them. The first two arguments will be in RDI and RSI.

There's also a difference in how you should use system calls in 64-bit mode. The syscall number and arguments should be placed in the following registers (source):

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

And the sys_write syscall number in 64-bit mode is 1, not 4. Also, instead of int 0x80 you should use syscall. Performing syscalls with int 0x80 might work in 64-bit mode depending on how your kernel has been configured, but you still need to consider how function arguments are passed.

2
  • by the way,how to let the cpu arch don't affect my program (not my code)? Jun 25, 2013 at 8:08
  • If you program in assembly you will be affected by the CPU architecture. It comes with the territory. I suggest that you avoid using system calls directly unless you have a very good reason to do so, and instead use libc functions like puts and printf. That will at least solve part of the problem.
    – Michael
    Jun 25, 2013 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.