




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、機(jī)器組織與匯編語言設(shè)計(jì)第1頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四前言課程由來基本內(nèi)容教材和參考資料課程的管理要求A traditional first program for warm-up第2頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四課程的由來2002年12月,接受講匯編課程任務(wù)接收Prof.Dai的ppt,教材2002年12和2003年1月為確定教材找其他院校開設(shè)匯編課程的資料瀏覽國(guó)內(nèi)匯編方面書籍通過搜索引擎查找2003年2月確定教材2004年2月重新確定教材第3頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四The power of assembl
2、y programming引自(插曲)Identity of the bloated software Independence from GLIBC Requirement of exit function How to communicate with system calls in assembly? Brief anatomy of Executable and Linking Format (ELF) 第4頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四GNU第5頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四Linus Torvalds第6頁,共30頁,2022年,
3、5月20日,2點(diǎn)20分,星期四1. Identity of the “bloated software”Test0.cint main() return(123); Compile and link$gcc o test0 test0.c$ls l test0-rwxr-xr-x 1 root src 4552 Jan 6 20:38 test0 $ ./test0; echo $?123第7頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四1.1 Examine library dependency & symbol name definitions$ldd test0libc.so.
4、6 = /lib/tls/libc.so.6 (0 xb74a0000)/lib/ld-linux.so.2 = /lib/ld-linux.so.2 (0 xb75eb000)$nm test008048320 t _do_global_dtors_aux w _gmon_start_ U _libc_start_mainGLIBC_2.0080483b0 T main 080494fc b object.11 08049424 d p.3 第8頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四1.2 identity of “bloated software”gcc v o test
5、0 test0.cConfigured with: ./configure -prefix=/usr -mandir=/usr/share/man -infodir=/usr/share/info -enable-shared -enable-threads=posix -disable-checking -with-system-zlib -enable-_cxa_atexit -host=i386-redhat-linuxThread model: posixgcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)cc1 -lang-c -v
6、-D_GNUC_=3 -D_GNUC_MINOR_=2 -D_GNUC_PATCHLEVEL_=3 -D_GXX_ABI_VERSION=102 -D_ELF_ -Dunix -D_gnu_linux_ -Dlinux -D_ELF_ -D_unix_ -D_gnu_linux_ -D_linux_ -D_unix -D_linux -Asystem=posix -D_NO_INLINE_ -D_STDC_HOSTED_=1 -Acpu=i386 -Amachine=i386 -Di386 -D_i386 -D_i386_ -D_tune_i386_ test0.c -quiet -dumpb
7、ase test0.c -version -o /tmp/ccBPEYcR.sGNU CPP version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) (cpplib) (i386 Linux/ELF)GNU C version 3.2.3 20030502 (Red Hat Linux 3.2.3-20) (i386-redhat-linux) compiled by GNU C version 3.2.3 20030502 (Red Hat Linux 3.2.3-20).ignoring nonexistent directory /usr/i386
8、-redhat-linux/include#include . search starts here:#include search starts here: /usr/local/include /usr/includeEnd of search list. as -V -Qy -o /tmp/ccaVmECA.o /tmp/ccBPEYcR.sGNU assembler version .4 (i386-redhat-linux) using BFD version .4 20030523collect2 -eh-frame-hdr -m elf_i386 -dynamic-linker
9、/lib/ld-linux.so.2 -o test0/tmp/ccaVmECA第9頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四2. Independence from GLIBCgcc nostdlib o test0 test.c/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048094nm /usr/lib/crt1.o00000000 D _data_start00000000 W data_start00000000 R _fp_hw00000004 R _IO_stdin_u
10、sed U _libc_csu_fini U _libc_csu_init U _libc_start_main U main00000000 T _start第10頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四2.1 escape from the problemgcc c test0.cld e main o test0 test0.o$ ls -l test0 -rwxr-xr-x 1 root src 989 Jan 6 21:00 test0 $ ldd test0 not a dynamic executable$ nm test0 080490ac A _bss_sta
11、rt080490ac A _edata080490ac A _end08048094 T main$./test0 segment error第11頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四3. Requirement of exit function$ gdb test0GNU gdb Red Hat Linux (5.3.90-0.20030710.40rh)Copyright 2003 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License,
12、and you arewelcome to change it and/or distribute copies of it under certain conditions.Type show copying to see the conditions.There is absolutely no warranty for GDB. Type show warranty for details.This GDB was configured as i386-redhat-linux-gnu.(no debugging symbols found).Using host libthread_d
13、b library /lib/tls/libthread_db.so.1.(gdb) disassemble mainDump of assembler code for function main:0 x08048094 : push %ebp0 x08048095 : mov %esp,%ebp0 x08048097 : sub $0 x8,%esp0 x0804809a : and $0 xfffffff0,%esp0 x0804809d : mov $0 x0,%eax0 x080480a2 : sub %eax,%esp0 x080480a4 : mov $0 x7b,%eax0 x
14、080480a9 : leave 0 x080480aa : ret End of assembler dump.第12頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四3.1 A new version “tetst1.c”Test1.c#include main() exit(123); Compile and link$gcc o test1 test1.c$ls l test1-rwxr-xr-x 1 root src 4640 Jan 6 21:48 test1 $ ./test1; echo $?123第13頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四3.2 De
15、pend on a standard library$gcc nostdlib o test1 test1.c/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048094/tmp/cc27NBFk.o(.text+0 x16): In function main: undefined reference to exitcollect2: ld returned 1 exit status第14頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四4. Communicate with system
16、calls in assemblyxexit.asmbits 32 ; Use 32bit mode.section .text ; Code must resides in the .text in GCC.global xexit ; Declare xexit() as a public function.; void xexit (int status)xexit: mov ebp, esp ; Now, ebp points return address. ; and ebp+4 points 1st argument. mov ebx, ebp + 4 ; Read status
17、code. mov eax, 1 ; Call sys_exit(). int 0 x80 ; Never returns.$nasm f elf xexit.asm第15頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四4.1 Focus the sourceDeclare 32bit mode at the beginng (bits 32; in case of 16bit mode, use bits 16). Use section .text for the code segment (Im afraid whether segment is a right term in
18、this context or not.). Declare xexit as a global function name. If you forget it, you cant call xexit from C source file. The code begins under exit:. Read passed argument into EBX. In C language, caller puts arguments and return address into a stack (Usually, stacking is performed by a unit of word
19、, 32bits). ebp points return address, ebp+4 points first argument, ebp+8 points second argument, and so on. Set one in EAX. This is a number of exit system call. UNIX kernel supports many system calls for a communication with process. You can see all of supported system calls in /usr/include/asm/uni
20、std.h. Finally, execute exit system call by software interrupt 128 (0 x80). This is the essence of system call formula in assembly. It requires only 4 instructions! 第16頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四4.2 /usr/include/asm/unistd.h#ifndef _ASM_I386_UNISTD_H_#define _ASM_I386_UNISTD_H_/* * This file contai
21、ns the system call numbers. */#define _NR_exit 1#define _NR_fork 2#define _NR_read 3#define _NR_write 4#define _NR_open 5#define _NR_close 6#define _NR_waitpid 7#define _NR_creat 8#define _NR_link 9#define _NR_unlink 10#define _NR_execve 11#define _NR_chdir 12第17頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四4.3 A new
22、 version “tetst2.c”Test2.cextern void xexit(int);main() xexit(123); Compile and link$gcc c test2.c$ld e main o test2 test2.o xexit.o$ls l test2-rwxr-xr-x 1 root src 954 Jan 6 21:48 test1 $ ./test2; echo $?123第18頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四5. Brief anatomy of Executable and linking Format (ELF)ELF is
23、 a widely used object format in the modern UNIX world. In Linux, recent distributors compile all packages in this format. It is flexible, but quite difficult to understand the structure. $less test2test2 may be a binary file. See it anyway? ?ELFAAABCA4TA4 C(EAEPAFPQtdGDUEC)LjFPDAGCC: (GNU) 3.2.3 200
24、30502 (Red Hat Linux 3.2.3-20)The Netwide Assembler mentESCAF,P!ACDCD,ASAQC_A5AABBGDP CC6ACACBCCCDCECFCGAD DSP_RA$P+P0PAtest2.cxexit.asm_bss_startmain_edata_endxexit第19頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四5.1 strip symbols$strip test2; $ls l test2-rwxr-xr-x 1 root src 546 Jan 6 21:46 test2 $strip -remove-sec
25、tion=.comment -remove-section=.note test2 $ls l test2-rwxr-xr-x 1 root src 432 Jan 6 21:47 test2 $strip -remove-section=.data -remove-section=.sbss -remove-section=.bss test2 $ls l test2-rwxr-xr-x 1 root src 344 Jan 6 21:48 test2 第20頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四基本內(nèi)容By gaining a deeper understanding o
26、f how computers work, the reader can often be much more productive developing software in higher level languages such as C and C+. Learning to program in assembly language is an excellent way to achieve this goal.Learning assembler in Linux environment.第21頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四教材和參考資料Paul A. C
27、arter.Tutorial: PC Assembly Language, November 11, 2003.Randall Hyde. The Art of Assembly Language , 2001.第22頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四課程的管理要求背景知識(shí)要求程序設(shè)計(jì)(C)Web知識(shí) (瀏覽器,HTML,HTTP,MIME,CGI,等)有SE編程經(jīng)驗(yàn)(例如做過去年數(shù)據(jù)結(jié)構(gòu)作業(yè))數(shù)據(jù)結(jié)構(gòu)成績(jī)構(gòu)成課程項(xiàng)目實(shí)踐,50%期末考試,50%課程網(wǎng)站/aoa/第23頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四Systemcalls 除非一個(gè)匯編程序僅僅進(jìn)行
28、數(shù)學(xué)運(yùn)算,否則其必須進(jìn)行輸入輸出及退出等操作,而要進(jìn)行這些操作就需要調(diào)用操作系統(tǒng)的服務(wù)。實(shí)際上,除了使用系統(tǒng)的服務(wù)以外,不同操作系統(tǒng)的匯編編程是很類似的。在UNIX系統(tǒng)下有兩種方式實(shí)現(xiàn)對(duì)系統(tǒng)調(diào)用的使用:通過經(jīng)過封裝的C庫(libc)或者直接調(diào)用。在匯編程序中是否使用libc不僅僅是一個(gè)編程風(fēng)格的問題,而libc封裝用來確保當(dāng)系統(tǒng)調(diào)用接口發(fā)生變化時(shí),無須對(duì)使用libc的程序進(jìn)行修改 第24頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四程序風(fēng)格 當(dāng)前IA-32UNIX都是32bit的操作系統(tǒng),具有以下特點(diǎn):運(yùn)行在保護(hù)模式下,具有“平坦”(flat)內(nèi)存地址空間,使用ELF二進(jìn)制代碼格式。
29、一個(gè)程序可以劃分為不同的段:.text是程序執(zhí)行代碼(只讀),.data是程序的數(shù)據(jù)部分(讀寫),.bss是沒有經(jīng)過初始化的數(shù)據(jù)(read-write);同時(shí)還可以具有其他一些標(biāo)準(zhǔn)的段及用戶自定義段,但是這種情況很少被應(yīng)用。而一個(gè)程序至少具有.text段。 第25頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四A traditional first program for warm-upBrian KernighanDennis Ritchie Ken Thompson UNIX第26頁,共30頁,2022年,5月20日,2點(diǎn)20分,星期四LinuxLinux環(huán)境下的系統(tǒng)調(diào)用是通過int0 x80來實(shí)現(xiàn)的
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 設(shè)定明確的工作優(yōu)先級(jí)計(jì)劃
- 財(cái)務(wù)分析在企業(yè)評(píng)估中的應(yīng)用計(jì)劃
- 教學(xué)創(chuàng)新與成果分享機(jī)制計(jì)劃
- 防止職業(yè)倦怠的小技巧計(jì)劃
- 醫(yī)學(xué)影像科醫(yī)生工作計(jì)劃
- 建立員工反饋與建議機(jī)制計(jì)劃
- 2025年電動(dòng)晾衣機(jī)項(xiàng)目合作計(jì)劃書
- 景區(qū)承包合同
- 珠寶定制服務(wù)特殊條款協(xié)議
- 農(nóng)產(chǎn)品電商項(xiàng)目開發(fā)合作框架協(xié)議
- 2025年月度工作日歷含農(nóng)歷節(jié)假日電子表格版
- 部編版六年級(jí)下冊(cè)道德與法治全冊(cè)教案教學(xué)設(shè)計(jì)
- 物流無人機(jī)垂直起降場(chǎng)選址與建設(shè)規(guī)范
- 《監(jiān)理安全培訓(xùn)》課件
- 最新六年級(jí)英語下冊(cè)《全套教材分析解讀》外研版課件
- Counting Stars 歌詞
- 肩鎖關(guān)節(jié)脫位的分型及其endobutton手術(shù)治療
- 管理系統(tǒng)中計(jì)算機(jī)應(yīng)用PPT課件
- 企業(yè)辦公自動(dòng)化系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)
- 標(biāo)準(zhǔn)的個(gè)人簡(jiǎn)歷表格()
- [農(nóng)林牧漁]CPM—Dairy v3幫助文件
評(píng)論
0/150
提交評(píng)論