flat assembler
Message board for the users of flat assembler.

Index > Main > Keep passed values after a cpuid call, dll procedure

Author
Thread Post new topic Reply to topic
jmurray



Joined: 28 Sep 2019
Posts: 8
Location: Plymouth, UK
jmurray 28 Apr 2020, 20:39
I'm trying to implement a method to measure memory latency for my university project. The procedure takes in two values rcx(two longs for timestamp counting) and rdx(a memory location pointer).

I require both of these to be available after a CPUID call to see whether such a call affects memory latency. Previously I have solved with with a push pop of rcx, but I am unable to push pop both rcx and rdx as I get an "invalid value" error for "push rdx".

The basic method is as follows: rdtsc > CPUID 0 > read from rdx > rdtsc > return rdtsc's

The code is below:

Code:
proc MeasureMemoryCPUID uses rcx, rdx ;rcx and rdx need to be push'ed before CPUID
     push rdx
     ;measure cycles
     rdtsc
     MFENCE
     mov [rcx],eax
     add rcx,4
     mov [rcx],edx
     add rcx,4
     ;code to measure
     push rcx
     mov eax,0H
     CPUID
     pop rdx
     mov eax, dword [rdx];measuring the leading dword of [rdx] into eax
     pop rcx
     ;measure cycles
     rdtsc
     MFENCE
     mov [rcx],eax
     add rcx,4
     mov [rcx],edx
     add rcx,4
     mov rax,rcx
     ret
endp
    


I am unsure how to implement this correctly.
Post 28 Apr 2020, 20:39
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20451
Location: In your JS exploiting you and your system
revolution 28 Apr 2020, 22:09
Remove the comma after rcx.
Code:
proc ... uses rcx rdx    
Also if you want your code section to show correctly don't tick the "Disable BBCode in this post" in your post. I have edited your post to fix it.
Post 28 Apr 2020, 22:09
View user's profile Send private message Visit poster's website Reply with quote
fpissarra



Joined: 10 Apr 2019
Posts: 64
fpissarra 29 Apr 2020, 14:24
If you want to "measure the latency", you should follow Intel's recomendations. For example, they recomend to serialize the processor BEFORE trying to read TSC. You could use MFENCE before the serialization:

Code:
  push rbx  ; MS ABI demands RBX, RSI and RDI to be preserved.
  push rsi
  push rdi

  mfence    ; memory sync.

  xor eax,eax  ; serialize processor.
  cpuid

  rdtsc

  ; store EDX:EAX somewhere... (probably 1 additional cycle).
  mov  esi,eax
  mov  edi,edx

  ...
  ... instructions to measure here... (RSI and RDI must be preserved!).
  ...

  ; OBS: You should reserialize the processor again here. to avoid
  ; reordering effects.
  xor  eax,eax  ; 20+ cycles, maybe?
  cpuid

  rdtsc

  ; subtract EDX:EAX from previously obtained EDX:EAX.
  sub  eax,esi
  sbb  edx,edi
  shl  rdx,32
  or   rax,rdx

  ; OBS: You could subtract the additional cycles from RAX here.
  ;   sub  rax,21    ; 21 cyvles, maybe?
  ;   jns  .not_negative
  ;   xor  rax,rax   ; clamp to zero.
  ; .not_negative:

  pop rdi
  pop rsi
  pop rbx
  ret    

But be aware that using TSC to measure latency isn't a precise method...
Post 29 Apr 2020, 14:24
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.