Author Topic: [Win] Mixing 32/64 bit code  (Read 24544 times)

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
[Win] Mixing 32/64 bit code
« on: July 29, 2014, 06:44:57 PM »
Hello, I'm new on forum but not new to assembly.

I'm just curious why NASM don't allow to mix 32/64 bits code? I mean if we write
Code: [Select]
; [...] some 32bit code [...]

bits 64

; [...] some 64bit code [...]
compiling with nasm -f win32 file.asm returns file.asm:3: error: win32 output format does not support 64-bit code.

Other assemblers let us do this and sometimes it's helpful. It's not so hard to change it for yourself but I don't want to change it every new version. Does that makes problems for somebody?

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #1 on: July 29, 2014, 06:57:35 PM »
Well for one you are compiling using win32 if you used win64 maybe that will change things??? I would try that before anything else I use linux so for me I would use elf or elf64 and mixing code works just fine for me I might be wrong but that's how I would do it.
Thanks in advance, Anonymous

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #2 on: July 29, 2014, 07:15:03 PM »
I see in nasm.c:90 is defined global int maxbits = 0;. Some of the output formats seems to be changing this to 64 like (filenames) outbin.c, outcoff.c, outelf64.c, outelf32x.c. In the nasm.c:2099 static int get_bits(char *value); there is the check if passed argument (to the bits statement) is not equal to maxbits and if so return error. So for win32 format giving bits 64 it checks if (64 != 0) { // error: [...] not support 64-bit code }.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #3 on: July 29, 2014, 07:27:06 PM »
Mhmm Then that must mean that doing something like nasm -f Win64 File.asm should work because it has a max bits check so it can have from 8 to 64 bit code.
Thanks in advance, Anonymous

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #4 on: July 29, 2014, 07:31:26 PM »
But then executable is 64bit not 32 and that's what I need. On windows if you have 32bit executable you can run it on 64bit system.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #5 on: July 29, 2014, 07:35:11 PM »
Well thats not always true you can link it using go link here is some information regarding that: http://www.godevtool.com/GolinkHelp/GoLink.htm
Thanks in advance, Anonymous

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #6 on: July 29, 2014, 07:49:54 PM »
Quote
GoLink automatically senses whether it is being given 32-bit or 64-bit input files and produces a 32-bit or 64-bit executable accordingly. No command line switching is needed. Because you cannot mix 32-bit and 64-bit code, GoLink will show an error if you try to mix these input files.

If I compile as win64 and link with GoLink I get 64bit executable.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #7 on: July 29, 2014, 07:50:46 PM »
Oh are you using a 32 bit machine??? If that's the case 64 bit code won't work at all. At least I don't think it's possible
Thanks in advance, Anonymous

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #8 on: July 29, 2014, 07:52:25 PM »
No, I use 64 bit.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #9 on: July 29, 2014, 07:52:53 PM »
Then it should work with the 64 bit executable
Thanks in advance, Anonymous

Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #10 on: July 29, 2014, 07:58:28 PM »
It works with 64 bit executable but that's not my goal.

I want to generate 32 bit executable with some 64 bit code (I can make another file, compile and then embed generated binary data in the first one but that's not the goal). Just one thing is to "force" compiler to accept that theoretically wrong code.

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #11 on: July 29, 2014, 08:01:27 PM »
Oh well I don't think I can help you there I really don't think it is possible but maybe I am wrong
Thanks in advance, Anonymous

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #12 on: July 29, 2014, 08:21:13 PM »
Quote
Other assemblers let us do this...

Use the other assemblers then. I can't imagine that such a thing would possibly work (the thing is either in long mode or it's in compatibility mode). If other assemblers will do this, go for it!

(As you apparently know, "incbin" would let you do this in Nasm, if you really insist...)

Best,
Frank


Offline Snake38

  • Jr. Member
  • *
  • Posts: 9
Re: [Win] Mixing 32/64 bit code
« Reply #13 on: July 29, 2014, 09:25:58 PM »
What you can't imagine? Generating opcodes from 64 bit code and not looking on specified target bits? Just check forked version of NASM.

I use linux so for me I would use elf or elf64 and mixing code works just fine for me I might be wrong but that's how I would do it.

Why then allow this for other platforms?

Offline Anonymous

  • Jr. Member
  • *
  • Posts: 78
  • Country: us
Re: [Win] Mixing 32/64 bit code
« Reply #14 on: July 29, 2014, 09:33:50 PM »
I thought you were talking about generating a 64 bit executable not a 32 bit one when I said that and putting the elf in their was a mistake for mixing code I would only use elf64
« Last Edit: July 29, 2014, 09:36:23 PM by Anonymous »
Thanks in advance, Anonymous