//---------------------------------------------------------------- // sigdemo2.s // // This is a revision of our 'sigdemo1.s' program. It adds // code which displays the program's user-level stack, both // before and after the signal is generated, so that we can // study the signaling mechanism being used by the kernel. // // assemble using: $ as sigdemo2.s -o sigdemo2.o // then link with: $ ld sigdemo2.o -o sigdemo2 // // programmer: ALLAN CRUSE // written on: 03 MAY 2005 //---------------------------------------------------------------- .equ SIGSEGV, 11 .equ sys_exit, 1 .equ sys_write, 4 .equ sys_sigaction, 67 .equ SA_SIGINFO, 0x00000004 .equ STDOUT_FILENO, 1 .section .data sa: # struct sigaction sa_handler: .long action # __sighandler_t sa_handler sa_mask: .long 0 # __sighandler_t sa_msask sa_flags: .long SA_SIGINFO # unsigned long sa_flags sa_restorer: .long 0 # __sig_restore_t sa_restorer outline: .ascii "\nxxxxxxxx: xxxxxxxx " outsize: .int . - outline hexlist: .ascii "0123456789ABCDEF" origtos: .int 0 .text action: # show user-stack AFTER entering the signal-handler push %esp call showtos # exit( 1 ) movl $sys_exit, %eax movl $1, %ebx int $0x80 subrtn: # try writing 0 to the supplied memory-address movl 4(%esp), %edi movb $0, (%edi) ret _start: # show user-stack BEFORE entering the signal-handler movl %esp, origtos pushl origtos call showtos # sigaction( SIGSEGV, &mysa, NULL ); movl $sys_sigaction, %eax movl $SIGSEGV, %ebx movl $sa, %ecx movl $0, %edx int $0x80 # subrtn( NULL ); pushl $0 call subrtn addl $4, %esp # exit( 0 ); movl $sys_exit, %eax movl $0, %ebx int $0x80 showtos: # procedure to display values on the user-stack pushal # loop displays current values on the user-stack movl origtos, %esi # source-index subl %esp, %esi shrl $2, %esi incl %esi nxelt: # format the item's address leal outline+1, %edi leal 36(%esp, %esi, 4), %eax call eax2hex # format the item's value leal outline+11, %edi movl 36(%esp, %esi, 4), %eax call eax2hex # display the formatted item-info call output # adjust the item-index decl %esi jnz nxelt # finish by writing a newline movl $sys_write, %eax movl $STDOUT_FILENO, %ebx leal outline, %ecx movl $1, %edx int $0x80 popal ret $4 # Note: function-argument discarded by callee output: # writes contents of 'outline' array to the screen pushal movl $sys_write, %eax movl $STDOUT_FILENO, %ebx leal outline, %ecx movl outsize, %edx int $0x80 popal ret eax2hex: # procedure converts EAX to a hexadecimal string at EDI pushal movl %eax, %edx leal hexlist, %ebx movl $8, %ecx nxnyb: roll $4, %edx movb %dl, %al andb $0xF, %al xlat movb %al, (%edi) inc %edi loop nxnyb popal ret .global _start .end