I have this assembly code:
00000000 <Q2>: 0: 55 push %ebp 1: 89 e5 mov %esp,%ebp 3: 83 ec 20 sub $0x20,%esp 6: c7 45 fc 1f 00 00 00 movl $0x1f,-0x4(%ebp) d: c7 45 f8 06 f4 ff ff movl $0xfffff406,-0x8(%ebp) 14: 8b 45 fc mov -0x4(%ebp),%eax 17: 8b 55 08 mov 0x8(%ebp),%edx 1a: 8d 04 02 lea (%edx,%eax,1),%eax 1d: 89 45 f4 mov %eax,-0xc(%ebp) 20: 8b 45 08 mov 0x8(%ebp),%eax 23: 0f af 45 f4 imul -0xc(%ebp),%eax 27: 89 45 f0 mov %eax,-0x10(%ebp) 2a: 8b 45 f8 mov -0x8(%ebp),%eax 2d: 8b 55 f0 mov -0x10(%ebp),%edx 30: 8d 04 02 lea (%edx,%eax,1),%eax 33: 89 45 ec mov %eax,-0x14(%ebp) 36: 83 7d ec 00 cmpl $0x0,-0x14(%ebp) 3a: 74 0a je 46 <Q2+0x46> 3c: b8 00 00 00 00 mov $0x0,%eax 41: 8b 00 mov (%eax),%eax 43: 89 45 f4 mov %eax,-0xc(%ebp) 46: 8b 45 f8 mov -0x8(%ebp),%eax 49: 89 c2 mov %eax,%edx 4b: c1 fa 1f sar $0x1f,%edx 4e: f7 7d 08 idivl 0x8(%ebp) 51: c9 leave 52: c3 ret I thought the segmentation fault was on line 4e where it would divide by the %edx after it has been shifted right by 0x1f (31) in decimal. I figured that answer would most likely result in a division by 0 unless the number was greater than or equal to 2^31.
I have been informed that whether or not the segmentation fault occurs depends on the value of the 1 parameters of the function. Upon further inspection of the assembly code I have come to the conclusion that the fault is not dependent on the parameter. I can't seem to find what I've overlooked. Can anyone help?
objdumpoutput of an object file, line 41 may have an invisible relocation entry. @John if so, can you use-drswitches?4eunless you got a corrupted framepointer (%ebp) register. That's because division-by-zero causesSIGFPEwith asiginfo_thavingsi_code == FPE_INTDIV. Nothing in your function can corrupt%ebp, so if aSIGSEGVoccurs on that line, you either have a very buggy signal handler / home-grown context switcher (the only userspace entities capable of changing%ebp), or a kernel bug (unlikely). Use 'gdb' to do aninfo reg/disas $eip-20 $eip+20. Also - your code is unoptimized compiler output. Source, please, to justify the "C" tag ;-)