CodexBloom - Programming Q&A Platform

Stack Overflow scenarios When Using RDTSC for High-Precision Timing in x86 Assembly

👀 Views: 2 💬 Answers: 1 📅 Created: 2025-06-04
assembly x86 performance Assembly

I can't seem to get Hey everyone, I'm running into an issue that's driving me crazy... I'm trying to measure the execution time of a specific function using the RDTSC instruction in my x86 assembly code, but I'm working with a stack overflow behavior when I run the program. Here’s a snippet of what I’ve implemented: ```assembly section .text global _start _start: call start_timer ; Call the timer function ; Your function code here call stop_timer ; Call the function to stop the timer ; Exit the program start_timer: rdtsc ; Read time-stamp counter shl rdx, 32 ; Shift high part to the left or rax, rdx ; Combine high and low parts ret stop_timer: rdtsc ; Read time-stamp counter again ; Here I would calculate the difference ret ``` The program compiles fine without any warnings, but when I run it, I get a stack overflow behavior. I suspect that it might be due to the absence of proper stack frame management or perhaps because I’m calling these functions recursively in some other part of my code. I’ve tried addressing this by ensuring that I don’t have unintended recursive calls, but the behavior continues. I also ran this on a machine with an Intel i7 processor and I'm using NASM to assemble the code. In addition, I checked the stack size with the command line option `-Wl,--stack,1048576` to ensure that it’s large enough. Still, the overflow happens consistently. Can anyone provide insight into what I might be missing or if there’s a better way to implement high-precision timing without running into stack issues? My development environment is macOS. Any help would be greatly appreciated! Am I approaching this the right way?