




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
機(jī)器組織與匯編語言設(shè)計第1頁,共30頁,2023年,2月20日,星期一前言課程由來基本內(nèi)容教材和參考資料課程的管理要求Atraditionalfirstprogramforwarm-up第2頁,共30頁,2023年,2月20日,星期一課程的由來2002年12月,接受講匯編課程任務(wù)接收Prof.Dai的ppt,教材2002年12和2003年1月為確定教材找其他院校開設(shè)匯編課程的資料瀏覽國內(nèi)匯編方面書籍通過搜索引擎查找2003年2月確定教材2004年2月重新確定教材第3頁,共30頁,2023年,2月20日,星期一Thepowerofassemblyprogramming引自/linux/assembly/section1.html
(插曲)Identityofthe"bloatedsoftware"IndependencefromGLIBCRequirementofexitfunctionHowtocommunicatewithsystemcallsinassembly?BriefanatomyofExecutableandLinkingFormat(ELF)第4頁,共30頁,2023年,2月20日,星期一GNU第5頁,共30頁,2023年,2月20日,星期一LinusTorvalds第6頁,共30頁,2023年,2月20日,星期一1.Identityofthe“bloatedsoftware”Test0.cintmain(){ return(123);}Compileandlink$gcc–otest0test0.c$ls–ltest0-rwxr-xr-x1rootsrc4552Jan620:38test0$./test0;echo$?123第7頁,共30頁,2023年,2月20日,星期一1.1Examinelibrarydependency&symbolnamedefinitions$lddtest0libc.so.6=>/lib/tls/libc.so.6(0xb74a0000)/lib/ld-linux.so.2=>/lib/ld-linux.so.2(0xb75eb000)$nmtest0
…
08048320t__do_global_dtors_aux w__gmon_start__ U__libc_start_main@@GLIBC_2.0
… 080483b0Tmain
080494fcbobject.11 08049424dp.3第8頁,共30頁,2023年,2月20日,星期一1.2identityof“bloatedsoftware”gcc–v–otest0test0.cReadingspecsfrom/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/specsConfiguredwith:../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-linuxThreadmodel:posixgccversion3.2.320030502(RedHatLinux3.2.3-20)/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/cc1-lang-c-v-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-dumpbasetest0.c-version-o/tmp/ccBPEYcR.sGNUCPPversion3.2.320030502(RedHatLinux3.2.3-20)(cpplib)(i386Linux/ELF)GNUCversion3.2.320030502(RedHatLinux3.2.3-20)(i386-redhat-linux)compiledbyGNUCversion3.2.320030502(RedHatLinux3.2.3-20).ignoringnonexistentdirectory"/usr/i386-redhat-linux/include"#include"..."searchstartshere:#include<...>searchstartshere:/usr/local/include/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/include/usr/includeEndofsearchlist.
as-V-Qy-o/tmp/ccaVmECA.o/tmp/ccBPEYcR.sGNUassemblerversion.4(i386-redhat-linux)usingBFDversion.420030523/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/collect2--eh-frame-hdr-melf_i386-dynamic-linker/lib/ld-linux.so.2-otest0/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crti.o/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/crtbegin.o-L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3-L/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../tmp/ccaVmECA.o-lgcc-lgcc_eh-lc-lgcc-lgcc_eh/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/crtend.o/usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crtn.o第9頁,共30頁,2023年,2月20日,星期一2.IndependencefromGLIBCgcc–nostdlib–otest0test.c/usr/bin/ld:warning:cannotfindentrysymbol_start;defaultingto08048094nm/usr/lib/crt1.o00000000D__data_start00000000Wdata_start00000000R_fp_hw00000004R_IO_stdin_usedU__libc_csu_finiU__libc_csu_initU__libc_start_main
Umain00000000T_start第10頁,共30頁,2023年,2月20日,星期一2.1escapefromtheproblemgcc–ctest0.cld–emain
–otest0test0.o$ls-ltest0 -rwxr-xr-x1rootsrc989Jan621:00test0$lddtest0 notadynamicexecutable$nmtest0 080490acA__bss_start 080490acA_edata 080490acA_end 08048094Tmain$./test0segmenterror第11頁,共30頁,2023年,2月20日,星期一3.Requirementofexitfunction$gdbtest0GNUgdbRedHatLinux(5.3.90-0.20030710.40rh)Copyright2003FreeSoftwareFoundation,Inc.GDBisfreesoftware,coveredbytheGNUGeneralPublicLicense,andyouarewelcometochangeitand/ordistributecopiesofitundercertainconditions.Type"showcopying"toseetheconditions.ThereisabsolutelynowarrantyforGDB.Type"showwarranty"fordetails.ThisGDBwasconfiguredas"i386-redhat-linux-gnu"...(nodebuggingsymbolsfound)...Usinghostlibthread_dblibrary"/lib/tls/libthread_db.so.1".(gdb)disassemblemainDumpofassemblercodeforfunctionmain:0x08048094<main+0>:push%ebp0x08048095<main+1>:mov%esp,%ebp0x08048097<main+3>:sub$0x8,%esp0x0804809a<main+6>:and$0xfffffff0,%esp0x0804809d<main+9>:mov$0x0,%eax0x080480a2<main+14>:sub%eax,%esp0x080480a4<main+16>:mov$0x7b,%eax0x080480a9<main+21>:leave0x080480aa<main+22>:ret
Endofassemblerdump.第12頁,共30頁,2023年,2月20日,星期一3.1Anewversion“tetst1.c”Test1.c#include<stdlib.h>main(){ exit(123);}Compileandlink$gcc–otest1test1.c$ls–ltest1-rwxr-xr-x1rootsrc4640Jan621:48test1$./test1;echo$?123第13頁,共30頁,2023年,2月20日,星期一3.2Dependonastandardlibrary$gcc–nostdlib–otest1test1.c /usr/bin/ld:warning:cannotfindentrysymbol_start;defaultingto08048094 /tmp/cc27NBFk.o(.text+0x16):Infunction`main': :undefinedreferenceto`exit' collect2:ldreturned1exitstatus第14頁,共30頁,2023年,2月20日,星期一4.Communicatewithsystemcallsinassemblyxexit.asm bits32 ;Use32bitmode. section.text ;Codemustresidesinthe".text"inGCC. globalxexit ;Declarexexit()asapublicfunction. ;voidxexit(intstatus) xexit: movebp,esp ;Now,[ebp]pointsreturnaddress. ;and[ebp+4]points1stargument. movebx,[ebp+4] ;Readstatuscode. moveax,1 ;Callsys_exit(). int0x80 ;Neverreturns.$nasm–felfxexit.asm第15頁,共30頁,2023年,2月20日,星期一4.1FocusthesourceDeclare32bitmodeatthebeginng(bits32;incaseof16bitmode,usebits16).Usesection.textforthecodesegment(I'mafraidwhether"segment"isarightterminthiscontextornot.).Declarexexitasaglobalfunctionname.Ifyouforgetit,youcan'tcallxexitfromCsourcefile.Thecodebeginsunder"exit:".ReadpassedargumentintoEBX.InClanguage,callerputsargumentsandreturnaddressintoastack(Usually,stackingisperformedbyaunitofword,32bits).[ebp]pointsreturnaddress,[ebp+4]pointsfirstargument,[ebp+8]pointssecondargument,andsoon.SetoneinEAX.Thisisanumberofexitsystemcall.UNIXkernelsupportsmanysystemcallsforacommunicationwithprocess.Youcanseeallofsupportedsystemcallsin/usr/include/asm/unistd.h.Finally,executeexitsystemcallbysoftwareinterrupt128(0x80).Thisistheessenceofsystemcallformulainassembly.Itrequiresonly4instructions!第16頁,共30頁,2023年,2月20日,星期一4.2/usr/include/asm/unistd.h#ifndef_ASM_I386_UNISTD_H_#define_ASM_I386_UNISTD_H_/**Thisfilecontainsthesystemcallnumbers.*/#define__NR_exit1#define__NR_fork2#define__NR_read3#define__NR_write4#define__NR_open5#define__NR_close6#define__NR_waitpid7#define__NR_creat8#define__NR_link9#define__NR_unlink10#define__NR_execve11#define__NR_chdir12…第17頁,共30頁,2023年,2月20日,星期一4.3Anewversion“tetst2.c”Test2.cexternvoidxexit(int);main(){xexit(123);}Compileandlink$gcc–ctest2.c$ld–emain–otest2test2.oxexit.o$ls–ltest2-rwxr-xr-x1rootsrc954Jan621:48test1$./test2;echo$?123第18頁,共30頁,2023年,2月20日,星期一5.BriefanatomyofExecutableandlinkingFormat(ELF)ELFisawidelyusedobjectformatinthemodernUNIXworld.InLinux,recentdistributorscompileallpackagesinthisformat.Itisflexible,butquitedifficulttounderstandthestructure.$lesstest2"test2"maybeabinaryfile.Seeitanyway?^?ELF^A^A^A^@^@^@^@^@^@^@^@^@^B^@^C^@^A^@^@^@<A0><80>^4^@^@^@T^A^@^@^@^@^@^@4^@^@^C^@(^^@^E^@^A^@^@^@^@^@^@^@^@<80>^^@<80>^<CC>^@^@^@<CC>^@^@^@^E^@^@^@^@^P^@^@^A^@^@^@<CC>^@^@^@<CC><90>^<CC><90>^^@^@^@^@^@^@^@^@^F^@^@^@^@^P^@^@Q<E5>td^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^G^@^@^@^D^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@U<89><E5><83><EC<83><E4><F0><B8>^@^@^@^@)<C4><83><EC>^Lj{<E8>^F^@^@^@<83><C4>^P<C9><C3><90><89><E5><8B>]^D<B8>^A^@^@^@<CD><80>^@GCC:(GNU)3.2.320030502(RedHatLinux3.2.3-20)^@^@TheNetwideAssembler0.98.38^@^@.symtab^@.strtab^@.shstrtab^@.text^@.data^@.bss^@.comment^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ESC^@^@^@^A^@^@^@^F^@^@^@<A0><80>^<A0>^@^@^@,^@^@^@^@^@^@^@^@^@^@^@^P^@^@^@^@^@^@^@!^@^@^@^A^@^@^@^C^@^@^@<CC><90>^<CC>^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@^@^@^@^@'^@^@^^@^@^@^C^@^@^@<CC><90>^<CC>^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^D^@^@^@^@^@^@^@,^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@<CC>^@^@^@S^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^Q^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@^_^A^@^@5^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^A^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@<94>^B^@^@<F0>^@^@^@^G^@^@^@^@^@^@^D^@^@^@^P^@^@^@^@^@^@^C^@^@^@^@^@^@^@^@^@^@^@<84>^C^@^@6^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@<A0><80>^^@^@^@^@^C^@^A^@^@^@^@^@<CC><90>^^@^@^@^@^C^@^B^@^@^@^@^@<CC><90>^^@^@^@^@^C^@^C^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^D^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^E^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^F^@^@^@^@^@^@^@^@^@^@^@^@^@^C^@^G^@^A^@^@^@^@^@^@^@^@^@^@^@^D^@<F1><FF>^@^@^@^@^@^@^@^@^@^@^@^D^@<F1><FF>^S^@^@^@<CC><90>^^@^@^@^@^P^@<F1><FF>^_^@^@^@<A0><80>^^_^@^@^@^R^@^A^@$^@^@^@<CC><90>^^@^@^@^@^P^@<F1><FF>+^@^@^@<CC><90>^^@^@^@^@^P^@<F1><FF>0^@^@^@<C0><80>^^@^@^@^@^P^@^A^@^@test2.c^@xexit.asm^@__bss_start^@main^@_edata^@_end^@xexit^@第19頁,共30頁,2023年,2月20日,星期一5.1stripsymbols$striptest2; $ls–ltest2
-rwxr-xr-x1rootsrc546Jan621:46test2$strip--remove-section=.comment--remove-section=.notetest2 $ls–ltest2
-rwxr-xr-x1rootsrc432Jan621:47test2$strip--remove-section=.data--remove-section=.sbss--remove-section=.bsstest2 $ls–ltest2
-rwxr-xr-x1rootsrc344Jan621:48test2第20頁,共30頁,2023年,2月20日,星期一基本內(nèi)容Bygainingadeeperunderstandingofhowcomputerswork,thereadercanoftenbemuchmoreproductivedevelopingsoftwareinhigherlevellanguagessuchasCandC++.Learningtoprograminassemblylanguageisanexcellentwaytoachievethisgoal.LearningassemblerinLinuxenvironment.第21頁,共30頁,2023年,2月20日,星期一教材和參考資料PaulA.Carter.Tutorial:PCAssemblyLanguage,November11,2003.RandallHyde.TheArtofAssemblyLanguage,2001./第22頁,共30頁,2023年,2月20日,星期一課程的管理要求背景知識要求程序設(shè)計(C)Web知識(瀏覽器,HTML,HTTP,MIME,CGI,等)有SE編程經(jīng)驗(例如做過去年數(shù)據(jù)結(jié)構(gòu)作業(yè))數(shù)據(jù)結(jié)構(gòu)成績構(gòu)成課程項目實踐,50%期末考試,50%課程網(wǎng)站/~aoa/第23頁,共30頁,2023年,2月20日,星期一System
calls
除非一個匯編程序僅僅進(jìn)行數(shù)學(xué)運(yùn)算,否則其必須進(jìn)行輸入輸出及退出等操作,而要進(jìn)行這些操作就需要調(diào)用操作系統(tǒng)的服務(wù)。實際上,除了使用系統(tǒng)的服務(wù)以外,不同操作系統(tǒng)的匯編編程是很類似的。在UNIX系統(tǒng)下有兩種方式實現(xiàn)對系統(tǒng)調(diào)用的使用:通過經(jīng)過封裝的C庫(libc)或者直接調(diào)用。在匯編程序中是否使用libc不僅僅是一個編程風(fēng)格的問題,而libc封裝用來確保當(dāng)系統(tǒng)調(diào)用接口發(fā)生變化時,無須對使用libc的程序進(jìn)行修改
第24頁,共30頁,2023年,2月20日,星期一程序風(fēng)格當(dāng)前IA-32
UNIX都是32bit的操作系統(tǒng),具有以下特點:運(yùn)行在保護(hù)模式下,具有“平坦”(flat)內(nèi)存地址空間,使用ELF二進(jìn)制代碼格式。一個程序可以劃分為不同的段:.text是程序執(zhí)行代碼(只讀),.data是程序的數(shù)據(jù)部分(讀寫),.bss是沒有經(jīng)過初始化的數(shù)據(jù)(read-write);同時還可以具有其他一些標(biāo)準(zhǔn)的段及用戶自定義段,但是這種情況很少被應(yīng)用。而一個程序至少具有.text段。第25頁,共30頁,2023年,2月20日,星期一Atraditionalfirstprogramforwarm-upBrianKernighanDen
溫馨提示
- 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. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 中醫(yī)藥學(xué)概論試題及答案
- 隨州市重點中學(xué)2024-2025學(xué)年數(shù)學(xué)高二第二學(xué)期期末達(dá)標(biāo)檢測模擬試題含解析
- 企業(yè)財務(wù)數(shù)據(jù)安全保密及員工行為規(guī)范合同
- 全球化市場拓展與外貿(mào)企業(yè)知識產(chǎn)權(quán)保護(hù)合同
- 車輛贈與合同范本及贈與條件約定
- 采石場土地及礦產(chǎn)資源開采權(quán)移交合同
- 餐飲業(yè)人力資源招聘與配置顧問合同
- 精細(xì)化管理餐飲業(yè)廚師崗位勞動合同
- 團(tuán)工委工作計劃-團(tuán)委團(tuán)支部工作計劃
- 學(xué)生批量請假管理制度
- 感悟親情親情類作文指導(dǎo)
- 王春武-農(nóng)藥干懸浮劑(DF)項目研究與開發(fā)
- 幼兒啟蒙12電子狗機(jī)器人課件
- 四川康美泰衛(wèi)生用品有限公司生態(tài)康護(hù)用品研發(fā)及產(chǎn)業(yè)化項目環(huán)境影響報告
- 《好的數(shù)學(xué):數(shù)的故事》讀書筆記模板
- 機(jī)床數(shù)控技術(shù)PPT完整全套教學(xué)課件
- 2023國家開放大學(xué):《人文英語1》形考答案解析5-8unit
- 土溶洞處理監(jiān)理實施細(xì)則
- 道路危險貨物運(yùn)輸安全標(biāo)準(zhǔn)化手冊
- 名校版初中物理“公式+考點+方法技巧”大匯編
- 醫(yī)院消毒供應(yīng)中心CSSD??浦R《CSSD器械消毒與干燥方式的正確選擇》精美培訓(xùn)課件
評論
0/150
提交評論