NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Snake38 on July 29, 2014, 06:44:57 PM

Title: [Win] Mixing 32/64 bit code
Post by: Snake38 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?
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 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 }.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 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.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 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.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 on July 29, 2014, 07:52:25 PM
No, I use 64 bit.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous on July 29, 2014, 07:52:53 PM
Then it should work with the 64 bit executable
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 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.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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
Title: Re: [Win] Mixing 32/64 bit code
Post by: Frank Kotler 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

Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 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?
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous 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
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 on July 29, 2014, 09:38:19 PM
But if you do that it works, you choose elfx32 and it allows you to do so. For windows there is no winx32.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous on July 29, 2014, 09:40:20 PM
I Honestly did not know that I never would actually try it myself using a 64 bit executable on a 64 bit machine seems like the ideal thing to do why then try to trick the compiler into doing something it's not supposed to do anyway???
Title: Re: [Win] Mixing 32/64 bit code
Post by: Snake38 on July 29, 2014, 10:01:16 PM
Nevermind. Frank looks like he have no idea how this works = it's usless and not possible.

Will check for other solution.
Title: Re: [Win] Mixing 32/64 bit code
Post by: Anonymous on July 29, 2014, 10:08:26 PM
Frank did say that you could use inc bin if you really wanted to assemble it as a binary then make a new file and do this
Code: [Select]


incbin "Somefile.bin"


then use nasm -f win32 File.asm