本帖最後由 Super169 於 2014-1-10 18:17 編輯
回復 31# goshigoo
你只係講到 c++ 咋, 幾驚你問我 assembly 甚至 micro-programming, 呢 d 就真係無掂好多年了.
既然你對 c 有認識, 咁就易講 D 喇, 用番 c 講. 唔知你對 c compile 出黎既 program, 同佢點樣在 os execute, 又知道有幾多呢? 真係 native 比 cpu 行?
你有咁既諗法, 可能你只係做過 d console 既 program, 計下數 print 番個 result 出黎呢類功課. 或者係因為 compile 出黎既 binary 好多時會叫做 machine code. 令你以為成個都係純 cpu instruction.
windows 比較複雜, 先用比較簡單既 dos 去睇, 當一個簡單既 console in/out.
你用 c 寫句 printf 去 display 一個 character, 你估背後會 compile 出乜?
真係一個直接可以在 cpu 行既 instruction? 就算寫 assembly, 好多野都唔係直接 instruction, 都係經番一 d dos 既 interrupt 去做. int 21 就係 dos 包裝左既 int 了. 定係你諗住佢會 gen code 直接 update display map? (Coding level 我最盡就係做到呢步, 完全 bypass OS. 最後點轉化成畫面既顯示, 就係 hw level 既野了. 我唔知家陣係咪可以寫野落 display card control 佢出 monitor. 如果你識得 coding level control hardware 出 display 既做法, 我都想請教一下. 不過, 我相信 office 呢類野唔會咁做, 肯定係經 api 出.)
由 windows 去就更長遠, 要 display 一個 character, 對 cpu 黎講係無意義既, 當中要經 windows / bios 內部去處理, 轉化成對 hd 既 code. 而你 compile 出黎果個, 只係比 windows 行到既 "machine code". 雖然 比 .net 既 IL 更低 level, 但都未去到完全 native 比 cpu 行既 code, 當中仲有大把 OS level 既 call.
另一個簡單D講, 一句 malloc, 你估佢係問 cpu 攞 memory 定係問 os 攞 memory?
以上只係簡單既兩個 function, 我諗你唔會唔認識吧, 你認為佢地可唔可能 compile 出一段唔駛靠 os 純 cpu 行既 instruction?
你應該聽過 DLL 吧, 你寫 program 或者都會用到一 d 人地既 library, 你都應該知道, compile 出黎既 code 唔係直接 native 行到哂喇 - 無左 d library 行到嗎? 再者, 就算你唔用人地既 library, 又或者個 DLL 都係你寫既, 你估 compile 完既 program 背後可唔可以唔需要用 windows 既 library?
真係純 cpu intruction? 你估佢無左個 os 仲行到幾多?
就算你 command prompt 用 debug 直接入 code 寫 program, 都未必可以完全係 cpu 直接行既 instruction 喇, 何況 windows application.
如果你只係話佢行出黎唔會慢好多, 因為 overhead 唔太大, 都仲講得通. 但就唔係因為 gen 出黎既 program 成個係 native 既 cpu instuction 囉.
都係果句, 諗下 app, os, cpu 之間既關係吧.
除非你寫緊 BIOS 或者一d low level 既 driver, 否則都好難話 compile 出黎既成舊 cpu instruction (就算 driver 都可能 call 左好多其他野). |