PageRenderTime 15ms CodeModel.GetById 12ms app.highlight 1ms RepoModel.GetById 2ms app.codeStats 0ms

/sigaction.s

http://github.com/felipensp/Assembly-x86
Assembly | 73 lines | 61 code | 12 blank | 0 comment | 2 complexity | 5c5ed22ef351e6a923ae3d399b0cd563 MD5 | raw file
 1# Signal handling
 2# Author: Felipe Pena <sigsegv>
 3# Date: 2011-05-09
 4#
 5# $ as -o sigaction.o sigaction.s
 6# $ ld --dynamic-linker /lib/ld-linux.so.2 -lc -o sigaction sigaction.o
 7# $ ./sigaction 
 8# ^CExiting...
 9# $ echo $?
10
11	.section .data
12
13.set SIGINT, 2
14.set SA_SIGINFO, 4
15str:	.string "Exiting...\n"
16		len = . - str
17
18	.section .bss
19	
20# The sigaction structure
21# sizeof(struct sigaction) = 140 
22# (this might be different on your system, see sigaction.h)
23#
24# struct sigaction {
25#	sighandler_t sa_handler (4 bytes)
26#	sigset_t sa_mask (128 bytes)
27#	int sa_flags (4 bytes)
28#   void (*sa_restorer) (void); (4 bytes)
29# }
30.lcomm struct_sigaction, 140
31
32	.section .text
33.global _start
34
35__sigint_handler:
36	# Writing in STDOUT
37 	movl $4, %eax
38	movl $1, %ebx
39	movl $str, %ecx
40	movl $len, %edx	
41	int $0x80
42	
43	# Exiting using the exit status that would be used by the system
44	# without the signal handler  (i.e. 128 + signal number)
45	movl $1, %eax
46	movl $3, %ebx
47	addl $128, 4(%esp)
48	movl 4(%esp), %ebx 	
49	int $0x80
50	
51_start:
52	# Writing the sa_handler field
53	# offsetof(struct sigaction, sa_handler) == 0
54	movl $__sigint_handler, struct_sigaction
55	
56	# Writing the sa_flags field
57	# offsetof(struct sigaction, sa_flags) == 132
58	movl $132, %edi
59	movl $SA_SIGINFO, struct_sigaction(,%edi,1)
60	
61	# Calling sigaction(int, const struct sigaction *, struct sigaction *)
62	pushl $0
63	pushl $struct_sigaction
64	pushl $SIGINT
65	call sigaction
66	addl $12, %esp
67	
68	# Infinite loop
69	jmp .
70
71	movl $1, %eax
72	movl $0, %ebx
73	int $0x80