NASM - The Netwide Assembler

NASM Forum => Other Discussion => Topic started by: dogman on July 12, 2013, 08:05:54 AM

Title: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: dogman on July 12, 2013, 08:05:54 AM
Can anybody confirm it's illegal to access 64 bit regs in 32 bit code?

Nasm gives me an error for something like

mov 1, rax

Is the converse also true? Is it not ok to do

mov 1, eax

in 64 bit code or does that work ok since you can refer to ax in 32 bit code?

Thanks.

Does this stupid key captcha thing go away after x number of posts? It takes me like 10 minutes to get one correct.
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: Frank Kotler on July 12, 2013, 11:40:12 AM
Correct, you can't use 64-bit registers in 32 bit code.

In 64-bit code, you can do:
Code: [Select]
mov eax, 1
but not:
Code: [Select]
mov 1, eax
Intel syntax goes dest<-src, not src->dest. One point against HLA! :)

You also can't push a 32-bit register in 64-bit code - the stack is 64-bit.

I apologize for the "guess the puzzle" games. I think it goes away after a while, but I don't know when. I was signed up before that (expletive deleted) was instituted... or I wouldn't be here!

Best,
Frank

Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: dogman on July 12, 2013, 11:49:21 AM
Thanks Frank. Hah, yeah, I'm totally bamboozled by the operand order. It's not because of HLA btw. So many platforms, so many variations! Sorry for my bad example and thanks for understanding it anyway.



Edit: if you encoded generate something like mov rax, 1 in a 32 bit piece of code what signal would Linux give and what hardware error is it?
Arrrg I did it again! (fixed)
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: Frank Kotler on July 12, 2013, 02:29:15 PM
True, it isn't a(n) "HLA problem". We don't have an "assembly language standards committee", so everybody can invent their own syntax... and we do! Nice to have the flexibility, I guess, but it's a PITA in a way, too!

As for "mov rax, 1" in  32-bit code... I had to try it. Nasm won't let you do it, but we can stuff in "db"s to get the same effect. Turns out that "mov rax, 1" is encoded as "mov eax, 1" (!) - upper bits of rax are cleared when moving to eax, and this is an "optimization", I guess. Using a bigger number to force "rax"... Linux says "Segmentation fault". I suspect the actual hardware exception is "invalid opcode", but I'm not certain of that.

Best,
Frank

Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: dogman on July 14, 2013, 09:13:22 AM
As for "mov rax, 1" in  32-bit code... I had to try it. Nasm won't let you do it, but we can stuff in "db"s to get the same effect. Turns out that "mov rax, 1" is encoded as "mov eax, 1" (!)

Haha so my example was correct after all. What a code psychic  :P

Thanks.
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: Rob Neff on July 16, 2013, 12:30:31 AM
Unless you're developing apps which will run on the new X32 ABI (http://en.wikipedia.org/wiki/X32_ABI) standard where you can make use of 64-bit registers in a 32-bit app!  ;D
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: dogman on July 17, 2013, 09:26:48 AM
That's it! Thanks Rob. I forgot the name. Is that implemented anywhere? I mean the wiki page says it's in the Linux kernel at some point but is any distro running with support for it?
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: Rob Neff on July 17, 2013, 04:34:41 PM
Not to my knowledge - although you can try the link contain in http://wiki.debian.org/X32Port (http://wiki.debian.org/X32Port) to see how far you get.  There still remains rework in the entire tool-chain.
Title: Re: Can't refer to 64 bit regs in 32 bit code, correct?
Post by: dogman on July 17, 2013, 05:20:17 PM
Thank you.