Author Topic: 64 bit linking in Windows  (Read 22764 times)

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
64 bit linking in Windows
« on: June 25, 2014, 05:01:38 AM »
Hi, I was trying to link a my nasm program in windows and i used this command to compile
Code: [Select]
nasm -f elf64 Asm.asm that worked and then when I tried to link it like so
Code: [Select]
  ld -m elf_x86_64 -s -o Asm.exe Asm.o    it would give this error
Code: [Select]
ld unrecognized emulation mode: elf_x86_64
supported emulations: i386pe
and yes I do have a 64 bit system
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 64 bit linking in Windows
« Reply #1 on: June 25, 2014, 06:05:33 AM »
Try "-f win64" instead of "-f elf64". Elf is for Linux.

Best,
Frank


Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 64 bit linking in Windows
« Reply #2 on: June 25, 2014, 06:30:31 PM »
No it is still giving me the same error
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 64 bit linking in Windows
« Reply #3 on: June 25, 2014, 08:26:30 PM »
That's probably a sign that I should shut up and let someone who knows something about Windows try to help you. But I can't resist guessing. :)

Still linking like?
Code: [Select]
  ld -m elf_x86_64 -s -o Asm.exe Asm.o
Anything with "elf" in it is probably wrong there, too. What happens if you try to link it with no "-m" switch at all? As I recall, the default output name from Nasm -f win64 will be "Asm.obj" rather than "Asm.o" - you'll probably want to change that, too.

If that doesn't help... maybe want to wait until someone who knows Windows comes along...

Courage!

Best,
Frank



Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 64 bit linking in Windows
« Reply #4 on: June 25, 2014, 09:41:10 PM »
Ok well I have done it with  "win64" ,and now I am using golink because my ld is only 32 bit for some odd reason and I cannot find another ld that is,  and I had to remove
Code: [Select]
global _start: because it gives me this error:
 
Code: [Select]
Asm.asm:6: error: COFF format does not support any special symbol types and when I removed the global start and I linked it with this
Code: [Select]
golink /console Asm.obj and it gave me this error [code]
Warning!
Assumed entry point (Start) was not found.
Output file: Asm.exe
Format: X64   Size: 1,536 bytes
and when I tried to run the Asm.exe file it crashed.
Thanks in advance, Anonymous

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 64 bit linking in Windows
« Reply #5 on: June 25, 2014, 10:06:11 PM »
Now its fixed I just had to change _start: to Start but the program isn't working is it because I am using different system calls? How would I go about translating this code from linux to windows nasm :

Code: [Select]
section .text

%include "functions.asm"


Start:
mov qword[x_number], "-90"
mov r8, x_number
call atoi

mov qword[x_number], rax
mov r8, qword[x_number]
call itoa

mov rax, 60
mov rdi, 0
syscall

ret



Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: 64 bit linking in Windows
« Reply #6 on: June 25, 2014, 10:56:42 PM »
Code: [Select]
Asm.asm:6: error: COFF format does not support any special symbol types
Well, that's odd! I don't see any reason why Nasm should think that's a "special symbol". "..start" is a special symbol - known only to "-f obj" - and Nasm will complain about that in other output formats (but the error message is slightly different). It looks like golink is expecting "Start" - note uppercase 'S' and no underscore. You will want to declare it "global", and will probably want a ':' after it (the label itself, not the "global" declaration). The warning Nasm will generate with no ':' can be ignored (but ignoring warnings is a bad habit). The warning can be disabled, too - easier to just put a ':' after it.

There are a lot of reasons (probably infinite, but I'm not done counting) why your program can crash, even if it assembles and links with no errors. We will probably need to see more code to diagnose that. Once you've got an executable, you can step through it in a debugger to find the problem, so you're making progress!

(later) Ah, yes that's Linux code. I think Windows would exit with:
Code: [Select]
    mov rdi, 0
    call ExitProcess
?

But you've probably got other Linux system calls hidden away in your "functions.asm"(?). I think you're going to need better advice than mine to translate this.

Best,
Frank


Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 64 bit linking in Windows
« Reply #7 on: June 25, 2014, 11:41:15 PM »
Well here is my code in functions.asm for anyone that knows how to convert to windows nasm code
Code: [Select]

section .data

MSG: db "Input number : ",10
MSG_L: equ $ - MSG
negsign: dq '-'
negsign_L: equ  $ - negsign
section .bss
x_number resq 255
 
section .text

atoi:
    push     rbx;if you are going to use rbx you must preserve it by pushing it onto the stack
    ;~ Address is passed in rdi
    xor r9, r9
    mov     rbx, 10 ; to multiply by
    xor     rax, rax; to use as "result so far"
    xor     rcx, rcx ; our character/digit (high bits zero)
    cmp byte [r8], '-'
    jne  .top
   
    add r8, 1
    add r9, 1
   
   
.top:
    mov     cl, byte [r8] ; get a character
    add     r8, 1  ; get ready for the next one
    cmp     cl, 0 ; end of string?
    je      .done
    cmp     cl, '0'
    jb      .invalid
    cmp     cl, '9'
    ja      .invalid
    sub     cl, '0' ; or 48 or 30h
    ; now that we know we have a valid digit...
    ; multiply "result so far" by 10
    mul     rbx
    jc      .overflow ; ?
    ; and add in the new digit
    add     rax, rcx
    jmp     .top
    ; I'm not going to do anything different for overflow or invalid
    ; just return what we've got
.overflow:
.invalid:
.done:
    cmp r9, 1
    jne .done2
   
    neg rax
    .done2:
    pop     rbx;restore rbx to its original value
    ret ; number is in rax 
itoa:
jmp beggining ; go to the beggining of the function

negate:
; print out a negative sign
mov     rdx, negsign_L;length
mov     rsi, negsign;variable
mov     rdi,1;sys_write
mov     rax, 1;stdout
syscall

neg r8
mov rax, r8
inc rcx

jmp godivide
beggining:
push rbx
;number is passed in through r8
mov rbx, 10
xor rcx, rcx
cmp r8, 0
jl negate
 
mov rax, r8;move number into rax

godivide:
xor rdx, rdx ; clear remainder
div rbx ; divide rax by 10
push rdx ; put rdx on stack
inc rcx ; increment number of digits
cmp rax, 0 ; if rax
jne godivide ; is not equal to zero, divide again

nextdigit:
pop rax ;pull off the stack
add rax, 30h ;convert to ASCII
push rcx ; store counter

mov rdx, 1 ; one byte




push rax
mov rsi, rsp

mov     rdi,1;sys_write
mov     rax, 1;stdout
syscall

pop rax
;;
pop rcx ; restore counter
dec rcx ; get ready for the next one
cmp rcx, 0 ; if rcx
jg nextdigit ; is greater than 0, jump nextdigit


pop rbx
ret



thanks for the help anyway frank
« Last Edit: June 25, 2014, 11:44:43 PM by Anonymous »
Thanks in advance, Anonymous

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: 64 bit linking in Windows
« Reply #8 on: June 26, 2014, 10:28:58 AM »
Do you have switched from Linux to Windows though your linking problems? Say hello to Windows and your new problems. ;)

I think you'll need a windows programming tutorial, there are many out there but I'am sorry I don't know one that's based on NASM.
Please comment your code! It helps to help you.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: 64 bit linking in Windows
« Reply #9 on: June 27, 2014, 08:15:36 PM »
Ya I have not found a nasm tutorial for windows but I guess I will just use a virtual machine to run linux and do assembly I had to switch to windows because my linux machine broke.
Thanks in advance, Anonymous