The MASM Forum

General => The Campus => Topic started by: tda0626 on June 30, 2024, 12:09:03 AM

Title: Using 64bit Registers in 32bit
Post by: tda0626 on June 30, 2024, 12:09:03 AM
I am going to convert my 16bit random number generator to a 64 bit version and then use it in a 32bit application. Are there any special rules that I need to know about using 64 bit registers or can I use the general purpose registers like their 32 bit counterparts?


Tim
Title: Re: Using 64bit Registers in 32bit
Post by: zedd on June 30, 2024, 12:23:04 AM
Quote from: tda0626 on June 30, 2024, 12:09:03 AMI am going to convert my 16bit random number generator to a 64 bit version and then use it in a 32bit application.
Would ml.exe even know that rax, rcx, rdx, rbx, r8, r9, r10, etc. are (64 bit) registers? Offhand I would think not. I think that ml would assume that they are undefined variables.

a bit later...
I have made a small test as an example. The 64 bit registers chosen was purely arbitrary and not chosen for any particular reason other than showing that it won't work.
    include \masm32\include\masm32rt.inc

    .data


    .code
    start:
        mov rax, 1
        mov rcx, rsi
        invoke ExitProcess, 0

    end start
(https://i.postimg.cc/Fsw9fJVL/untitled.png)

Quote from: tda0626 on June 30, 2024, 12:09:03 AMor can I use the general purpose registers like their 32 bit counterparts?
As shown above, sadly no.
Title: Re: Using 64bit Registers in 32bit
Post by: jj2007 on June 30, 2024, 12:23:56 AM
You can't use rax or rdx in a 32-bit application. Some instructions, like mul and div, work with the edx:eax pair.

You can, however, use SIMD instructions in 32-bit code.
Title: Re: Using 64bit Registers in 32bit
Post by: Mikl__ on June 30, 2024, 01:17:59 AM
Ciao Jochen! Hi zedd151!
tda0626
You cannot use rax or rdx in a 32-bit application. But it's not very difficult to write a 64-bit application and use all 16 general purpose registers. For general development it is also useful to forget about 32-bit applications and start learning 64-bit ones
Title: Re: Using 64bit Registers in 32bit
Post by: FORTRANS on June 30, 2024, 01:28:10 AM
Hi,

   For something simple, like a random number generator,
you could use the FPU.  With the integer load and store
instructions, it should be easy to implement.  Just make
sure the integer results of a calculation are properly
scaled.

Cheers,

Steve N.
Title: Re: Using 64bit Registers in 32bit
Post by: daydreamer on June 30, 2024, 01:35:38 AM
Quote from: jj2007 on June 30, 2024, 12:23:56 AMYou can't use rax or rdx in a 32-bit application. Some instructions, like mul and div, work with the edx:eax pair.

You can, however, use SIMD instructions in 32-bit code.
Uncertain if left shift and right shift working on 32 bit reg + 32 bit memory can use two 32 bit regs
64 bit left or right can be used in sse2 instructions, 128 bit left or right shift is shifting bytes not bits
My 128 bit SIMD sse2 version of 16 bit random generator,are in fact 4 different 32 bit random generators,same alto but different seeds
But sse2 isn't true full fledged 64 bit instruction set,its crippled to few 64 bit instructions,its specialised for speed up 8 bit,16 bit,32 bit running many in parallel 4x32 bit,8x16 bit,16 x 8 bit fit in 128 bit xmm regs
Different animal than use masm64
Title: Re: Using 64bit Registers in 32bit
Post by: C3 on June 30, 2024, 01:37:00 AM
Quote from: tda0626 on June 30, 2024, 12:09:03 AMI am going to convert my 16bit random number generator to a 64 bit version and then use it in a 32bit application. Are there any special rules that I need to know about using 64 bit registers or can I use the general purpose registers like their 32 bit counterparts?


Tim

Hi!

Do you know: RDRAND — Read Random Number Instruction? I was wondering why you do something what is already ready for you use easily?

QuoteDescription from Intel Manual:
Loads a hardware generated random value and store it in the destination register. The size of the random value is
determined by the destination register size and operating mode.
Title: Re: Using 64bit Registers in 32bit
Post by: zedd on June 30, 2024, 02:42:30 AM
Quote from: C3 on June 30, 2024, 01:37:00 AMDo you know: RDRAND — Read Random Number Instruction? I was wondering why you do something what is already ready for you use easily?
One issue that I can foresee in using RDRAND is that not all processors have it implemented.
Title: Re: Using 64bit Registers in 32bit
Post by: tda0626 on June 30, 2024, 02:58:49 AM
Thanks for the info. I am looking for a way to pass a seed value to my random number generator, XOR shift, and noticed the Windows API returns 64 bit values like the QueryInterruptTime function. I guess I could truncate the value to 32 bit. In 16 bit, I queried the timer interrupt to seed the function. Is there a similar way to do that?



Quote from: C3 on June 30, 2024, 01:37:00 AMHi!

Do you know: RDRAND — Read Random Number Instruction? I was wondering why you do something what is already ready for you use easily?

QuoteDescription from Intel Manual:
Loads a hardware generated random value and store it in the destination register. The size of the random value is
determined by the destination register size and operating mode.


I did not know about that and good to know. Since this was implemented in 2012 and 2015 with AMD processors, is there a similar method that would be used for processors predating 2012?


Quote from: FORTRANS on June 30, 2024, 01:28:10 AMHi,

  For something simple, like a random number generator,
you could use the FPU.  With the integer load and store
instructions, it should be easy to implement.  Just make
sure the integer results of a calculation are properly
scaled.

Cheers,

Steve N.

I was hoping to avoid using the FPU if possible. I haven't messed with it before but it might be time to learn it. Can you use bitwise operations on the FPU or would I have to transfer to a x86 register?









 
Title: Re: Using 64bit Registers in 32bit
Post by: C3 on June 30, 2024, 04:31:03 AM
QuoteAll Intel processors that support the RDRAND instruction indicate the availability of the RDRAND instruction via reporting CPUID.01H:ECX.RDRAND[bit 30] = 1.

You need put 01h to eax and use cpuid, then you can check ecx bit 30 if its supported. Can't remember but CPUID was already in 386? Processors pre that used FLAGS and some instructions to detect older CPU version. It's all told in the Intel® 64 and IA-32 Architectures Software Developer's Manual get that from Intel Website. And yes, you are using AMD, there maybe similar manual from AMD available.
Title: Re: Using 64bit Registers in 32bit
Post by: jj2007 on June 30, 2024, 05:14:57 AM
Quote from: tda0626 on June 30, 2024, 02:58:49 AMI was hoping to avoid using the FPU if possible. I haven't messed with it before but it might be time to learn it. Can you use bitwise operations on the FPU or would I have to transfer to a x86 register?

The FPU is as fast as the scalar SIMD instructions. No bitwise instructions, though.
Title: Re: Using 64bit Registers in 32bit
Post by: NoCforMe on July 01, 2024, 08:14:35 AM
Quote from: tda0626 on June 30, 2024, 02:58:49 AMI was hoping to avoid using the FPU if possible. I haven't messed with it before but it might be time to learn it.
I think you ought to learn how to use it. It's pretty damn useful.

It's also implemented pretty strangely, kind of like using some ancient technology. But once you master the quirks you can do a lot with it.

In the meantime you can use Raymond Filiatreault's very useful FPU library, which gives you all the FPU functionality without actually coding any FPU instructions. It's a lot slower, but it does do the job. (It's all in \masm32\fpulib.)
Title: Re: Using 64bit Registers in 32bit
Post by: daydreamer on July 09, 2024, 03:13:46 PM
Quote from: jj2007 on June 30, 2024, 05:14:57 AM
Quote from: tda0626 on June 30, 2024, 02:58:49 AMI was hoping to avoid using the FPU if possible. I haven't messed with it before but it might be time to learn it. Can you use bitwise operations on the FPU or would I have to transfer to a x86 register?

The FPU is as fast as the scalar SIMD instructions. No bitwise instructions, though.
You aren't restricted to only floats/doubles if  you use scalar sse2  instructions ,use real8 and convert to integer ,after that you can use pxor,land,por ,64 bit left and right bitwise shift