The MASM Forum

64 bit assembler => 64 bit assembler. Conceptual Issues => Topic started by: maor02x on October 25, 2024, 07:10:51 PM

Title: __asm to masm rdtsc
Post by: maor02x on October 25, 2024, 07:10:51 PM
fixed thanks  :greenclp:
Title: Re: __asm to masm rdtsc
Post by: TimoVJL on October 25, 2024, 07:30:00 PM
why not use intrinsic
__rdtsc (https://learn.microsoft.com/en-us/cpp/intrinsics/rdtsc?view=msvc-170)

main:
00000000  4883EC38                 sub rsp, 38h
00000004  0F31                     rdtsc
00000006  48C1E220                 shl rdx, 20h
0000000A  480BC2                   or rax, rdx
0000000D  4889442420               mov qword ptr [rsp+20h], rax
00000012  488B542420               mov rdx, qword ptr [rsp+20h]
00000017  488D0D00000000           lea rcx, [$SG8278]
0000001E  E800000000               call printf
00000023  33C0                     xor eax, eax
00000025  4883C438                 add rsp, 38h
00000029  C3                       ret
Title: Re: __asm to masm rdtsc
Post by: maor02x on October 25, 2024, 07:33:58 PM
 :greenclp:
Title: Re: __asm to masm rdtsc
Post by: maor02x on October 25, 2024, 08:53:09 PM
Quote from: TimoVJL on October 25, 2024, 07:30:00 PMwhy not use intrinsic
__rdtsc (https://learn.microsoft.com/en-us/cpp/intrinsics/rdtsc?view=msvc-170)

main:
00000000  4883EC38                sub rsp, 38h
00000004  0F31                    rdtsc
00000006  48C1E220                shl rdx, 20h
0000000A  480BC2                  or rax, rdx
0000000D  4889442420              mov qword ptr [rsp+20h], rax
00000012  488B542420              mov rdx, qword ptr [rsp+20h]
00000017  488D0D00000000          lea rcx, [$SG8278]
0000001E  E800000000              call printf
00000023  33C0                    xor eax, eax
00000025  4883C438                add rsp, 38h
00000029  C3                      ret

works !!

.code
rdtsc_asm PROC
    rdtsc                     
    mov rsi, rcx           
    mov dword ptr [rsi], eax 
    mov dword ptr [rsi + 4], edx
    ret
rdtsc_asm ENDP
END
now i wonder if its possible do with 64 bit rax   rdtsc
shl rdx, 32
or rax, rdx
Title: Re: __asm to masm rdtsc
Post by: TimoVJL on October 26, 2024, 01:19:03 AM
try this  :smiley:
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
.code
test_rdtsc PROC
rdtsc
shl rdx, 20h
or rax, rdx
ret
test_rdtsc ENDP
END
with poasm, had to use retn
Title: Re: __asm to masm rdtsc
Post by: maor02x on October 26, 2024, 01:35:27 AM
Quote from: TimoVJL on October 26, 2024, 01:19:03 AMtry this  :smiley:
OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE
.code
test_rdtsc PROC
rdtsc
shl rdx, 20h
or rax, rdx
ret
test_rdtsc ENDP
END
with poasm, had to use retn
amazing its works ! what did u do that fix it ?  :thumbsup:
Title: Re: __asm to masm rdtsc
Post by: maor02x on November 06, 2024, 10:01:06 AM
using clang also fix it so i can use _asm in x64  :azn:

unsigned __int64 time;
unsigned __int64* dest = &time;
__asm
{
    rdtsc
    shl rdx, 32
    or rax, rdx
    mov rsi, dest              // Load the address of time into RSI
    mov qword ptr[rsi], rax   // Store the combined 64-bit result in time
}


    std::cout << "Timestamp Counter: 1054980444028 " << time << std::endl;
    std::cout << "Timestamp Counter2: 1054985324299" << __rdtsc() << std::endl;