Interview questions: 64-bit timer (decrementer) device


A hardware system contains a 64-bit timer (decrementer) device. This device changes a 64-bit register on each processor tick (very often). The instruction set contains only two commands to work with this register. Specifically, the instruction to get the 32-bit high part of the register - get_hi and the instruction to get the 32-bit low part of the register - get_lo. Please develop an algorithm using get_hi and get_lo to get the whole 64-bit value of the register.


static inline uint64_t join(uint32_t h, uint32_t l)
   return (((uint64_t)h << 32) | l);
static inline uint64_t get_hi_and_lo()
   uint32_t h1, h2, l;
   do {
      /*pheudocode like*/
      __asm__ __volatile__(
         "mov %%reg1, %%reg3\n\t"
         "get_hi\n\t" : "=r3" (h1), "=r2" (l), "=r1" (h2)
   } while (h1 != h2);
   return join(h1, l);


comments powered by Disqus