flat assembler
Message board for the users of flat assembler.

Index > Main > 64-bits registers in 32-bits mode

Author
Thread Post new topic Reply to topic
cane



Joined: 27 Oct 2005
Posts: 7
Location: Italy
cane
Ok, maye this is the stupidest question ever... Wink

I was wondering: would it be possible to use 64-bits general registers (e.g.: extended 32-bits ones and r8,...,r15) while programming a function or module or whatever for a 32-bits OS? I mean: I don't need 64-bits memory access, 64-bits IP, etc... but only 64-bits registers for storing stuff.
Post 09 Nov 2005, 14:28
View user's profile Send private message Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen
It is simply impossible. 32-bit processor doesn't know these registers. It means the processor can't decode them correctly.
Post 09 Nov 2005, 15:14
View user's profile Send private message Visit poster's website Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
are you on a 32-bit processor.. or a 32-bit OS... if your processor is 64-bit, but your OS is 32, you can use the 64-bit registers/instructions fine, but if the processor is 32-bit you cannot

_________________
redghost.ca
Post 09 Nov 2005, 15:15
View user's profile Send private message AIM Address MSN Messenger Reply with quote
cane



Joined: 27 Oct 2005
Posts: 7
Location: Italy
cane
Let me clear things: I have a 64-bits processor and a 32-bits OS Smile
The question was related to this kind of scenario.
Post 09 Nov 2005, 15:20
View user's profile Send private message Reply with quote
cane



Joined: 27 Oct 2005
Posts: 7
Location: Italy
cane
RedGhost wrote:
if your processor is 64-bit, but your OS is 32, you can use the 64-bit registers/instructions fine


ehm... how? Thanks!
Post 09 Nov 2005, 15:25
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
it isn't like in 16bit mode with 32bit regs? eg.
Code:
db 66h
mov ax,lower_word
dw upper_word
etc.
    
Post 09 Nov 2005, 20:48
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Kain



Joined: 26 Oct 2003
Posts: 108
Kain
RedGhost wrote:
are you on a 32-bit processor.. or a 32-bit OS... if your processor is 64-bit, but your OS is 32, you can use the 64-bit registers/instructions fine, but if the processor is 32-bit you cannot


Really? I haven't been able to do this from Windows (32 bits WinXP running on 64bit AMD).

_________________
:sevag.k
Post 09 Nov 2005, 22:42
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7801
Location: Kraków, Poland
Tomasz Grysztar
No, on AMD64 the 64-bit registers and instructions are available only in long mode, they are not recognized in legacy and compatibility modes (since the REX prefixes used for those extensions use the 40h-4Fh opcodes range, and in compatibility modes they have old meaning, the short forms of INC/DEC instructions).
Post 09 Nov 2005, 22:54
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
do the 64bit instruction have prefix in long mode? that would be stupid slowdown to everything.
Post 09 Nov 2005, 23:24
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Quote:
do the 64bit instruction have prefix in long mode?
Yes, but I don't think there is much of a slowdown (caches help here). Indeed using 64bit registers in many cases will speed up data processing. One minor caveat is 64bit pointer manipulation. If you have many 64bit pointers saving and restoring etc. means twice as much memory access.
Post 10 Nov 2005, 00:52
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Code:
org 100h

use64

        mov     rax, rax
        mov     eax, eax
        int     20h    
C:\DOCUME~1\Hernan\ESCRIT~1>debug amd64.com
-u
0D18:0100 48 DEC AX
0D18:0101 89C0 MOV AX,AX
0D18:0103 89C0 MOV AX,AX
0D18:0105 CD20 INT 20

Apparently you need the REX byte to access long registers and I think for the other things too.

http://www.sandpile.org/aa64/opc_rex.htm
Post 10 Nov 2005, 00:56
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
i think one extra byte per instruction causes quite big slowdown with internal processor caches
Post 10 Nov 2005, 01:59
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
Quote:
i think one extra byte per instruction causes quite big slowdown with internal processor caches
But not every instruction requires a REX prefix, only those that use the 64 bit registers (and XMM8-XMM15). Also consider this:
Code:
add rax,rbx ;64 bit add, one instruction, compare to ...

add eax,ebx ;32 bit add and...
adc edx,ecx ;another 32 bit add to complete the 64 bit addition    
This is why data processing can be faster even with a REX prefix the 64 bit code is shorter overall.
Post 10 Nov 2005, 03:17
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7801
Location: Kraków, Poland
Tomasz Grysztar
Note also that you don't really need, for example, to do:
Code:
mov rax,1    

since
Code:
mov eax,1    

does the same, as all operations on 32-bit registers zero-extend the result to the whole 64-bit register. This is directly related to the fact, that 32-bit is the default size of operations (when no prefix provided) in the long mode. And thus general rule is that you should use 32-bit operations everywhere where possible (well, in fact many of the 32-bit code should be directly portable into long mode, and even instruction encodings don't change at all).
Post 10 Nov 2005, 11:16
View user's profile Send private message Visit poster's website Reply with quote
cane



Joined: 27 Oct 2005
Posts: 7
Location: Italy
cane
Thanks for helping me out! Now, to summarize things up: the only way to use 64-bits registers is to turn on long mode (use64 directive) which, though, prevents the code from running on a 32-bits OS. Is that correct?
Post 10 Nov 2005, 11:26
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
usa64 directive doesn't "turn on the mode", it just tells assembler to assemble code that can be run in long mode. OS must run your code in long mode.
Post 10 Nov 2005, 12:36
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
cane



Joined: 27 Oct 2005
Posts: 7
Location: Italy
cane
Yeah I meant that... but once I get the 64-bit assembled code there's no hope a 32-bit OS will run it.
Post 10 Nov 2005, 12:45
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
in reality no. in theory you could make "64bit" code which will use only instructions that are same as in 32bit mode, and such one would be runable
Post 10 Nov 2005, 13:36
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen
This thread could be also interesting in this topic.
Post 10 Nov 2005, 15:44
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.