1
+ ;Example of setting interrupt timers with the setitimer API. API calls found in this example program:
2
+ ; setitimer, signal, write, exit
3
+ ; High level description of what theis example program does:
4
+ ; set up an initial signal handler for our alarm signal
5
+ ; initialize the timer values to 0
6
+ ; Set up timer with 5 second delay with setitimer
7
+ ; Go to infinite loop and wait for signals
8
+ ; First handler
9
+ ; prints message
10
+ ; sets up a new handler (second handler)
11
+ ; sets a re-occuring timer for every 2 seconds
12
+ ; returns to infinite loop
13
+ ; Second handler
14
+ ; writes a new message
15
+ ; refreshes the signal handler for itself for persistence
16
+
17
+ section .text
18
+ global _start
19
+
20
+ _start:
21
+
22
+ ; Set up signal handler to intercept SIGINT (CTRL+C)
23
+ ;------------------------------------------------------------------------------
24
+ mov eax , 48 ; signal
25
+ mov ebx , 14 ; SIGALRM
26
+ mov ecx , handle_it ; Address to signal handler when catching sigint
27
+ int 0x80
28
+
29
+
30
+ ; Setup timing values
31
+ ;------------------------------------------------------------------------------
32
+ mov word [ timeval_interval_s ], 0
33
+ mov word [ timeval_interval_su ], 0
34
+ mov word [ timeval_stop_s ], 0
35
+ mov word [ timeval_stop_su ], 0
36
+
37
+ ; Set Timer
38
+ ;------------------------------------------------------------------------------
39
+ mov word [ timeval_stop_s ], 5 ;Set one-time delay of 5 seconds
40
+ mov eax , 104 ; setitimer
41
+ mov ebx , 0 ; type 0, REAL
42
+ mov ecx , timeval_interval_s ; stop interval
43
+ mov edx , 0
44
+ int 0x80
45
+
46
+ ; Infinit Loop
47
+ ;------------------------------------------------------------------------------
48
+ iloop:
49
+ jmp iloop
50
+
51
+ ; Handler for Interupt Signal
52
+ ;------------------------------------------------------------------------------
53
+ handle_it:
54
+ mov eax , 4 ; write
55
+ mov ebx , 1 ; stdout
56
+ mov ecx , message
57
+ mov edx , 5 ; how many bytes to print
58
+ int 0x80
59
+ ; Set up signal handler to intercept SIGINT (CTRL+C)
60
+ ;------------------------------------------------------------------------------
61
+ mov eax , 48 ; signal
62
+ mov ebx , 14 ; SIGALRM
63
+ mov ecx , annoying_you ; Address to signal handler when catching sigint
64
+ int 0x80
65
+ ; Set New Timer (interval of every 2 seconds)
66
+ ;------------------------------------------------------------------------------
67
+ mov word [ timeval_stop_s ], 2
68
+ mov word [ timeval_interval_s ], 2
69
+ mov eax , 104 ; setitimer
70
+ mov ebx , 0 ; type 0, REAL
71
+ mov ecx , timeval_interval_s ; stop interval
72
+ int 0x80
73
+ ret ; but for now, go back to our infinite loop
74
+
75
+ ; Handler for Interupt Signal
76
+ ;------------------------------------------------------------------------------
77
+ annoying_you:
78
+ mov eax , 4 ; write
79
+ mov ebx , 1 ; stdout
80
+ mov ecx , message2
81
+ mov edx , 14 ; how many bytes to print
82
+ int 0x80
83
+ ; Persist signal handler to intercept SIGINT (CTRL+C)
84
+ ;------------------------------------------------------------------------------
85
+ mov eax , 48 ; signal
86
+ mov ebx , 14 ; SIGALRM
87
+ mov ecx , annoying_you ; Address to signal handler when catching sigint
88
+ int 0x80
89
+ ret ; but for now, go back to our infinite loop
90
+
91
+ section .data
92
+ message db 'Ohai' , 0x0a
93
+ message2 db 'Annoying You!' , 0x0a
94
+
95
+ section .bss
96
+ timeval_interval_s resb 4
97
+ timeval_interval_su resb 4
98
+ timeval_stop_s resb 4
99
+ timeval_stop_su resb 4
100
+
101
+ ; itimerval structure - Each value can be null
102
+ ;------------------------------------------------------------------------------
103
+ ; timeval interval - if null, interupt once based on timeval current time
104
+ ; timeval current time - if null, interupt at interval specified by the interval
105
+
106
+ ; timeval structure
107
+ ;------------------------------------------------------------------------------
108
+ ; sec
109
+ ; usec
0 commit comments