The MASM Forum Archive 2004 to 2012

Project Support Forums => 64 Bit Assembler => Topic started by: MazeGen on April 18, 2008, 11:30:44 AM

Title: Some fun with portable asm code
Post by: MazeGen on April 18, 2008, 11:30:44 AM
Long time ago, I was seriously thinking about portable asm code between x86-32 and x86-64. I never realized it though. However, my thoughts are now written down in this article:

http://x86asm.net/articles/portable-x86-flat-syntax/

Small demo (MASM) is included. Have fun :)
Title: Re: Some fun with portable asm code
Post by: jj2007 on April 18, 2008, 12:02:00 PM
Interesting. I stumbled over this statement:

The exception is PUSH const, which always default to current stack width using zero extension

Given that ordinary code contains plenty of memory accesses, not only as push/pop, what are the implications for speed? Is 64-bit code half as fast??  :eek
Title: Re: Some fun with portable asm code
Post by: MazeGen on April 18, 2008, 03:06:48 PM
I don't know if I got you right, you meant if there is any penaulty because you always need to PUSH 64-bit operand instead of 32-bit one?
Title: Re: Some fun with portable asm code
Post by: jj2007 on April 18, 2008, 03:24:08 PM
Yes. After posting, I googled around a bit and found this story (http://www.osnews.com/story/5768/Are_64-bit_Binaries_Really_Slower_than_32-bit_Binaries_/page2/) confirming my suspicion - although there are arguments pro 64-bit, too. In a nutshell:
- If you have a thousand pointers to variables that count "only" from 0 to 2^31, then doing all these memory accesses with a 64-bit pointer might slow down your code;
- If you manage to make efficient use of the extra registers, you may speed it up.
Of course, even in extreme cases it will never been half as fast... ;-)
Title: Re: Some fun with portable asm code
Post by: Randall Hyde on June 25, 2008, 03:14:22 AM
Quote from: MazeGen on April 18, 2008, 11:30:44 AM
Long time ago, I was seriously thinking about portable asm code between x86-32 and x86-64. I never realized it though. However, my thoughts are now written down in this article:

http://x86asm.net/articles/portable-x86-flat-syntax/

Small demo (MASM) is included. Have fun :)


Since the x64-based OSes all support 32-bit compatibility mode, I'm not sure why anyone would care to have the same assembly code run in 32-bit and 64-bit mode. Indeed, running "only 32-bit code" in 64-bit mode (compiled properly) tends to run slower. The real benefit of 64-bit mode is realized by having the extra registers available.  If you're not going to take advantage of the extra facilities present in 64-bit mode, it makes a lot of sense to compile for 32-bits and run your code in compatibility mode.
Cheers,
Randy Hyde

Title: Re: Some fun with portable asm code
Post by: Mark Jones on June 25, 2008, 11:41:13 PM
Also, from my limited experience, Win64 executables impose a much larger memory footprint from the OS -- perhaps by a factor of 2-3x in practice. So a program written in 32-bit may use 10MB of ram, but the 64-bit equivalent uses 30MB. I was shocked to see how much RAM Win XP x64 used on inital install. A nearly bare-bones WinXP dev system on x32 may use 175MB RAM, and a bare-bones x64 version uses 450MB! So anything that can be done to reduce the memory footprint of x64 apps would be greatly beneficial.
Title: Re: Some fun with portable asm code
Post by: MazeGen on June 26, 2008, 02:08:56 PM
Quote from: Randall Hyde on June 25, 2008, 03:14:22 AM
Since the x64-based OSes all support 32-bit compatibility mode, I'm not sure why anyone would care to have the same assembly code run in 32-bit and 64-bit mode. Indeed, running "only 32-bit code" in 64-bit mode (compiled properly) tends to run slower. The real benefit of 64-bit mode is realized by having the extra registers available.  If you're not going to take advantage of the extra facilities present in 64-bit mode, it makes a lot of sense to compile for 32-bits and run your code in compatibility mode.
Cheers,
Randy Hyde

Hi Randy, it is mostly for fun. I've done some research long time ago and I just wanted to publish it. As I wrote in the lead paragraph, it is weird idea after all.

On the other side, there can be an edge case where you need to assemble the same code for both modes. For example, this can happen if you need to make a project, which must be written in asm and which must run in both 32-bit protected mode and 64-bit mode. It can be more complicated to maintain two similar asm projects for both modes.