恒美力健 發表於 2020-2-18 16:56:00

汇编语言转移指令实例

<p>实验8 分析一个奇怪的程序</p>
<p>整理了一下对汇编语言第二版实验8的理解</p>
<p>涉及了转移指令,计算机组成原理的一些知识</p>
<p>&nbsp;</p>
<p>首先我们来看一下代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> assume <span style="color: rgba(0, 128, 128, 1)">cs:</span><span style="color: rgba(0, 0, 0, 1)">codesg
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">codesg segment
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>   
<span style="color: rgba(0, 128, 128, 1)"> 4</span>   <span style="color: rgba(0, 0, 255, 1)">mov</span><span style="color: rgba(0, 0, 0, 1)"> ax,4c00h
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span>   <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> 21h
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>   
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 128, 128, 1)">start:</span><span style="color: rgba(0, 0, 255, 1)">mov</span> ax,<span style="color: rgba(128, 0, 128, 1)">0</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>   <span style="color: rgba(0, 128, 128, 1)">s:</span><span style="color: rgba(0, 0, 255, 1)">nop</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(0, 0, 255, 1)">nop</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>   
<span style="color: rgba(0, 128, 128, 1)">11</span>   <span style="color: rgba(0, 0, 255, 1)">mov</span><span style="color: rgba(0, 0, 0, 1)"> di,offset s
</span><span style="color: rgba(0, 128, 128, 1)">12</span>   <span style="color: rgba(0, 0, 255, 1)">mov</span><span style="color: rgba(0, 0, 0, 1)"> si,offset s2
</span><span style="color: rgba(0, 128, 128, 1)">13</span>   <span style="color: rgba(0, 0, 255, 1)">mov</span> ax,<span style="color: rgba(0, 128, 128, 1)">cs:</span><span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 128, 1)">14</span>   <span style="color: rgba(0, 0, 255, 1)">mov</span> <span style="color: rgba(0, 128, 128, 1)">cs:</span><span style="color: rgba(0, 0, 0, 1)">,ax
</span><span style="color: rgba(0, 128, 128, 1)">15</span>   
<span style="color: rgba(0, 128, 128, 1)">16</span>   <span style="color: rgba(0, 128, 128, 1)">s0:</span> <span style="color: rgba(0, 0, 255, 1)">jmp</span><span style="color: rgba(0, 0, 0, 1)"> short s
</span><span style="color: rgba(0, 128, 128, 1)">17</span>   
<span style="color: rgba(0, 128, 128, 1)">18</span>   <span style="color: rgba(0, 128, 128, 1)">s1:</span> <span style="color: rgba(0, 0, 255, 1)">mov</span> ax,<span style="color: rgba(128, 0, 128, 1)">0</span>
<span style="color: rgba(0, 128, 128, 1)">19</span>         <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> 21h
</span><span style="color: rgba(0, 128, 128, 1)">20</span>         <span style="color: rgba(0, 0, 255, 1)">mov</span> ax,<span style="color: rgba(128, 0, 128, 1)">0</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>   
<span style="color: rgba(0, 128, 128, 1)">22</span>   <span style="color: rgba(0, 128, 128, 1)">s2:</span> <span style="color: rgba(0, 0, 255, 1)">jmp</span><span style="color: rgba(0, 0, 0, 1)"> short s1
</span><span style="color: rgba(0, 128, 128, 1)">23</span>         <span style="color: rgba(0, 0, 255, 1)">nop</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">codesg ends
</span><span style="color: rgba(0, 128, 128, 1)">25</span> end start</pre>
</div>
<p>初略的查看代码发现</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 255, 1)">mov</span><span style="color: rgba(0, 0, 0, 1)"> di,offset s
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">mov</span><span style="color: rgba(0, 0, 0, 1)"> si,offset s2
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">mov</span> ax,<span style="color: rgba(0, 128, 128, 1)">cs:</span><span style="color: rgba(0, 0, 0, 1)">
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 255, 1)">mov</span> <span style="color: rgba(0, 128, 128, 1)">cs:</span>,ax</pre>
</div>
<p>11.将s处的偏移地址传到di寄存器</p>
<p>12.将s2处的偏移地址传到si寄存器</p>
<p>13.将s2处的代码传到ax寄存器</p>
<p>14.再将ax中的数据传到s处</p>
<p>简单的将就是将s2处的代码移到s处</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">8</span> <span style="color: rgba(0, 0, 255, 1)">jmp</span><span style="color: rgba(0, 0, 0, 1)"> short s1
</span><span style="color: rgba(0, 128, 128, 1)">9</span> <span style="color: rgba(0, 0, 255, 1)">nop</span></pre>
</div>
<p>注意这里有点问题再汇编语言中offset s并不是汇编语言,只是有编译器进行解析</p>
<p>通过debug命令</p>
<p>我们来分析一下代码的具体过程</p>
<p><img src="https://img2018.cnblogs.com/common/1860798/202002/1860798-20200218161415080-558414520.png" alt="" width="715" height="484"></p>
<p>我们直接看到s2处的第一行</p>
<p>0770:0020 EBF6&nbsp; &nbsp; JMP 0018</p>
<p>对应的汇编指令是jmp short s1</p>
<p>我们可以发现再EBF6中并没有出现18,而是使用位移量来确认地址</p>
<p>我们可以来计算一下为什么是F6</p>
<p>18-22=F6</p>
<p>18是s1处的地址,22是该指令的下一条指令的地址(这却决于计算机的设计,每执行一条指令前,ip会先加上该条指令的长度)</p>
<p>内存中存放的是二进制数据,而不是直接的jmp short s1</p>
<p>这意味着被复制到s处的指令也是EBF6</p>
<p>执行一下代码后跳转到s处继续执行</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 128, 128, 1)">s0:</span> <span style="color: rgba(0, 0, 255, 1)">jmp</span> short s</pre>
</div>
<p>此时s处的指令位EBF6,下图</p>
<p>可以算出当执行s处代码后,0A+F6=00,跳转到mov ax,4C00处执行,最后退出程序</p>
<p><img src="https://img2018.cnblogs.com/common/1860798/202002/1860798-20200218164919421-1695640821.png" alt="" width="559" height="374"></p>
<p>&nbsp;</p>
<p>个人的思路有些地方不严谨,有错误难免,还望指正!</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/naturals/p/12324481.html
頁: [1]
查看完整版本: 汇编语言转移指令实例