# linking problem using nasm and ld under 64 bit



## mf93 (Mar 22, 2010)

alright I'm beginning to learn assembly-language and I have created a sample program that does some adding and I'm getting a problem while linking. Heres the program---

```
USE64
global _start
section .stack 

section .text

_start:
   sub RCX,RCX			;set sum to zero
   mov RAX,47h			;starting number for adds
   par_loop:
      push RAX			;pass parameter
      dec RAX			;decrement amount of parms left
      inc RCX			;update number of parameters
      cmp RAX,40h		;if not done
      jne par_loop		;repeat
    push RCX			;final parameter
    call total_add		;find the ands of the parms

total_add:
    push RBP			;save EBP
    mov RBP,RSP			;start of parameters
    add RBP,16			;
    push RCX;			;save ECX
    push RDX			;save EDX
    mov RCX,[EBP+4]		;counter
    add RCX,8;			;beginning of parms
    add RCX,RCX			;multiply ECX by size of parameters
    add RCX,RCX			;
    add RCX,RCX			;
    add RCX,RCX			;
    add RBP,RCX			;beginning 
    sub RAX,RAX			;zero out sum
    add RSP,32			;set ESP to top
  add_loop:
      sub RBP,8			;update EBP
      mov RDX,[RBP]		;second parameter
      add RDX,[RBP+8]		;add them together
      add RAX,RDX		;add to sum
      cmp RBP,RSP		;if end of parameters
      jne and_loop		;then end
    sub RSP,32			;
    pop RDX 			;restore EDX
    add RSP,24			;clearing parameters
    add RSP,RCX			;clear all parameters
    ret 			;clear stack again for processing
```
when I compile with nasm it compiles fine, but when I try to link it with ld i get this:

```
$ ld -s -o practice practice.o
ld: warning: i386 architecture of input file `practice.o' is incompatible with i386:x86-64 output
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
```
running it under root does not help and I've tried using both USE64 and BITS64 in the file to no avail-ive heard the cannot find entry symbol _start is just a symptom of the i386 architecture error...can anyone help me with this?
thank you!


----------



## trasz@ (Mar 22, 2010)

Make sure you use nasm properly.  You need relatively recent version (at least 2.0, afaik) and you need to specify the format, like this:

`nasm -f elf64 -o practice.o practice.s`


----------



## mf93 (Mar 23, 2010)

alright thx that solved that problem except now I'm compiling and running it and even with this program

```
global _start
section .stack 

section .text

_start:
    mov rax,45h		;put 45h in eax
    push rax		;pass eax as parameter
```
it compiles and links fine, but returns a segfault when I try to run it...What am I doing wrong?


----------



## mf93 (Mar 23, 2010)

never mind I fixed it i forgot to add the exit interrupt:

```
mov rbx,0
mov rax,1
int 0x80
```
now it works. Thank you!


----------

