Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
系統程式 第 4 章 -- 組譯器
Search
陳鍾誠
April 14, 2022
Education
650
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
系統程式 第 4 章 -- 組譯器
陳鍾誠
April 14, 2022
More Decks by 陳鍾誠
See All by 陳鍾誠
第 6 章、巨集處理器
ccckmit
0
170
第 7 章、高階語言
ccckmit
0
240
第 9 章、虛擬機器
ccckmit
0
160
第 8 章、編譯器
ccckmit
0
300
數學、程式和機器
ccckmit
2
890
語言處理技術
ccckmit
0
260
微積分
ccckmit
1
600
系統程式 第 1 章 -- 系統軟體
ccckmit
0
640
系統程式 第 2 章 -- 電腦的硬體結構
ccckmit
0
610
Other Decks in Education
See All in Education
[2026前期火5] 論理学(京都大学文学部 前期 第14回)「計算は、証明ではない——ハルシネーションを三層ハーモニーで診る」
yatabe
0
260
Tableau Public入門ハンズオン|Titanicデータで学ぶViz作成とMakeover
hayashi_ds017
0
130
Fundamentos, Caracteristicas y Aplicaciones de los Modulos NumPy , Matplotlib y Pandas
robintux
0
250
観察、仮説、実行、検証、計画、提案を一年で3000回トレーニングする方法/3000 Thinking Loops in 365 Days
moriyuya
5
1.3k
Del ojo entrenado a la visión computacional: evaluación de la severidad de enfermedades
emdelponte
0
130
セキュリティ担当がいない中小医療機関を院外のRISSはどう支えるべきか / How External RISS Can Help Small Medical Institutions
eller86
1
200
20260908_ターゲットとスコープを決める発表法
masakiokuda
1
110
!コスパよくインターンに受かる方法!
ruribou
1
360
Examen de Selectividad. Geografía julio 2026 (Convocatoria Extraordinaria). UCLM
juanmartin2026
1
13k
NDIAS Automotive / IoT CTF 2026 Recap - Keyfob & OSINT
himitu23
0
340
Soluciones al examen de Geografía 2026. JUNIO (Convocatoria Ordinaria)
juanmartin2026
1
10k
AI Generated Beer
lonbrew
0
120
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
650
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
520
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
The Language of Interfaces
destraynor
162
27k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
How STYLIGHT went responsive
nonsquared
100
6.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
450
Abbi's Birthday
coloredviolet
4
10k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
The agentic SEO stack - context over prompts
schlessera
0
940
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Tell your own story through comics
letsgokoyo
1
1.1k
Transcript
第 4 章、組譯器 作者:陳鍾誠 旗標出版社
第 4 章、組譯器 4.1 組譯器簡介 4.2 組譯器的演算法
4.3 完整的組譯範例 4.4 實務案例:處理器 IA32 上的 GNU 組譯器
4.1 組譯器簡介 組譯器乃是將組合語言轉換為機器碼的工具。 組合語言 (組譯器) 目的檔
(或執行檔) 組譯器是組合語言程式師所使用的主要工具。
組譯器的過程示意圖 MUL R5, R2, R2 ADD R1, R2, R5 ADD
R2, R4, R2 15 52 20 00 13 12 50 00 13 24 20 00 組譯器 Assembler 組合語言 目的檔 圖 4.1 組譯器的過程示意圖
簡單的組合語言程式
組譯:將組合語言轉譯成目的碼
組譯的原理 (絕對定址)
組譯的原理 (相對於 PC 定址)
二階段的組譯方式 (1). 運算元轉換: 將指令名稱轉換為機器語言 例如 LD 轉為00,
ST 轉為01等 (2). 參數轉換: 將暫存器轉為代號,符號轉換成機器位址 例如 R1 轉為 1, A 轉為 000C,B 轉為 0010 等。 (3). 資料轉換: 將原始程式當中的資料常數轉換為內部的機器碼 例如 29 轉換為 001D。 (4). 目的碼產生: 根據指令格式, 轉換成目的碼, 輸出到目的檔中。 (1) (2) (3) (2)
映像檔 映像檔:最簡單的目的碼 組合語言 (組譯器) 映像檔
as0 組譯器
4.2 組譯器的演算法 第一階段 計算符號位址 第二階段 組譯指令與資料
資料結構:符號表
第一階段 (計算符號位址) 1. 決定每一個指令所佔記憶空間 的大小 例如 決定
WORD、RESW 等指令所定 義的資料長度 決定 LD、ST 等指令所佔空間的大 小 2. 計算出程式當中每一行的位址。 3. 儲存每一個標籤與變數的位址 例如 變數 A 為 0x000C 變數 B 為 0x0010
第二階段 (組譯指令與資料) (1). 轉換指令OP欄位為機器碼 例如 LD 轉換為
00 ST 轉換為 01 (2). 轉換指令參數為機器碼 例如 R1轉換為 1 B 轉換為 000C (3). 轉換資料定義指令為位元值 例如 WORD B 29 轉為 0000001D (4). 產生目的碼並輸出到目的檔當 中 (1) (2) (2) (3)
組譯器的資料結構 運算碼表 (Op Table) 儲存指令與代碼的配對表 通常使用雜湊表格
符號表 (Symbol Table) 儲存符號與位址的配對表 通常使用雜湊表格
第一階段 (PASS1) 計算位址
第二階段 (PASS2) – 主程式 指令轉為機器碼
第二階段 (PASS2) – 指令轉換函數 根據指令類型 L 型
A 型 J 型 轉換成該型編碼
4.3 完整的組譯範例 範例:陣列加總功能 功能 如右圖 C 語言所示
原始程式 如範例 4.7 所示 組譯結果 如範例 4.8 所示
原始程式
組譯結果 組譯報表 範例 4.8 符號表 圖
4.6 目的檔 範例 4.9
4.4 實務案例: 處理器 IA32 上的 GNU 組譯器 as :
GNU 的組譯器 -a 參數 讓 GNU 的 as 組譯器 產生組譯報表檔
GNU 的 組譯報表 指令長度不固定 範例 addl %eax, sum 010500000000
addl $1, %eax 83C001 符號表
結語 對 IA32 的編碼方式有興趣的讀者,可以參考 Kip Irvine 的組合語言一書
習題 1. 請說明組譯器的輸入、輸出與功能為何? 2. 請說明組譯器第一階段 (PASS1) 的功能為何? 3. 請說明組譯器第二階段 (PASS2)
的功能為何? 4. 請說明組譯器當中的符號表之用途為何? 5. 請說明組譯器當中的指令表之用途為何? 6. 請仿照範例 4.4,使用本書第12章所實作的 as0 組譯器,組譯 Ex4_1.asm0 組合語 言檔,並仔細觀察其輸出結果。 7. 請閱讀本書第12章所附的 Assembler.c 與 Assembler.h 等C語言程式,並且對照本 章的演算法,以學習 CPU0 組譯器的實作方式。 8. 請按照4.4節的方法,操作 GNU 工具對組合語言進行組譯動作,並檢視組譯報表, 找出各個符號的位址。 9. 請於 http://kipirvine.com/asm/ 網站下載Kip Irvine 書籍組合語言程式範例,並以 Visual Studio 進行組譯與執行。