Author Topic: Can't refer to 64 bit regs in 32 bit code, correct?  (Read 20317 times)

Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Can't refer to 64 bit regs in 32 bit code, correct?
« 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.
« Last Edit: July 12, 2013, 08:09:02 AM by dogman »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #1 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


Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #2 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)
« Last Edit: July 12, 2013, 12:56:02 PM by dogman »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #3 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


Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #4 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.

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #5 on: July 16, 2013, 12:30:31 AM »
Unless you're developing apps which will run on the new X32 ABI standard where you can make use of 64-bit registers in a 32-bit app!  ;D

Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #6 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?

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #7 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 to see how far you get.  There still remains rework in the entire tool-chain.

Offline dogman

  • Jr. Member
  • *
  • Posts: 51
Re: Can't refer to 64 bit regs in 32 bit code, correct?
« Reply #8 on: July 17, 2013, 05:20:17 PM »
Thank you.