C++輸出hello world,請從電子電路、內存CPU、程序層面解釋一下?

放棄提問:知乎上是否有大牛可以把用C++輸出hello world 從原子、電子、電路、內存CPU、程序層面一直描述到哲學層面?


你用沾滿油膩的手在鍵盤上敲了".","/","h","e","l","l","o"。你按鍵的動作導致該鍵所在的行與列都接通了。與此同時,你鍵盤內部的控制器正在例行公事地拉高電壓進行掃描, 這次它發現某些輸入埠居然產生了電子的流動, 這立刻牽扯到了許多鍵盤控制器內部的場效應晶體管源極和漏極的導通與關閉,根據預設的邏輯,他把煙頭一撇,熟練地讀取所有行列的電平高低,並且根據行編號和列編號計算出被按下的鍵。

過了一會兒,USB埠的電平發生了變化,原來是2毫秒一次的USB詢問來了。在忽高忽低的電平驅動下,鍵盤控制器把按下的鍵的掃描碼告訴了USB控制器,然後繼續坐下休息並等待下一次掃描周期。

USB控制器之前總是拿不到數據有些無聊,這下總算有事情做了。它馬上拉了中斷線的電平, CPU一個激靈,停下了當前的指令,轉而進入中斷處理流程。CPU地址匯流排上每根線的電平在飛速的跳躍著,最後組成了一個地址,它正是指向了USB驅動程序內的一段中斷處理程序。於是忙碌的CPU指令緩存開始通過數據匯流排向CPU輸送代碼。

一直在內存長眠的USB驅動程序代碼被CPU驚醒了,它看了看拿到的數據,決定繼續上報。早在開機的時候,相應的輸入模塊就在他這裡註冊在案了,於是輸入模塊的代碼開始被CPU執行。輸入模塊把這個掃描碼翻譯成了鍵盤按鍵,隨後打開了在它這裡的所有註冊人的名單並逐一通知: "弟兄們開工了!"。於是,操作系統內核的這次任務就到此為止了。

通常情況下,系統的輸入中樞程序會將拿到的輸入事件有序地存放起來,然後再逐個分發。桌面程序是它的常客。它拿到了輸入事件之後查看了當前在前台的窗口,然後便把事件通過進程間通信發了過去。

這回在前台的是終端程序。它可能之前沒有被進程調度器放到CPU上,有些不高興。不過幸運的是這回輪到他的時候終於有事做了,它老實地把按鍵一個一個告訴顯示模塊:於是,幾乎同剛才的過程相反地,一串信息流從上往下在桌面,圖形庫,顯卡驅動間不斷地被傳遞和轉化,最後到達了終點:顯示器內部的控制器。於是,你看到了紫色的終端跳出了一些白字,"./hello", 然後你若無其事地按下了回車鍵。你可能以為計算機的忙碌才剛剛開始,於是跑到知乎上問了這個問題...

好了,不講了。其實我真正想說的是,問這個問題,無異於問: "上帝打了個噴嚏,全宇宙的每個物體都發生了什麼?"


首先:

原子:原子

電子:電子

電荷:電荷

電場:電場

電勢:電勢

電壓(電勢差)電壓

電流:電流

然後:

電阻:電阻

電容:電容

電感:電感

阻抗:阻抗

歐姆定律:歐姆定律

串聯:串聯電路

並聯:並聯電路

KCL:基爾霍夫電路定律

再然後:

變壓器:變壓器

交流電:交流電

直流電:直流電

整流器:整流器

要進計算機了:

半導體:半導體

導體:導體

PN結:PN結

晶體管:晶體管

電子管:真空管

二極體:二極體

場效應管:場效應管

達靈頓:達靈頓晶體管

並且:

邏輯門:邏輯閘

同時:

集成電路:集成電路

接下來:

CPU:中央處理器

內存:電腦記憶體

硬碟:硬碟

南橋:南橋

北橋:北橋

匯流排:I/O匯流排

顯卡:顯示卡

顯示器:顯示器

鍵盤:電腦鍵盤

滑鼠:滑鼠

好累啊:

二進位:二進位

原,反,補碼:反碼_百度百科

ascII:ASCII

數理邏輯;:數理邏輯

數論:數論

圖論:圖論

編碼理論:編碼理論

還有:

操作系統:操作系統

程序語言:編程語言

編譯器:編譯器

握了無數棵草!

根本寫不完的好嘛!!!

本來以為就百科就好!!!

結果!!

來來來,戰個痛快!!!

超級長的【數學】【物理】【專業】書單

EE參考書目---電子類

EE參考書目---信號通信類

EE參考書目---計算機類

【邏輯學】書單

唐李洋的計算機書單

[數學書單]數學專業參考書整理推薦

看完以上不保證能回答這個問題………………………………

心好塞!!


比較高級的抽象說法可以是,CPU利用電子流建立了一個從硬碟的狀態到LCD的狀態的映射。

以下是純乾貨,括弧內是可以擴展學習的關鍵詞。

接 @顏駿 的回車鍵,假設程序從SHELL啟動:

操作系統運行在CPU上,在不同的進程間來回切換著,保證了每個進程都有自己運行的時間。突然,操作系統偵測到了一個寄存器的bit的從0變成了1,這是一個硬體中斷bit。通過這個bit的位置,和預存的信息,操作系統判斷出:回車鍵被按下了。(Context Switch, Signal Handling)

操作系統檢查了自己的前台程序變數,是SHELL,於是就把這段字元發送到SHELL的標準輸入里。之後,操作系統恢復了上次SHELL運行的狀態變數,切換到了SHELL。

SHELL醒了過來,在未來的0.01秒內,CPU將完全屬於SHELL。

一直在無窮循環等待輸入的SHELL終於讀到了一串字元。他parse了這段字元,發現是要求打開一個新進程。他決定創造一個子進程。(Fork)

子進程創造指令被執行的一瞬間,操作系統蘇醒了,操作系統收到了創建子進程l的信號,於是他複製了SHELL的虛擬內存結構體創造了一個新的SHELL,然後切換到了這個新的SHELL。

子SHELL接收了fork的返回值,得知自己是個子進程,通過條件判斷,子SHELL進入了和母SHELL不同的指令區域,開始運行./hello。(Exec)

操作系統又醒了過來,接受到了運行./hello的信號,修改了子SHELL的指令區域,使其指向硬碟的上的的./hello文件,清空了子SHELL的棧和堆,哦,不,現在已經是hello進程了。操作系統隨即切換到了hello進程。(Virtual Memory Struct)

Hello想要運行自己的指令,於是他告訴了CPU自己指令的虛擬地址。(VPO to PPO)

CPU需要將虛擬地址轉化為物理地址。CPU首先檢查了TLB,發現並沒有該虛擬地址的記錄,於是CPU去檢查虛擬地址頁面表格。CPU發現,這個虛擬頁面還並未被載入到物理內存,於是CPU向操作系統發出了page fault的信號。(Page Fault)

操作系統又醒了。因為物理內存已經被pages塞滿,操作系統踢掉了一個最不常用的page,將./hello文件的內容拷貝到了新騰出來的位置,然後切換回了hello進程。(Memory Management)

Hello進程重新執行了一遍剛剛失敗的指令,這次CPU找到了物理地址。幾條指令後,"hello world"字元串已經被放到內存里的參數位置。Hello進程call了printf函數,這是一個系統庫的函數,早就被載入在物理內存,於是CPU跳到了printf的位置開始執行。(Shared Library)

Printf將字元串翻譯成了字模信息,傳送給了顯卡,顯卡將信息傳送給了LCD。

LCD的電場開始變化,液晶分子開始扭曲,折射率開始變化,「Hello World」出現了。---


這個問題需要本星球上的各類量子力學專家,核物理專家,高能物理專家,電子專家,電路設計專家,設計CPU專家,邏輯學專家,數學家,演算法理論專家,程序專家,人機交互專家,視覺理論專家,腦科學專家,神經學專家,社會學家,人類學家,以及各種研究宇宙現象和存在意義的哲學家們一起努力上個三五年的,給你找(寫)上幾百本基礎的,中級的,高級的書,以及一堆可能誰也看不懂的膜宇宙理論,多維宇宙理論,或者平行宇宙理論,然後再聯合本宇宙和各個宇宙大牛,先解決掉溝通問題之後,討論上個三五個宇宙紀元。

大概能夠給出一個答案。

「42」


你搞清楚之後會覺得非常無聊

不過

純粹以hello world 來說的話

需要搞清楚的其實是

誰擁有顯示(輸出)裝置:

顯示卡

某一塊跟顯示卡說好的解析度那麼大的記憶體(內存)好像還要乘一個 1677216 個bit?(不管在哪裏)

誰擁有在這塊內存上噴灑座位圖的權力

(就當他是 CPU 吧)

誰擁有CPU?

(一般就是 OS 中廣義的繪圖模組了)

誰算好這個上面畫著"hello world"的圖去拜託繪圖模組去跟 OS 請求把我把圖噴上去?

不一定

有時候是畫好了噴

有時候是跟繪圖模組說:

「只是文字而已你看著辦吧」

(於是有了 freetype 有了各種噴字體的方案)

(因為我只有在 Linux 上幹過這麼低級的事所以我只知道 freetype,但據我所知大家都是差不多作法)

那怎麼跟繪圖模組說交代這件事呢??

就是C++編譯器看到了

main 裡面有個

std::cout

編譯器說

「std」什麼的我家裡當然都有

在iostream裡面寫著呢

照它什麼的說明

我得先拿一塊內存

把這行字塞進去

然後跟 OS 裏頭的 libc++ 說(好像是先跟 OS 要 interrupt ?)

等您有空了幫我去拜託負責繪圖的老師把這個圖給噴到輸出裝置上?

有沒有一種申請開公司留學辦護照辦簽證的感覺??

就跟你說很無聊吧??

打掃房子的事交給阿姨

辦簽證的事儘量交給旅行社

人生有很多其他有趣的事情

不用什麼事情都事必躬親??


原子:原子核對電子的束縛不夠,導致電子逃逸。

電子:逃逸的電子在合適的電勢下,不停地向低電勢處運動。

電路:不停運動的電子通過導線的橫截面,產生足夠的電流。可以產生反方向的電流,再利用一個電阻和電容,最終形成一個LC振蕩電路,用來傳遞信息。

內存:LC的每次震蕩,都可以對內存進行充電、放電,可以寫入、讀取信息。

CPU:根據預設的指令,不停地從內存中讀取信息,進行「+-*/%|~!」等對信息進行計算。還可以把這些計算的結果信息寫入內存。

程序:編寫一些指令,讓這些指令來計算我們覺得感興趣的信息:女神的照片、老師的課程。

哲學:別鬧了,其實你看的這個世界是虛擬的,你覺得你知道的東西只是別人設定好的指令讓你覺得你覺得你知道。


C++輸出hello world,請從電子電路、內存、CPU、程序層面解釋一下?

此問題是我3年前提出的,我現在已經可以解答。對此問題答案感興趣的知友歡迎關注我的專欄,尤其是《自製8位計算機》,僅這一個專欄就可以把我之前所有的疑問給解答了。

自製8位計算機

硅之美

大宗師 (另外特別解釋一下「大宗師」的含義,「宗」指敬仰、尊崇,「大宗師」意思是最值得敬仰、尊崇的老師。誰夠得上稱作這樣的老師呢?那就是「道」。換句話,也就是說,大宗師 = 道,我沒有稱自己是「大宗師」,更不會自稱為「道」。一個知友因為自己不理解「大宗師」的含義而對我開嘲諷模式,被我兌了,希望大家能夠理解其意思,避免類似誤會發生。參考鏈接:莊子·大宗師全文_原文|翻譯_古詩文網)

自製簡易OS

自製編譯器

另外,對於那些建議我讀《深入理解計算機系統》的知友,謝謝你們的回復。其實我在3年前提此問題之前,已經認真讀過了此書了,我確信裡面沒有我想要的答案,才來知乎提問的,依然謝謝各位的熱心解答。

有知友說學習完數字電路、模擬電路、計算機體系結構、彙編就可以回答這些問題,我的回答是,還不行。為什麼這麼肯定呢?因為他們說的這些我都學了。

看我的最高贊回答:

知乎用戶:程序員對計算機背後的運行機制的掌握是不是很有必要?

下面是我之前學習所做的筆記:

課程及筆記分享(一):CMU和ETHZ的《計算機體系結構》

課程及筆記分享(二):國防科技大學《模擬電子技術基礎》

課程及筆記分享(三):國防科技大學《數字電子技術基礎》

課程及筆記分享(四):南京大學《計算機系統基礎》(上)

課程及筆記分享(五):清華大學《計算機組成原理》

課程及筆記分享(六):中國科技大學《編譯原理》

課程及筆記分享(七):蘇黎世聯邦理工學院(ethz)《數字電路設計》

彙編語言的我沒有做筆記,但我用的書是王爽的《彙編語言》那本書。

PS:為什麼大家覺得勵志啊?我一點也沒有覺得勵志,就是之前的問題弄明白了,花了比較長的時間而已,真沒有覺得勵志。


自己去讀CSAPP第一章


看過編碼沒?看了你就懂了


看看計算機組成原理不就清楚了么。。。參見《計算機組成-結構化方法》這本書。。。從電路到彙編都有描述。。。還要電路以下去看模電數電,要看彙編以上去看編譯原理


未必能訪問G+,所以直接貼在這個。但是的但是,這個說的不是運行一個程序,而是使用google搜索。我回頭補齊題主的提問相關的東西。

原始鏈接Dizzying but invisible depth

You just went to the Google home page.

Simple,…

Dizzying but invisible depth

You just went to the Google home page.

Simple, isn"t it?

What just actually happened?

Well, when you know a bit of about how browsers work, it"s not quite that simple. You"ve just put into play HTTP, HTML, CSS, ECMAscript, and more. Those are actually such incredibly complex technologies that they"ll make any engineer dizzy if they think about them too much, and such that no single company can deal with that entire complexity.

Let"s simplify.

You just connected your computer to www.google.com.

Simple, isn"t it?

What just actually happened?

Well, when you know a bit about how networks work, it"s not quite that simple. You"ve just put into play DNS, TCP, UDP, IP, Wifi, Ethernet, DOCSIS, OC, SONET, and more. Those are actually such incredibly complex technologies that they"ll make any engineer dizzy if they think about them too much, and such that no single company can deal with that entire complexity.

Let"s simplify.

You just typed www.google.com in the location bar of your browser.

Simple, isn"t it?

What just actually happened?

Well, when you know a bit about how operating systems work, it"s not quite that simple. You"ve just put into play a kernel, a USB host stack, an input dispatcher, an event handler, a font hinter, a sub-pixel rasterizer, a windowing system, a graphics driver, and more, all of those written in high-level languages that get processed by compilers, linkers, optimizers, interpreters, and more. Those are actually such incredibly complex technologies that they"ll make any engineer dizzy if they think about them too much, and such that no single company can deal with that entire complexity.

Let"s simplify.

You just pressed a key on your keyboard.

Simple, isn"t it?

What just actually happened?

Well, when you know about bit about how input peripherals work, it"s not quite that simple. You"ve just put into play a power regulator, a debouncer, an input multiplexer, a USB device stack, a USB hub stack, all of that implemented in a single chip. That chip is built around thinly sliced wafers of highly purified single-crystal silicon ingot, doped with minute quantities of other atoms that are blasted into the crystal structure, interconnected with multiple layers of aluminum or copper, that are deposited according to patterns of high-energy ultraviolet light that are focused to a precision of a fraction of a micron, connected to the outside world via thin gold wires, all inside a packaging made of a dimensionally and thermally stable resin. The doping patterns and the interconnects implement transistors, which are grouped together to create logic gates. In some parts of the chip, logic gates are combined to create arithmetic and bitwise functions, which are combined to create an ALU. In another part of the chip, logic gates are combined into bistable loops, which are lined up into rows, which are combined with selectors to create a register bank. In another part of the chip, logic gates are combined into bus controllers and instruction decoders and microcode to create an execution scheduler. In another part of the chip, they"re combined into address and data multiplexers and timing circuitry to create a memory controller. There"s even more. Those are actually such incredibly complex technologies that they"ll make any engineer dizzy if they think about them too much, and such that no single company can deal with that entire complexity.

Can we simplify further?

In fact, very scarily, no, we can"t. We can barely comprehend the complexity of a single chip in a computer keyboard, and yet there"s no simpler level. The next step takes us to the software that is used to design the chip"s logic, and that software itself has a level of complexity that requires to go back to the top of the loop.

Today"s computers are so complex that they can only be designed and manufactured with slightly less complex computers. In turn the computers used for the design and manufacture are so complex that they themselves can only be designed and manufactured with slightly less complex computers. You"d have to go through many such loops to get back to a level that could possibly be re-built from scratch.

Once you start to understand how our modern devices work and how they"re created, it"s impossible to not be dizzy about the depth of everything that"s involved, and to not be in awe about the fact that they work at all, when Murphy"s law says that they simply shouldn"t possibly work.

For non-technologists, this is all a black box. That is a great success of technology: all those layers of complexity are entirely hidden and people can use them without even knowing that they exist at all. That is the reason why many people can find computers so frustrating to use: there are so many things that can possibly go wrong that some of them inevitably will, but the complexity goes so deep that it"s impossible for most users to be able to do anything about any error.

That is also why it"s so hard for technologists and non-technologists to communicate together: technologists know too much about too many layers and non-technologists know too little about too few layers to be able to establish effective direct communication. The gap is so large that it"s not even possible any more to have a single person be an intermediate between those two groups, and that"s why e.g. we end up with those convoluted technical support call centers and their multiple tiers. Without such deep support structures, you end up with the frustrating situation that we see when end users have access to a bug database that is directly used by engineers: neither the end users nor the engineers get the information that they need to accomplish their goals.

That is why the mainstream press and the general population has talked so much about Steve Jobs" death and comparatively so little about Dennis Ritchie"s: Steve"s influence was at a layer that most people could see, while Dennis" was much deeper. On the one hand, I can imagine where the computing world would be without the work that Jobs did and the people he inspired: probably a bit less shiny, a bit more beige, a bit more square. Deep inside, though, our devices would still work the same way and do the same things. On the other hand, I literally can"t imagine where the computing world would be without the work that Ritchie did and the people he inspired. By the mid 80s, Ritchie"s influence had taken over, and even back then very little remained of the pre-Ritchie world.

Finally, last but not least, that is why our patent system is broken: technology has done such an amazing job at hiding its complexity that the people regulating and running the patent system are barely even aware of the complexity of what they"re regulating and running. That"s the ultimate bikeshedding: just like the proverbial discussions in the town hall about a nuclear power plant end up being about the paint color for the plant"s bike shed, the patent discussions about modern computing systems end up being about screen sizes and icon ordering, because in both cases those are the only aspect that the people involved in the discussion are capable of discussing, even though they are irrelevant to the actual function of the overall system being discussed.


我就說一下最後的哲學原理吧,就是1和0的組合,和這個世界一樣,陰陽兩極生四象,四象生八卦,產生各種基本粒子。然後你就知道了,高中物理及格的話。


csapp,這本書能給你所有的答案


參見CSAPP第一章


能帶理論 -&> PN節 -&> MOSFET -&> 半導體製造 -&> 門電路 -&>振蕩電路-&> 處理器 -&> 指令集 -&> 機器碼 -&> 操作系統 -&> 編譯原理 - &> 彙編 -&> C

CPU &<-&> 南橋北橋 &<-&> IO匯流排/PCI匯流排 -&> 內存,硬碟, 顯卡 -&>顯示器


其實挺有意思的問題,看你想要多詳細的答案。這個問題本身就比較有哲學的味道。

雖然人造了計算機,但已經複雜到很少有一個人完全理解。從硬體上按鍵產生電容變化,轉化成計算機輸入,最後到顯卡把顯存里的內容映射到屏幕上。

就我了解的一些細節,說是這樣的:

1 輸入。電容式鍵盤每敲擊一下,會產生電容變化,這種變化經模數轉換成數字信號,硬體上連到主機的一個的介面。這個硬體上的介面對應到主板上的一小塊信號緩存區,新的信號會產生一個硬體中斷上。驅動程序會響應這個中斷,並把緩存區的信號翻譯成你敲的內容。編輯器不斷的從操作系統中讀入字元。

2編譯。這個很多編譯原理都會講到。無非是先預處理,再加個頭,並鏈接到標準庫。

3執行。操作系統會把程序讀到內存,並把執行地址寄存器指向這片內存。CPU會依次讀取這片內存的指令並執行。printf這個函數會把每個字元從數據內存區放到寄存器里。如果是在dos平台里,printf應該去調用20號系統中斷,也就是dos的一個子程序。這個子程序負責把字元放到顯存里。顯卡的會把顯存里的內容顯示到顯示器上。


不需要這麼複雜的解釋吧,人能聽懂麽問題是?大概就是C++的編譯器轉換成計算機能看懂的二進位代碼,假如遇到1代表開電路,0代表關電路,或者說高電壓和低電壓,然後經過顯卡,顯示屏處理,你就看到了輸出的hello world


把這些說清楚,都可以造機器了哦,這裡有很多工作。

可以看看這個,Homebrew CPU Home Page,大神用74LS,自己造的機器。


C++輸出hello world,請從電子電路、內存CPU、程序層面解釋一下

C++並不是很底層,拿C說吧,下面這段C代碼可以在C++中編譯

#include &

int main()

{

printf("hello, world");

return 0;

}

首先是編譯,編譯分為很多個步驟,有預處理 語法分析 編譯 彙編 連接 還有編譯過程中的優化,編譯後的優化等,略

然後出來的程序是01010101010010101010101001010............

好了,這個程序才是處理器能認出來的,高中數學講的程序非常模糊以至於老師很容易講錯,別被誤導

然後運行嘛,處理器是這樣看待這段程序的:

在內存里xxx地址,有一串字元,hello,world

這段字元串的長度已經被算出來了

然後從xxx地址的第一位讀取,輸出每一個字元,那個長度就是計數用的

程序層面跟處理器層面差不多,事實上處理器里就是電路,下面說道電路時其實處理器的原理也是一樣的

首先處理器是二進位運算的,0/1運算,有與或非三個基本運算,這三個電路高中物理會講

然後處理器有大量的指令,處理器里一部分指令的集合叫指令集

如今的x86處理器就有多種指令集

一個處理器會有一些基本指令,可以參考RISC(比如ARM、MIPS、Alpha)(精簡指令集)

CISC(複雜指令集)的代表就是x86

這個RISC里的基本指令是怎樣的呢,每一個指令都執行一個操作,對於處理器來說非常基本,類似於3+4=(1+1+1)+(1+1+1+1)=1+1+1+1+1+1+1=7一樣

每一個基本指令都會有一些電路操作被設計好,這裡我沒學過

一個計算機的處理器應當是圖靈完全的,即 運算 輸入輸出 跳轉 等操作

這些都是指令能實現的

處理器有緩存,在CPU內部的有一級緩存 二級緩存,外部的有內存

CPU內部的用寄存器管理,這是指令可以直接操作的,彙編里可以學到。外部的內存比較大,所以使用內存地址進行管理的

Hello World程序被操作系統載入內存,由操作系統的函數調用,引導處理器來執行程序里的代碼(二進位代碼),這些代碼是由指令組成的,稱為機器語言。機器語言和彙編語言是可以互相翻譯的。

字元串被輸出,是要輸出到一個叫緩衝區的設備上(linux設備,Windows下不造),然後由操作系統輸出到控制台上,而操作系統可以支配顯卡,從而輸出

為什麼我提到多次操作系統卻沒說呢?這玩意太複雜。


有!

10年前高三畢業我也有這個疑問。今天剛好是ECE博士答辯。

想了想,這些年一共學了半導體物理,模擬電路,數字電路,VLSI,晶元實現,計算機體系結構,編譯原理,操作系統,嵌入式C,C++

要是當年直接從C++往上層學就好了。


推薦閱讀:

如何在一個月內提高C++水平?
如何說服同學在寫C++程序的時候用cstdio而不是stdio.h?
當你寫幾萬行代碼時,你在寫什麼?
有什麼現有函數,輸入是一個單詞變式的字元串,如複數,過去式,現在進行時等,輸出這個單詞的原型?
如何評價開學季知乎上大量出現C/C++新手問作業題?

TAG:中央處理器CPU | 編程 | C | 視角 |