﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-淡泊明志、宁静致远-随笔分类-汇编艺术</title><link>http://www.cnitblog.com/houcy/category/6992.html</link><description>A Diamond is just a piece of Coal that did well under Pressure.</description><language>zh-cn</language><lastBuildDate>Sat, 16 Mar 2013 18:18:33 GMT</lastBuildDate><pubDate>Sat, 16 Mar 2013 18:18:33 GMT</pubDate><ttl>60</ttl><item><title>反汇编心得</title><link>http://www.cnitblog.com/houcy/archive/2013/03/16/87075.html</link><dc:creator>挑灯看剑</dc:creator><author>挑灯看剑</author><pubDate>Sat, 16 Mar 2013 07:09:00 GMT</pubDate><guid>http://www.cnitblog.com/houcy/archive/2013/03/16/87075.html</guid><wfw:comment>http://www.cnitblog.com/houcy/comments/87075.html</wfw:comment><comments>http://www.cnitblog.com/houcy/archive/2013/03/16/87075.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/houcy/comments/commentRss/87075.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/houcy/services/trackbacks/87075.html</trackback:ping><description><![CDATA[看汇编代码时一定要牢牢把握栈的变化情况，脑里浮现栈帧图，并时刻跟踪ESP，这样会更容易读懂汇编。<br /><br />下面是函数调用时callee要做的事情（被调用者负责平衡堆栈: 分配和释放栈空间），所有的函数本质上都是callee，由别的函数调用指行，包括main函数：<br /><br />1. 保存旧的帧指针（把新的栈帧链接到栈帧链表中）<br />其实栈被分成一个个连续的栈帧(由于函数调用的原因)，这些栈帧就像链表一样被EBP链接了起来，每次被调用函数callee要做的第一件事情就是把新的栈帧链接到原来的栈帧链表上，即汇编代码:<br />&nbsp;push ebp<br />把EBP的值压栈，而EBP恰是caller函数的帧指针，这就相当于挂接到栈帧链表上。<br /><br />2. 建立新的帧指针<br />&nbsp;mov&nbsp; ebp ,&nbsp;esp<br /><br />3. 分配新的栈空间<br />sub esp , 0xC0h<br /><br />4. 把寄存器压栈<br />push ebx<br />push esi<br />push edi<br /><br />5. 初始化栈空间<br />初始化栈空间为0xCCh，<span lang="EN-US" xml:lang="EN-US">0xCCh</span>是汇编指令<span lang="EN-US" xml:lang="EN-US">int 3</span>的二进制码，便于中断纠错。<br />lea edi , [ebp - 0xC0h]<br />mov ecx , 30h&nbsp; ;长度，30h * 4 = 0xC0h<br />mov eax , 0xCCCCCCCCh <br />rep stosd<br /><br />6. 执行函数的算法代码<br /><br />7. 平衡堆栈：弹出保存的寄存器，恢复栈空间，恢复被保存的EBP，即从栈帧链表中删除callee的栈帧。<br />pop edi<br />pop esi<br />pop ebx<br />mov esp , ebp ;恢复栈空间，重置栈顶esp, 此时esp = ebp = 旧的ebp<br />pop ebp ;恢复旧的EBP，即把新的栈帧从栈帧链表中删除了，此时esp指向被保存的函数返回地址，即call之后的地址<br />retn&nbsp; ; 函数返回，此指令相当于pop eip，把esp指向的函数返回地址赋值给EIP<br /><br />栈帧结构图：<br /><img border="0" alt="" src="http://www.cnitblog.com/images/cnitblog_com/houcy/栈帧图.jpg" width="461" longdesc="" height="624" /><br /><br /><img src ="http://www.cnitblog.com/houcy/aggbug/87075.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/houcy/" target="_blank">挑灯看剑</a> 2013-03-16 15:09 <a href="http://www.cnitblog.com/houcy/archive/2013/03/16/87075.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Registers in x86 architecture</title><link>http://www.cnitblog.com/houcy/archive/2008/05/05/43378.html</link><dc:creator>挑灯看剑</dc:creator><author>挑灯看剑</author><pubDate>Mon, 05 May 2008 09:14:00 GMT</pubDate><guid>http://www.cnitblog.com/houcy/archive/2008/05/05/43378.html</guid><wfw:comment>http://www.cnitblog.com/houcy/comments/43378.html</wfw:comment><comments>http://www.cnitblog.com/houcy/archive/2008/05/05/43378.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/houcy/comments/commentRss/43378.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/houcy/services/trackbacks/43378.html</trackback:ping><description><![CDATA[[Excerpted from the book:&lt;The Linux Kernel Primer&gt;.]&nbsp; <br><br>&nbsp;&nbsp; The x86 architecture is a Complex Instruction Set Computing(CISC) architecture. Instructions are variable length,depending on their function.Three kinds of registers exist in the Pentuim class x86 architecture: general pupose,segment,and status/control. The basic user set is as follows:<br>* EAX:&nbsp; General purpose accumulator<br>* EBX:&nbsp; Pointer to data<br>* ECX:&nbsp; Counter for loop operations<br>* EDX:&nbsp; I/O pointer<br>* ESI: &nbsp; Pointer to data in DS segment<br>* EDI: &nbsp; Ponter to data in ES segment<br>* ESP: &nbsp; Stack pointer<br>* EBP:&nbsp; Pointer to data on the stack<br>&nbsp;&nbsp; These six segment registers are used in real mode addressing where memory is accessed in blocks.A given byte of memory is then referenced by an offset from this segment(for example,ES:EDI references memory in the ES with an offset of the value in EDI):<br>* CS:&nbsp; Code segment<br>* SS:&nbsp; Stack segment<br>* ES,DS,FS,GS: Data segment<br>&nbsp;&nbsp; The EFLAGS register indicates processor status after each intruction. This can hold results such as zero,overflow,or carry.The EIP is a dedicated pointer register that indicates an offset to the current instruction to the processor.This is generally used with the code segment register to form a complete address(for example, CS:EIP):<br>EFLAGS: Status,control,and system flags<br>EIP: The instruction pointer, contains an offset from CS<br><br><img src ="http://www.cnitblog.com/houcy/aggbug/43378.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/houcy/" target="_blank">挑灯看剑</a> 2008-05-05 17:14 <a href="http://www.cnitblog.com/houcy/archive/2008/05/05/43378.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Smashing The Stack For Fun And Profit</title><link>http://www.cnitblog.com/houcy/archive/2008/04/27/42819.html</link><dc:creator>挑灯看剑</dc:creator><author>挑灯看剑</author><pubDate>Sun, 27 Apr 2008 05:36:00 GMT</pubDate><guid>http://www.cnitblog.com/houcy/archive/2008/04/27/42819.html</guid><wfw:comment>http://www.cnitblog.com/houcy/comments/42819.html</wfw:comment><comments>http://www.cnitblog.com/houcy/archive/2008/04/27/42819.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/houcy/comments/commentRss/42819.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/houcy/services/trackbacks/42819.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: oO Phrack 49 Oo.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Volume Seven, Issue Forty-Nine&nb...&nbsp;&nbsp;<a href='http://www.cnitblog.com/houcy/archive/2008/04/27/42819.html'>阅读全文</a><img src ="http://www.cnitblog.com/houcy/aggbug/42819.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/houcy/" target="_blank">挑灯看剑</a> 2008-04-27 13:36 <a href="http://www.cnitblog.com/houcy/archive/2008/04/27/42819.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>