[問題] 呼叫gets前的sub $0xc,%esp

看板ASM作者 (-std=c++14)時間7年前 (2016/07/28 14:50), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/1
小弟最近在練習看組語,遇到問題想來請教各位 這是source code #include <stdlib.h> #include <stdio.h> void print(){ char buf[5]; gets(buf); } int main(){ print(); } 這是在 ubuntu 16.04 amd64 用 gcc -fno-stack-protector -m32 -O0 test.c 編譯出來的結果 0804840b <print>: 804840b: 55 push %ebp 804840c: 89 e5 mov %esp,%ebp 804840e: 83 ec 18 sub $0x18,%esp <=為啥預留24 bytes 8048411: 83 ec 0c sub $0xc,%esp <=為啥要減12 8048414: 8d 45 f3 lea -0xd(%ebp),%eax 8048417: 50 push %eax 8048418: e8 c3 fe ff ff call 80482e0 <gets@plt> 804841d: 83 c4 10 add $0x10,%esp 8048420: 90 nop 8048421: c9 leave 8048422: c3 ret 我的問題有兩個 (1) 我明明只宣告buf[5]的local variable 我覺得只會預留8 bytes,為甚麼gcc卻幫我預留了24 bytes (2) 為啥在呼叫gets前會出現 sub $0xc,%esp 這個指令有什麼作用? -----------------補充-------------------- 這是對照組,沒有呼叫gets的版本 #include <stdlib.h> #include <stdio.h> void print(){ char buf[5]; } int main(){ print(); } 080483db <print>: 80483db: 55 push %ebp 80483dc: 89 e5 mov %esp,%ebp 80483de: 83 ec 10 sub $0x10,%esp <=這邊也留了16 bytes 80483e1: 90 nop 80483e2: c9 leave 80483e3: c3 ret -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.186.243 ※ 文章網址: https://www.ptt.cc/bbs/ASM/M.1469688630.A.A2B.html

07/28 15:53, , 1F
在C_and_CPP得到解答了 原因是gcc的stack boundary
07/28 15:53, 1F

07/28 15:54, , 2F
07/28 15:54, 2F
文章代碼(AID): #1NcQiseh (ASM)