0

I am lost about taking time of my program in Assembly Language AT&T Syntax. Is there a time function built in Assembly?

Math
  • 1
  • 2
    What do you mean by “time function?” Also, what architecture and operating system are you programming for? Given that you mention AT&T syntax, is it i386 or amd64? – fuz Nov 01 '22 at 11:55
  • Assembly has no functions built in (there are only instructions and directives). Some CPUs do provide an instruction that obtains a time stamp (e.g. the `rdtsc` instruction on modern 80x86) but you'd need to be aware of their details and pitfalls and write suitable conversion code (as it'll probably give "CPU clock cycles since an unknown time" and not something nice like "day, month, year" or "nanoseconds since a known epoch"). – Brendan Nov 01 '22 at 12:57
  • 1
    Do you mean "time of day" (e.g. 7:48:37 AM) or elapsed time (4.102 ms)? Getting the time of day requires either a call to the operating system (if you are running under an OS), or accessing a hardware device (if you are running on bare metal, such as if you are writing the OS). So you will have to tell us which case applies to you, and then what operating system and/or hardware you are using. Elapsed time can be done with `rdtsc`, but you also have to find out the clock speed to convert it into an actual time interval. – Nate Eldredge Nov 01 '22 at 13:24
  • To time a loop, you can use `lfence`;`rdtsc` on a modern CPU, or `rdpmc` if you've programmed the hardware PMUs to be counting an event like `cpu_clk_unhalted.thread`. See [How to get the CPU cycle count in x86\_64 from C++?](https://stackoverflow.com/q/13772567) for `rdtsc` details, including some links about using it for microbenchmarks. I normally find it easier to use a high enough repeat count that I can time the whole program (in a static executable with no libc for minimum overhead) with `perf stat`. If a repeat loop runs about a second, that dwars startup overhead of a couple millis – Peter Cordes Nov 01 '22 at 22:26

0 Answers0