Posts

Showing posts with the label gcc

scanf throws Segmentation fault (core dumped) when using nasm + gcc on linux 64bit

When compiling below code: using: and then run it runs, print: enter a number: but then crashes and prints: Segmentation fault (core dumped) So printf works fine but scanf not. What am I doing wrong with scanf so? Use sub rsp, 8 / add rsp, 8 at the start/end of your function to re-align the stack to 16 bytes before your function does a call . Or better push/pop a dummy register, e.g. push rdx / pop rcx , or save/restore a call-preserved register like RBP. On function entry, RSP is 8 bytes away from 16-byte alignment because the call pushed an 8-byte return address. See Printing floating point numbers from x86-64 seems to require %rbp to be saved, main and stack alignment, and Calling printf in x86_64 using GNU assembler. This is an ABI requirement which you used to be able to get away with violating when there weren't any FP args for printf. But not any more. gcc's code-gen for glibc scanf now depends on 16-byte stack alignment even when AL == 0 . It seems to have auto...