编程模型 a Kernighan and Ritchie: C Programming language ode/intro/hello.c 1 #include <stdio.h> 3 int main() printf ("hello, world\n") code/intro/hello. c 正i Igure l I: The hello program. ASCll code( text characters) #1 d <SD> td⊥ 3510511099108117100101326011511610010511146 h An \n 1 n t <sp> m a n 104 10101051101163210997105110404110123 \n <sp> <sp> <sp> <sp> p r 1 n t f 1 10323232321121141051101161024034104101108 1 <8p> w d 10811144321191111141081009211034415910125 Figure 1.2: The AsCII text representation of hello.c A tutor on coding, Sep 9, 2011
11 A tutor on coding , Sep 9, 2011 编程模型 Kernighan and Ritchie: C Programming language ASCII code ( text characters)
编程模型 代码编译 tf. o hello. c hello.i compiler hello.assembler hello.o linker hello processor (1d) source modified assemb relocatable executable source object object (text) program ( text) programs program (binary) Figure 1.3: The compilation system 为什么需要了解编峄糸统 代码优化:例如 switch语句是否总是比 if-then-else有效?函数调用的代价 是多高?Whil循环是否总比do循环有效?等等问題,涉及编译器,机器 语言 理解链接错误信息;Iink- time errors,例如静态库与动态库的区别?库的 次序对链接的影响? 避免安全漏:例如缓冲区谥出,堆、栈的管理,参数传递及返回等 A tutor on coding, Sep 9, 2011 12
12 A tutor on coding , Sep 9, 2011 编程模型 代码编译 为什么需要了解编译系统 ⚫ 代码优化:例如switch语句是否总是比if-then-else有效?函数调用的代价 是多高?While循环是否总比do循环有效?等等问题,涉及编译器,机器 语言 ⚫ 理解链接错误信息:link-time errors,例如静态库与动态库的区别?库的 次序对链接的影响? ⚫ 避免安全漏洞:例如缓冲区溢出,堆、栈的管理,参数传递及返回等
编程模型 ■程序的执行 CPU register file ALU system bus memory bus Memory Interface bridge 1/O bus Expansion slots for other devices such USB graphIcs as network adapters adapter controller display hello executable disk stored on disk Figure 1.4: Hardware organization of a typical system. CPU: Central Processing Unit, ALU: Arith metic/Logic Unit. PC: Program counter. USB: Universal serial bus A tutor on coding, Sep 9, 2011 13
13 A tutor on coding , Sep 9, 2011 编程模型 程序的执行
编程模型 ■程序的执行 CPU register file ALU system bus memory bus main"hello Memory Intl memory l/o bus Expansion slots for other devices such graphics contro adapter controller mouse keyboa display disk Figure 1.5: Reading the hello command from the keyboard A tutor on coding, Sep 9, 2011 14
14 A tutor on coding , Sep 9, 2011 编程模型 程序的执行
编程模型 ■程序的执行 CPU register file em bu memory bus main""hello,worldIn Memory Interface memory he1l。code 1/O bus Expansion slots for other devices such graphic as network adapters controller mouse keyboard display hello executable Figure 1.6: Loading the executable from disk into main memory A tutor on coding, Sep 9, 2011 15
15 A tutor on coding , Sep 9, 2011 编程模型 程序的执行