2023年全國碩士研究生考試考研英語一試題真題(含答案詳解+作文范文)_第1頁
已閱讀1頁,還剩40頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Professional Programming,-------Secrets behind the code,TA:毛先領(lǐng),上機(jī)時常問的問題,為什么要先點擊“編譯”,再“運行”?為什么局部變量和全局變量有不同的生命周期?(或為什么這個變量只能在這個函數(shù)內(nèi)引用?)為什么VB不用編譯直接運行即可?為什么我的程序編譯不了?… … …. … …為什么… …,編程不僅僅是只知道語法規(guī)則,必須了解程序背后的秘密 ----計算機(jī)的系

2、統(tǒng)結(jié)構(gòu) ----計算機(jī)程序的運行方式 ----編譯原理 ………………….,Niubility!,outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,程序的編譯流程,.c文件經(jīng)過預(yù)處理(gcc 時帶上 -E選項)產(chǎn)生.i文件……&

3、#160;      預(yù)處理        編譯          匯編            

4、0; 連接.c------------>.i------------>.s------------>.o------------>a.out       -E             -S    &

5、#160;       -c,Pre-processor(cpp),,hello.i,Compiler(cc1),,hello.s,Assembler(as),,hello.o,Linker(ld),,hello,,hello.c,Sourceprogram(text),Modifiedsourceprogram(text),Assemblypr

6、ogram(text),Relocatableobjectprograms(binary),Executableobjectprogram(binary),,printf.o,A Example,源程序,1、預(yù)處理,預(yù)處理:使用-E參數(shù)輸出文件的后綴為“.cpp”gcc –E –o gcctest.cpp gcctest.c使用wc命令比較預(yù)處理后的文件與源文件,可以看到兩個文件的差異,,,行數(shù) 單詞數(shù) 字節(jié)數(shù)

7、,預(yù)編譯,,,,預(yù)處理文件?匯編代碼1)使用-x參數(shù)說明根據(jù)指定的步驟進(jìn)行工作,cpp-output指明從預(yù)處理得到的文件開始編譯2)使用-S說明生成匯編代碼后停止工作gcc –x cpp-output –S –o gcctest.s gcctest.cpp也可以直接編譯到匯編代碼gcc –S gcctest.c,2、編譯成匯編代碼,,,預(yù)處理文件?匯編代碼,直接編譯到匯編代碼,3、編譯成目標(biāo)代碼,匯編代碼?目標(biāo)代碼g

8、cc –x assembler –c gcctest.s直接編譯成目標(biāo)代碼gcc –c gcctest.c使用匯編器生成目標(biāo)代碼as –o gcctest.o gcctest.s,,,匯編代碼?目標(biāo)代碼,直接編譯成目標(biāo)代碼,使用匯編器,4、編譯成執(zhí)行代碼,目標(biāo)代碼?執(zhí)行代碼gcc –o gcctest gcctest.o直接生成執(zhí)行代碼gcc –o gcctest gcctest.c,,,目標(biāo)代碼?執(zhí)行代碼,直

9、接生成執(zhí)行代碼,outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,程序與計算機(jī)體系結(jié)構(gòu),圖靈機(jī) 磁帶、控制器以及讀寫磁頭三部分組成,,Registers,On-chip L1cache (SRAM),Main memory(DRAM),Local secondary storage(local disks),,,,,Larger, slower,

10、and cheaper (per byte)storagedevices,,Remote secondary storage(distributed file systems, Web servers),,Off-chip L2cache (SRAM),CPU registers hold words retrieved from cache memory.,,L0:,L1:,L2:,L3:,L4:,L5:,Smaller

11、,faster,and costlier(per byte)storage devices,,Mainmemory,,I/O bridge,,Bus interface,,,,,,,,ALU,Register file,,,CPU,System bus,,Memory bus,,,,Disk controller,,Graphicsadapter,,USBcontroller,,,Mouse,Keyboard,,D

12、isplay,,Disk,,,,,I/O bus,,,,,Expansion slots forother devices suchas network adapters,hello executable stored on disk,PC,Mainmemory,,I/O bridge,,Bus interface,,,,,,,,ALU,Register file,,,CPU,System bus,,Memory bus,,,

13、,Disk controller,,Graphicsadapter,,USBcontroller,,,Mouse,Keyboard,,Display,,Disk,,,,,I/O bus,,,,,Expansion slots forother devices suchas network adapters,PC,,,,,,,,,"hello",Usertypes"hello",Mai

14、nmemory,,I/O bridge,,Bus interface,,,,,,,,ALU,Register file,,,CPU,System bus,,Memory bus,,,,Disk controller,,Graphicsadapter,,USBcontroller,,,Mouse,Keyboard,,Display,,Disk,,,,,I/O bus,,,,,Expansion slots forother d

15、evices suchas network adapters,hello executable stored on disk,PC,,,,,hello code,"hello,world\n",outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,程序的運行方式,以匯編代碼為例 gcc –S gcctest.s gcctest.c,.fi

16、le "gcctest.c" .section .rodata.LC0: .string "Hello World!".LC1: .string "i=j+1=%d\n" .text.globl main .type main, @functionmain:

17、 leal 4(%esp), %ecx andl $-16, %esp pushl -4(%ecx) pushl %ebp movl %esp, %ebp pushl %ecx subl $36, %esp movl $0, -12(%ebp) movl $0

18、, -8(%ebp) movl -8(%ebp), %eax addl $1, %eax movl %eax, -12(%ebp) movl $.LC0, (%esp) call puts movl -12(%ebp), %eax movl %eax, 4(%esp)

19、movl $.LC1, (%esp) call printf movl $0, %eax addl $36, %esp popl %ecx popl %ebp leal -4(%ecx), %esp ret .size main, .-main .i

20、dent "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)" .section .note.GNU-stack,"",@progbits,Kernel virtual memory,Memory mapped region forshared libraries,,Run-time heap(created at runtime

21、 by malloc),,,User stack(created at runtime),,,Unused,0,Memoryinvisible touser code,,0xc0000000,0x08048000,0x40000000,,Read/write data,Read-only code and data,,Loaded from the hello executable file,printf() function,

22、0xffffffff,outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,程序的調(diào)試(GDB),Gcc 編譯代碼時必須加上-g選項,同時也可以打開-Wall選項查看所有的警告信息,gcc的高級選項,-Wall:打開所有的警告信息,,根據(jù)警告信息檢查源程序,,Main函數(shù)的返回值為int,在函數(shù)的末尾應(yīng)當(dāng)返回一個值,修改源程序,,gdb,Gdb = GNU debuge

23、rGNU tools中的調(diào)試器,功能強(qiáng)大設(shè)置斷點監(jiān)視、修改變量單步執(zhí)行顯示/修改寄存器的值堆棧查看遠(yuǎn)程調(diào)試,gdb使用舉例,源代碼如下,編譯:gcc –o bug bug.c,編譯并運行,,????,編譯,,,使用gdb調(diào)試bug,運行bug,輸入字符串hello,顯示出錯位置,能不能看到源代碼呢?,,,,,,使用gcc的-g參數(shù),gcc –g –o bug bug.c重新調(diào)試,列出源代碼,,,,,?怎么修改前面的源代

24、碼呢?,,設(shè)置斷點,,,outline,程序的編譯流程程序與計算機(jī)體系結(jié)構(gòu)程序的運行方式程序的調(diào)試(GDB)POJ 例子Q&A,POJ 2750雞兔同籠,Description一個籠子里面關(guān)了雞和兔子(雞有2只腳,兔子有4只腳,沒有例外)。已經(jīng)知道了籠子里面腳的總數(shù)a,問籠子里面至少有多少只動物,至多有多少只動物Input第1行是測試數(shù)據(jù)的組數(shù)n,后面跟著n行輸入。每組測試數(shù)據(jù)占1行,每行一個正整數(shù)a (a

25、< 32768)Output輸出包含n行,每行對應(yīng)一個輸入,包含兩個正整數(shù),第一個是最少的動物數(shù),第二個是最多的動物數(shù),兩個正整數(shù)用一個空格分開如果沒有滿足要求的答案,則輸出兩個0。Sample Input2320Sample Output0 05 10,#include using namespace std;int main(){ int nlines; cin >

26、> nlines; while (nlines--) { int legs; cin >> legs; if (legs % 2 == 1) cout << "0 0" << endl; else if (legs % 4 == 0) cout <&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論