timer.asm 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. SYS_TIMERFD_CREATE equ 0x142
  2. SYS_TIMERFD_GETTIME equ 0x146
  3. SYS_TIMERFD_SETTIME equ 0x145
  4. SYS_READ equ 0x3
  5. ;timerfd_setttime()
  6. ;argument struct itimerspec contains 2 struct timespec
  7. ;struct timespec contains seconds as time_t (integer unixtimestamp?) and nanoseconds as long
  8. section .bss
  9. ;timer filedescriptor
  10. tfd RESB 4
  11. ;target memory for sys_timerfd_gettime
  12. ot RESB 16
  13. ;timer struct interval
  14. itimer RESQ 2
  15. ;timer struct init val
  16. vtimer RESQ 1
  17. ;variable for timer read
  18. tval RESB 8
  19. section .data
  20. tick equ 2000
  21. msg db 'tick',0xa
  22. msgl equ 5
  23. section .text
  24. global _start
  25. _start:
  26. ;prepeare sys_timerfd_create
  27. mov eax, SYS_TIMERFD_CREATE
  28. mov ebx, 1
  29. mov ecx, 0
  30. int 0x80
  31. ;get timer fd
  32. mov [tfd], eax
  33. ;init timer
  34. ;interval used for timer triggers
  35. mov [itimer], dword 0x05 ;sec
  36. mov [itimer + 4], dword 0x00 ;nanosecs
  37. ;value - initial value must bes set to arm timer
  38. mov [itimer + 8], dword 0x05 ;secs
  39. mov [itimer + 12], dword 0x0 ;nanosecs
  40. ;settime sys_timerfd_settime
  41. mov eax, SYS_TIMERFD_SETTIME
  42. mov ebx, [tfd]
  43. mov ecx, 0
  44. mov edx, itimer
  45. mov esi, 0
  46. int 0x80
  47. poll:
  48. ;get time from fd
  49. ;not used just for debugging with strace
  50. mov eax, SYS_TIMERFD_GETTIME
  51. mov ebx, [tfd]
  52. mov ecx, ot
  53. int 0x80
  54. ;sysread on fd from timer blocks til timer triggerd
  55. mov eax, SYS_READ
  56. mov ebx, [tfd]
  57. mov ecx, tval
  58. mov edx, 0x20
  59. int 0x80
  60. ;print "tick"
  61. mov eax, 4
  62. mov ebx, 1
  63. mov ecx, msg
  64. mov edx, msgl
  65. int 0x80
  66. jmp poll
  67. exit:
  68. mov eax, 1
  69. mov ebx, 0
  70. int 0x80