[問題] funtion、address、instruction的關係

看板C_and_CPP作者 (NoTeXactLy)時間9年前 (2014/07/03 18:19), 9年前編輯推噓0(0024)
留言24則, 6人參與, 最新討論串1/1
以下是書本的句子: When a program reaches the function call instruction, the program stores the memory address of the instruction immediately following the function call, copies function arguments to the stack(a block of memory reserved for that purpose), jumps to the memory location that marks the beginning of the function, executes the instructon whose address it saved. 出處: http://tinyurl.com/o6xo4bd C++ primer plus books.google.com 請問 function call instructioninstructionfunction call這三者指同樣東西嗎? 而function是指function definition嗎? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.182.140.221 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1404382750.A.356.html

07/03 19:08, , 1F
這看起來是組合語言的東西
07/03 19:08, 1F

07/03 19:09, , 2F
function call inst => asm: call some_func
07/03 19:09, 2F
對 只是這剛好是C++的課文 想請教c++的function call in calling function與called function兩者 從instruction的角度看上文提到的address是甚麼樣子? 如果main()裡面有個func_one() function call是func_one() calling function是main() called function是 整個func_one()的definition func_one() { ... } 當compilation之後 memory address of the function call instruction會被放到 main()的func_one()之後 這是上述課文的意思嗎? 謝謝 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 21:04:27 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 21:05:41

07/03 21:30, , 3F
address of function call instruction push to stack是
07/03 21:30, 3F

07/03 21:31, , 4F
runtime 做的事,不是 compile time
07/03 21:31, 4F

07/03 21:32, , 5F
簡單來說這句就是在說每次執行到 function call 指令時
07/03 21:32, 5F

07/03 21:32, , 6F
會先記下 function return 回來時的位置,也就是當初你
07/03 21:32, 6F

07/03 21:33, , 7F
call function 跳走的下一條指令
07/03 21:33, 7F
但是課文中 只有copies function arguments to the stack 而 the program stores the memory address of the instruction immediately following the function call 這句話並沒有to the stack ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 22:39:54

07/03 23:05, , 8F
因為不見得要放stack 雖然一般都放stack
07/03 23:05, 8F
假設當 function call instruction的位址是69-75 function(整個definition)的位址是108-132 另外beginning of the function(整個definition)的位址是108 那麼會存76到133的意思嗎? 或是存76到stack但不會是133? 謝謝 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 23:25:41 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 23:27:56 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 23:39:56 ※ 編輯: NoTeXactLy (175.182.140.221), 07/03/2014 23:46:53

07/03 23:47, , 9F
有創意。
07/03 23:47, 9F

07/03 23:48, , 10F
身為一個程式修理員,多找幾本書看也很合理且合邏輯
07/03 23:48, 10F

07/03 23:49, , 11F
書上沒說to stack就咬死一定沒to stack,又不是背六法。
07/03 23:49, 11F
to stack當然可以 我也沒說一定沒to stack 您說的咬死比較有創意 謝謝 ※ 編輯: NoTeXactLy (175.182.140.221), 07/04/2014 00:14:14 ※ 編輯: NoTeXactLy (175.182.140.221), 07/04/2014 00:16:28

07/04 08:29, , 12F
果然是背六法的。
07/04 08:29, 12F

07/04 18:32, , 13F
function arguments放的位置並沒有明確定義名稱
07/04 18:32, 13F

07/04 18:33, , 14F
甚至有的還是丟到registry做pass的
07/04 18:33, 14F

07/04 18:33, , 15F
他唯一的名稱就叫作Function Frame
07/04 18:33, 15F

07/04 18:34, , 16F
居泥在他是不是stack...似乎有點搞錯方向了你
07/04 18:34, 16F

07/04 18:35, , 17F
這東西我只能跟你說 位置頗微妙 你要先搞懂stack/heap
07/04 18:35, 17F

07/04 18:35, , 18F
怎麼長的 比較有這種感覺就是...
07/04 18:35, 18F

07/04 18:36, , 19F
另外規格也沒有指名function frame要丟再哪裡 編譯器
07/04 18:36, 19F

07/04 18:36, , 20F
可以自己決定的
07/04 18:36, 20F
您誤解了 我對於function arguments放在何處並沒有疑問 所以也不"拘泥"function arguments是否放在stack 若您沒仔細閱讀英文句子 我可以很肯定是您搞錯方向 我的疑問在文章的前兩列 When a program reaches the function call instruction, the program stores the memory address of the instruction immediately following the function call, copies function arguments to the stack(a block of memory reserved for that purpose), jumps to the memory location that marks the beginning of the function, executes the instructon whose address it saved. 也就是 When a program reaches the function call instruction, the program stores the memory address of the instruction immediately following the function call, 第三列 copies function arguments to the stack 並非我的疑問所在 且此處的to the stack並非針對前兩列 最後謝謝carylorrk板友正確理解我的疑問並寄信解釋 ※ 編輯: NoTeXactLy (112.105.249.5), 07/04/2014 18:55:26 ※ 編輯: NoTeXactLy (175.182.114.79), 07/04/2014 23:52:35

07/06 01:29, , 21F
放不放stack要看平台的Call Convention(呼叫慣例)請查網.
07/06 01:29, 21F

07/06 01:30, , 22F
functionCall ins 一般是跳躍指令,所以跳之前將引數搬到
07/06 01:30, 22F

07/06 01:32, , 23F
Reg或Stack是編譯器的事情並非Runtime~跳躍指令會紀錄返
07/06 01:32, 23F

07/06 01:33, , 24F
回位置到rpc,return的asm會從rpc調回回去後執行的位址~
07/06 01:33, 24F
謝謝witchang的專業解答 也感謝另一位版友tjjh89017寄信畫圖解釋 ※ 編輯: NoTeXactLy (175.180.193.173), 07/06/2014 22:33:25 ※ NoTeXactLy:轉錄至看板 Sub_CS 07/12 03:01
文章代碼(AID): #1JjIuUDM (C_and_CPP)