64-bit op codes in 32-bit OS

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

64-bit op codes in 32-bit OS

Post by fzabkar »

If you are using the 32-bit FB compiler running under 32-bit Windows 10 on an x64 processor, can you use 64-bit op codes in ASM?

Does the processor run in "32-bit mode", or is it OS agnostic? I mean, does the OS switch the processor from "64-bit mode" to "32-bit mode", assuming there is such a thing?
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: 64-bit op codes in 32-bit OS

Post by Vortex »

If you are using the 32-bit FB compiler running under 32-bit Windows 10 on an x64 processor, can you use 64-bit op codes in ASM?
Hello,

You cannot run 64-bit applications on a 32-bit Windows 10\8\7.
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: 64-bit op codes in 32-bit OS

Post by SARG »

You can't as far I know.
For example the 32bit assembler doesn't know 64bit registers (rax, etc).

Post a short piece of code showing what you want to do.

A 32bit executable started on 64bit OS is recognized as 32bit and is running inside a sort of 'emulator' for all the system calls.

What is your goal ?

Anyway my advice is : Keep things simple.
If running only on 32bit compile to 32bit.
If running only on 64bit compile to 64bit.
If running on 32bit and 64bit compile to 32bit.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

I'm trying to reverse the endianness of a 64-bit qword.

This is 32-bit code:

Code: Select all

Function EndianRev64( ByVal num As ULongInt ) As ULongInt
    
	ASM
        
	  mov eax, [num]
          mov edx, [num + 4]
	  bswap eax
          bswap edx
	  mov [Function + 4], eax
          mov [Function], edx
      
	End ASM
    
'    Return num
    
End Function

Print Hex( EndianRev64(&H1122334455667788) )

sleep
I was hoping I could use this:

Code: Select all

Function EndianRev64( ByVal num As ULongInt ) As ULongInt
    
	ASM
        
	  mov rax, [num]
	  bswap rax
	  mov [Function], rax
      
	End ASM
    
End Function
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: 64-bit op codes in 32-bit OS

Post by marcov »

pshufb ? https://www.felixcloutier.com/x86/pshufb.html

Requires some alignment here and there though.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

I understand that x86 processors have real and protected modes. I'm asking if they also have 32-bit and 64-bit modes. If the answer is yes, then would a processor running in 32-bit mode raise an exception if it were to encounter a 64-bit op code?

Does the compiler have a directive which would enable me to say, "yes, I know you're a 32-bit compiler, and I know that the OS is 32-bit, but I have a 64-bit processor and I would really like to use a 64-bit op code"?

Alternatively, just as a thought experiment that would help me to understand the inner workings, what if I were to compile a 32-bit EXE including a 32-bit ASM, and then hack the binary ASM routine to replace 32-bit op codes with 64-bit RAX op codes? Windows would still see a 32-bit EXE, but will the code execute without error?
marcov
Posts: 3454
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: 64-bit op codes in 32-bit OS

Post by marcov »

fzabkar wrote: Jun 12, 2022 21:17 I understand that x86 processors have real and protected modes. I'm asking if they also have 32-bit and 64-bit modes. If the answer is yes, then would a processor running in 32-bit mode raise an exception if it were to encounter a 64-bit op code?
Not just that. The same instruction might be interpreted differently, namely using the default register size for that mode. So the opcode is not necessarily different.

Alternatively, just as a thought experiment that would help me to understand the inner workings, what if I were to compile a 32-bit EXE including a 32-bit ASM, and then hack the binary ASM routine to replace 32-bit op codes with 64-bit RAX op codes? Windows would still see a 32-bit EXE, but will the code execute without error?
Just get both a 32-bit and 64-bit compiler and familiarize yourself with "objdump" to see generated assembler annotated with the byte sequences.
SARG
Posts: 1755
Joined: May 27, 2005 7:15
Location: FRANCE

Re: 64-bit op codes in 32-bit OS

Post by SARG »

Unless you need to call the function million of times the difference of execution time is not visible.

You can use __FB_64BIT__ to get an executable according the OS of destination, obviously when compiling not when running.

#ifdef __FB_64BIT__
'...instructions for 64bit OSes...
#else
'...instructions for other OSes
#endif
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

SARG wrote: Jun 12, 2022 21:46 Unless you need to call the function million of times the difference of execution time is not visible.
I have to test whether the data pattern in each sector of a 1TB drive is equal to its big-endian LBA. The program will be I/O bound, so maybe the percentage difference in execution time won't be significant.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

I tried this code but it crashes with no output. I have a Core 2 Duo, so I guess it's too old.

Code: Select all

Function BytSwap8( Byval qword As uLongInt ) As uLongInt

    Dim Swap8Mask As Const uLongInt = &H0607040502030001
    
    ASM
        movups xmm1, [Swap8Mask]
        movups xmm0, [qword]
        pshufb xmm0, xmm1	        ' Swap8Mask
        movups [Function], xmm0
    
    End ASM

End Function

Print Hex( BytSwap8(&H1122334455667788) )
Sleep
Edit:

Could it be an alignment problem? If so, how do I deal with it?

I tried "mov mm0" op codes, but the compiler rejected them.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: 64-bit op codes in 32-bit OS

Post by srvaldez »

xmm# are 128 bit ( oword ) and you are trying to use them as 64-bit, perhaps if you use an array you could make it work
it's not an alignment problem because movups deals with unaligned memory
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

This compiles without error but still hangs during execution.

Code: Select all

Function BytSwap8( Byval qword As uLongInt ) As uLongInt

    Dim Swap8Mask As Const uLongInt = &H0607040502030001
    
    ASM
        movq mm1, [Swap8Mask]
        movq mm0, [qword]
        pshufb mm0, mm1	        ' Swap8Mask
        movq [Function], mm0
    
    End ASM

End Function

Print Hex( BytSwap8(&H1122334455667788) )

sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

My code now executes properly, after copying the "qword" argument into a new variable, "qword2". I don't understand why this should be so.

Code: Select all

Function EndianRev64( Byval qword As uLongInt ) As uLongInt

    Dim Swap8Mask As Const uLongInt = &H0001020304050607
    
    Dim qword2 As uLongInt    
    qword2 = qword
    
    ASM
        movq mm1, [Swap8Mask]
        movq mm0, [qword2]      ' <- this op code causes hanging on execution
        pshufb mm0, mm1	        ' Swap8Mask
        movq [Function], mm0
    
    End ASM
 
End Function

Print Hex( EndianRev64(&H1122334455667788) )
Sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

This compiles OK, but hangs on execution. I'm stumped.

Code: Select all

Dim Swap8Mask As Const uLongInt = &H0001020304050607
Dim qword As uLongInt
Dim qwresult As uLongInt

qword = &H8877665544332211

ASM
    movq mm1, [Swap8Mask]
    movq mm0, [qword]       ' <- this op code causes hanging on execution
    pshufb mm0, mm1	        ' Swap8Mask
    movq [qwresult], mm0

End ASM

Print Hex( qwresult )
Sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: 64-bit op codes in 32-bit OS

Post by fzabkar »

This works:

Code: Select all

Dim Swap8Mask As Const uLongInt = &H0001020304050607
Dim qword1 As uLongInt
Dim qwresult As uLongInt

qword1 = &H8877665544332211

ASM
    movq mm1, [Swap8Mask]
    movq mm0, [qword1]       ' <- this line causes hanging on execution if variable name is "qword"
    pshufb mm0, mm1	        ' Swap8Mask
    movq [qwresult], mm0

End ASM

Print Hex( qwresult )
Sleep
It appears that "qword" is an illegal variable name, probably because it is reserved.

Doh!
Post Reply