与-101x86第一个汇编程序及其它_第1页
与-101x86第一个汇编程序及其它_第2页
与-101x86第一个汇编程序及其它_第3页
与-101x86第一个汇编程序及其它_第4页
与-101x86第一个汇编程序及其它_第5页
已阅读5页,还剩56页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

$gcc- Thiswillproduceafile n:theoptimization$gcc–S-

main; .rdata,

o

$0, $0, .def_exit;.scl3;.type32;.def_printf;.scl3;.type32;

_main;%esp,$8,$-16,$0,$15,$15,$4,$4,%eax,--4(%ebp),

gccgcc- alloca main

$LC0, main;

.rdata, o

_main;%esp,$8,$-16,$0,$15,$15,$4,$4,%eax,--4(%ebp),

gccgcc- alloca main

$0,

$LC0,

.def_exit;.scl3;.type32;.def_printf;.scl3;.type32; main;

.rdata, oworld\12\0”.globl

_main;

$16,

%esp,$8, gcc- oworld.c- $-16, alloca 版本3.4.4(32位) main $LC0, $0, "o

_main;

$16,%esp,$8,$-16,$LC0,$0,汇编代码中以“.”开头的行都是汇编代码中以“.”开头的行都是汇编指示(Directives),如“.file”def”text main; #.defdirectivesdefinesadebugging#.scl2meansstorageclass(externalstorageclass)#.type32saysthissymbolisafunction.

地址的符号(Symbol)示例示例指示汇编器符号“_main”是全局的,这样同一程序的其它模块可它则则不是全局可见.p2align#指定下一行代码的对齐方式:第1参数表示按2的多少次幂第216.section.rdata,.ascii o.globl.align1请尝试解释一下intc1局变

oworld.c main; .ascii o.p2align.globl

#1615字节,则不对齐 _main; $16,%esp,$8,$-16,$LC0,$0,_puts; _exit; oworld.c "o.p2align.globl

#1615字节,则不对齐

%esp,$4,

关#分配4字节堆栈关

gcc-O2-mpreferred- main

oworld.c-

$LC0,$0,

oworld.c main;

.ascii o

#1615关oworld.c关oworld.c-gcc-O2-mpreferred-stack-boundary=2-fomit-$4,$LC0,$0,charbig_array[1<<24];/*16MBcharhuge_array[1<<28];/*256MB char*p1,*p2,*p3,intuseless(){return0;int{p1=malloc(1<<28);p2=malloc(1<<8);p3=malloc(1<<28);p4=malloc(1<<8);

/*256MB/*256B/*256MB/*256B/*Someprintstatements...}finalmalloc()

&p2finalmalloc()

addressrange &p2 as-omy-object- //ld–omy-exe-filemy-object-64位环境下添加命令行参数:-moworld

len

o $len,

$msg,$1,$4,$0,$1,

#系统输出(writeint80寄存器eax中存放的是系统调用的功能号,而传给系统调用的数则必须按顺序放到寄存器ebx,ecx,edx,esi,edi中当系统调用完成之后,返回值可以在寄存器eax中获需将系统调用功能号保存在寄存器eax中,全部参数依次放在一ebx中保存指向该内存区域系统调用完成之后,返回值仍将保存在寄存器eax中1(Thiscalldoesnotfork-createchild20intheprocessidofcloneororENOMEMinread-readfromfileor3fileaddressofthebuffertoumnumberofbytesnumberofbytesactually|EAGAIN|EBADF|EFAULTEINTR|EINVAL|EIO|close-closeafileor6filezeroforsuccess|waitpid-waitforaprocessesto7processidoftheprocessto0,oraddressofbuffertoexitoptionflags|0|WNOHANGpidoffinishedprocess||EINVAL|exitstateoffinishedprocess,non-zerovaluewasinputincreate-createa8addressofzero-filepermissionfiledescriptorofopenedfileEACCESS|EEXIST|EFAULTEISDIR|ELOOP|EMFILE,LONG|ENFILEENOENT|ENODEV|ENODIRENOMEM|ENOSPC|ENXIOEROFS|unlink-deleteanameandremovefilewhennotaddressofzero-terminatedpathnameexistingfile0|EACCES|EFAULT|EIO|EISDIR|ELOOP LONG|ENOENT|ENOMEMENOTDIR|EPERM|execve-executeaaddressofzero-terminatedpathnameaddressofzero-terminatedlistofofzero-terminatedargumentaddressofzero-terminatedlistofofzero-terminatedenvironmentIfsuccess,noreturnbecausethenewprograminheritsresourcesandoverwritescaller;otherwise:E2BIG|EACCES|EINVAL|EIO|EISDIR|ELIBBAD|ELOOP|ENFILE|ENOEXEC|ENOENT|ENOMEM|ENOTDIR|EFAULT|ENA LONG|EPERM|chdir-changeworkingaddressofzero-terminatedpathnameexisting0|EACCES|EBADF|EFAULT|EIO|ELOOPLONG|ENOENT|ENOMEM .globl

#

%ecx,%ecx%ecx,%edx,(%ebx),%al,%al$10,-$4,$1,%ebx,

#时,其所需的参数(argcargv)将sys_write)#文件描述符(stdout)指指 入。EAX寄存器用于决定CPUID指令EAX存器的值,CPUID指令在EBX,ECX和EDX寄存(0)值被EAX寄存器并且执行CPUID令时,处理器把厂商ID字符串返回到EBX,EDXECXEBX最低个字EDX中间个字ECX最高个字 "Theprocessvendoridis.sectionm.section

$0,$buffer, %ebx, %edx, %ecx,$as-ocpuid.o

$ld-lc-dynamic-linker/lib/ld-linux.so.2-ocpuid 汇编器使用_start符号表示默认的起始点,此外如果想要汇编 ,需要赋予.globl属性,如:.globl只读数据段一个数据元素时,需要使用Symbol示例如下.ascii o .float 可以在一行中定义多个值如.int20,10,30,

.32.long32位整数和int.octa16.quad8.short16.single单精度浮点数(和float相同和data段不同,无需 特定的数据类型,只需 示例如下.sectionmbuffer,#该语句把1000字节的内存地址赋予buffer外部模块不能相相比较.data段,.bss 的优点是.section.data.long.section.globlmovl$0,movldata_items(,%edi,4),%eaxmovl%eax,%ebxcmpl$0,%eaxjeloop_exitincl%edimovldata_items(,%edi,4),%eaxcmpl%ebx,%eaxjlemovl%eax,%ebxjmpstart_loopmovl$1,int

#1istheexit().section #Thesearethedata.long.section.globlmovl$0,%edi #move0intotheindexregistermovldata_items(,%edi,4),%eax#loadthefirstdatamovl%eax, #sincethisisthefirstitem,%eax#thecmpl$0,%eaxjeloop_exit

#start#checktoseeifwe'vehittheincl%edi #loadnextvaluemovldata_items(,%edi,4),%eaxcmpl%ebx,jlemovl%eax,%ebxjmpstart_loop

#compare#jumptoloopbeginningifthe#oneisn't#movethevalueasthelargest#jumptoloopbeginningmovlmovl$1,#1istheexit()int#%ebxisthestatuscodefortheexitsystem #anditalreadyhasthe um 补充:算术操作指指效描imullR[%edx]:R[%eax]=S*无符号64位mullR[%edx]:R[%eax]=S*有符号64位 R[%edx]:R[%eax]=符号位扩展转换为4字idivlR[%edx]=R[%edx]:R[%eax]%R[%eax]=R[%edx]:R[%eax]/divlR[%edx]=R[%edx]:R[%eax]%R[%eax]=R[%edx]:R[%eax]/64位数通 .section.globlpushlsecondpushlfirstcalltheaddl$8,thestackpointerpushlthefirstpushl$2pushl$5callpoweraddl$8,%esp

#pushsecondargument#pushfirstargument#callthefunction#movethestackpointersecondanswerisin%eax.We%eax,thefirstoutinto%ebxthemtogether;theresultisin%ebxmovl$1,%eax #exit(%ebxisreturned)int$0x80.typepower,@functionpushl%ebpmovl%esp,%ebpsubl$4,%espmovl8(%ebp),%ebxmovl12(%ebp),%ecxmovl%ebx,-

为何还要保存%ebp等操#saveoldbase#makestackpointerthebase#getroomforourlocalstorage#putfirstargumentin%eax#putsecondargumentin%ecx#storecurrentresultcmpl$1,%ecxjeend_power

#ifthepoweris1,wearemovl-4(%ebp),%eax#movethecurrentresultintoimull%ebx, #multiplythecurrentresultbythebasemovl%eax,-4(%ebp)#storethecurrentdecl #decreasethejmppower_loop_start#runforthenextmovl-4(%ebp),%eax#returnvaluegoesinmovl%ebp,popl

#restorethestack#restorethebase.type.typepower, lsthelinkerthatthesymbolpowershouldbetreatedasa阶乘.section.globl #thisisunneededunlesswewanttoshare.globlpushlcall

#Thefactorialtakesoneargument–#thenumberwewantafactorialof.#runthefactorialfunction$4,#restorethe%eax,#factorialreturnstheanswerin%eax,but$1,#wantitin%ebxtosenditasourexit#callthekernel’sexitint#Thisistheactualfunction factorial,pushl

%esp,8(%ebp), #Thismovesthefirstargumenttocmpl$1, #Ifthenumberis1,thatisourbasecase,#wesimplyreturn(1isalreadyin%eaxas#returnjedecl #otherwise,decreasethepushl #pushitforourcalltocall #callmovl8(%ebp), #%eaxhasthereturnvalue,sowereload#parameterintoimull%ebx, #multiplythatbytheresultofthelastcall#factorial(in%eax);theanswerisstoredmovl%ebp,%esppopl%ebpStep1:Step2:Step3:如果输入文件 位置已到文件尾,则转Step7;Step4: Step5:遍历该内容,将其中的小写字母转换成大写;Step6:将转换后的该内容写入输出文件,转Step3;Step7:程序结束。.equ用于把常量值设置为可以在程序中使用的.equfactor,.equLINUX_SYS_CALL,经过设置之后,数据符号值是不能在程序中改动的#######toupper.s#systemcall.equSYS_OPEN,.equSYS_WRITE,.equSYS_READ,.equSYS_CLOSE,.equSYS_EXIT,#optionsforO_RDONLY,O_PERMISSION,#standardfile.equSTDIN,.equSTDOUT,.equSTDERR,#systemcall.equLINUX_SYSCALL,.equEND_OF_FILE,0#Thisisthereturnvalueofreadwhichmeans#hittheendofthe.section.section#Buffer-thisiswherethedataisloadedintofromthe#fileandwrittenfromintotheoutput.equBUFFER_SIZE,mBUFFER_DATA,.section#STACKST_FD_IN,-ST_FD_OUT,-8ST_ARGC,#NumberofST_ARGV_0,#NameofST_ARGV_1,ST_ARGV_2,#Inputfile#Outputfile.globl###INITIALIZEmovl%esp,subl$ST_SIZE_RESERVE,%esp#Allocatespaceforour$SYS_OPEN,#openST_ARGV_1(%ebp),#inputfilenameinto$O_RDONLY,$O_PERMISSION,intmovl%eax, #savethegivenfilemovl$SYS_OPEN,movlST_ARGV_2(%ebp),

#openthemovl$O_CREAT_WRONLY_TRUNC, #flagsforwritingtothe#modefornewmovl$O_PERMISSION,%edxint$LINUX_SYSCALL#storethefiledescriptorheremovl%eax,ST_FD_OUT(%ebp)###BEGINMAINmovl$SYS_READ,movlST_FD_IN(%ebp),%ebx #gettheinputfiledescriptormovl$BUFFER_DATA,%ecx #thelocationtoreadintomovl$BUFFER_SIZE,%edx #thesizeofthebufferint #Sizeofbufferreadisreturned###EXITIFWE’VEREACHEDTHEcmpl$END_OF_FILE,%eaxjleend_loop

#checkforendoffile#iffoundoronerror,gotothe###CONVERTTHEBLOCKTOUPPERpushlpushlcallpopl%eaxaddl$4,%esp

#locationof#sizeofthe#getthesizeback#restore%esp###WRITETHEBLOCKOUTTOTHEOUTPUTmovl%eax,movl$SYS_WRITE,movlST_FD_OUT(%ebp),%ebxmovl$BUFFER_DATA,%ecxint$LINUX_SYSCALL###CONTINUETHEjmp

#sizeofthe#theoutputfile#locationofthe###CLOSETHEmovl$SYS_CLOSE,movlST_FD_OUT(%ebp),intmovl$SYS_CLOSE,%eaxmovlST_FD_IN(%ebp),%ebxint$LINUX_SYSCALLmovl$SYS_EXIT,movl$0,int#convert_to_upper#INPUT:Thefirstparameteristhelocationoftheblockofmemory#Thesecondparameteristhelengthofthat#OUTPUT:Thisfunctionoverwritesthecurrentbufferwiththe.equLOWERCASE_A,.equLOWERCASE_Z,

#Thelowerboundaryofoursearch#Theupperboundaryofour.equUPPER_CONVERSION,’A’-###STACK.equST_BUFFER_LEN,.equST_BUFFER,

#Lengthof#actualpushl%ebpmovl%esp,%ebp#ifabufferwithzerolengthwasgiventous,justcmpl$0,jemovb(%eax,%edi,1),cmpb$LOWERCASE_A,jlcmpb$LOWERCASE_Z,jg

#getthecurrent#gotothenextbyteunlessit###SET###SETUPmovlST_BUFFER(%ebp),movlST_BUFFER_LEN(%ebp),movl$0,#loop#otherwiseconvertthebytetouppercase,andstoreitaddb$UPPER_CONVERSION,movb%cl,inclcmpl%edi,%ebxjneconvert_loopmovl%ebp,%esppopl%ebp

#next#continueunlesswe’vereachedthe#Common#CommonLinux#SystemCall.equSYS_EXIT,.equSYS_READ,.equSYS_WRITE,.equSYS_OPEN,.equSYS_CLOSE,.equSYS_BRK,#SystemCallInterrupt.equLINUX_SYSCALL,#StandardFile.equSTDIN,.equSTDOUT,.equSTDERR,#CommonStatus.equEND_OF_FILE,.equRECORD_FIRSTNAME,.equRECORD_LASTNAME,.equRECORD_ADDRESS,.equRECORD_AGE,.equRECORD_SIZE,使用时.include.include"record-#read-.include"record-.include#INPUT:Thefiledescriptoranda#OUTPUT:Thisfunctionwritesthedatatothebufferandreturns#STACKLOCAL.equST_READ_BUFFER,.equST_FILEDES,

.section.globl.typeread_record,pushlmovl%esp,pushlmovlST_FILEDES(%ebp),%ebxmovlST_READ_BUFFER(%ebp),%ecxmovl$RECORD_SIZE,%edxmovl$SYS_READ,int

#NOTE-%eaxhasthereturnvaluepopl%ebxmovl%ebp,popl#write-.include.include"record-#INPUT:Thefiledescriptoranda#OUTPUT:Thisfunctionproducesastatus#STACKLOCAL.equST_WRITE_BUFFER,.equST_FILEDES,.section.globl.typewrite_record,@functionpushl%ebpmovl%esp,%ebppushl%ebxmovl$SYS_WRITE,movlST_FILEDES(%ebp),%ebxmovlST_WRITE_BUFFER(%ebp),%ecxmovl$RECORD_SIZE,%edxint

#NOTE-%eaxhasthereturnpoplmovl%ebp,popl%ebp#write-.include.include"record-.section.data.ascii.rept31#Paddingto40.byte

OpentheWritethreeClosethe.ascii.rept31#Paddingto40.ascii.rept31#Paddingto40.byte

“.reptN”209#Paddingto2400填充其与“.endr”之间容,重复N…

#skiprecord2~#Thisisthenameof.ascii

filewewillwrite.equST_FILE_DESCRIPTOR,-.globlmovl%esp,subl$4,%esp #Allocatespacetoholdthefiledescriptormovl$SYSOPEN%eaxaswrite-records.s-owrite-record.oaswrite-record.s-owrite-ldwrite-record.owrite-records.o-owrite-movl%eax,ST_FILE_DESCRIPTOR(%ebp)#Storethefilepushlpushl$record1callwrite_recordaddl$8,%esp

#Writethefirst#Writetheremainingrecords;closethefileand…OpentheAttempttoreadaIfweareattheendofthefile,Otherwise,countthecharactersofthefirstWritethefirstnametoWriteanewlinetoGobacktoreadanother#count-#INPUT:Theaddressofthe#OUTPUT:Returnsthecountin.typecount_chars,.globl.equST_STRING_START_ADDRESS,pushl

%esp,$0, #CounterstartsatmovlST_STRING_START_ADDRESS(%ebp), #Startingaddressofmovb(%edx),%alcmpb$0,%aljecount_loop

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论