/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. .section .data
  11. .set SIGINT, 2
  12. .set SA_SIGINFO, 4
  13. str: .string "Exiting...\n"
  14. len = . - str
  15. .section .bss
  16. # The sigaction structure
  17. # sizeof(struct sigaction) = 140
  18. # (this might be different on your system, see sigaction.h)
  19. #
  20. # struct sigaction {
  21. # sighandler_t sa_handler (4 bytes)
  22. # sigset_t sa_mask (128 bytes)
  23. # int sa_flags (4 bytes)
  24. # void (*sa_restorer) (void); (4 bytes)
  25. # }
  26. .lcomm struct_sigaction, 140
  27. .section .text
  28. .global _start
  29. __sigint_handler:
  30. # Writing in STDOUT
  31. movl $4, %eax
  32. movl $1, %ebx
  33. movl $str, %ecx
  34. movl $len, %edx
  35. int $0x80
  36. # Exiting using the exit status that would be used by the system
  37. # without the signal handler (i.e. 128 + signal number)
  38. movl $1, %eax
  39. movl $3, %ebx
  40. addl $128, 4(%esp)
  41. movl 4(%esp), %ebx
  42. int $0x80
  43. _start:
  44. # Writing the sa_handler field
  45. # offsetof(struct sigaction, sa_handler) == 0
  46. movl $__sigint_handler, struct_sigaction
  47. # Writing the sa_flags field
  48. # offsetof(struct sigaction, sa_flags) == 132
  49. movl $132, %edi
  50. movl $SA_SIGINFO, struct_sigaction(,%edi,1)
  51. # Calling sigaction(int, const struct sigaction *, struct sigaction *)
  52. pushl $0
  53. pushl $struct_sigaction
  54. pushl $SIGINT
  55. call sigaction
  56. addl $12, %esp
  57. # Infinite loop
  58. jmp .
  59. movl $1, %eax
  60. movl $0, %ebx
  61. int $0x80