汇编语言实验四
<h1 id="汇编语言实验四">汇编语言实验四</h1><h2 id="实验任务1">实验任务1</h2>
<p>在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串'welcome to masm'</p>
<p>task1.asm:</p>
<pre><code class="language-asm">assume cs:code,ds:data
data segment
db 'welcome to masm!'
data ends
code segment
start:mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov cx,16
mov bx,0
mov di,11*160+64;显示显存中的偏移量,11行64字节的列偏移量
s1:mov al,ds:
mov ah,00000010b;设置颜色
mov es:,ax;设置数值
inc bx
add di,2
loop s1
mov cx,16
mov bx,0
mov di,12*160+64
s2:mov al,ds:
mov ah,00100100B
mov es:,ax
inc bx
add di,2
loop s2
mov cx,16
mov bx,0
mov di,13*160+64
s3:mov al,ds:
mov ah,01110001B
mov es:,ax
inc bx
add di,2
loop s3
mov ax,4c00h
int 21h
code ends
end start
</code></pre>
<p>实验结果:<br>
<img src="https://tva1.sinaimg.cn/large/006rkvIPgy1glm27oad02j30hu0c1dg0.jpg" alt="h1" loading="lazy"></p>
<h2 id="实验任务2">实验任务2</h2>
<p>编写子程序printStr,实现以指定颜色在屏幕上输出字符串、调用它完成字符串输出:<br>
task2.asm:</p>
<pre><code class="language-asm">assume cs:code,ds:data
data segment
x dw 1234
str db 16 dup(0)
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,x
mov di,offset str
call num2str
mov di,offset str
call printstr
mov ax,4c00h
int 21h
num2str:
push ax
push bx
push cx
push dx
mov cx,0
mov bl,10
s1: div bl
inc cx
mov dl,ah
push dx
mov ah,0;将ah赋0,是将商一直除下去;
cmp al,0;
jne s1
s2: pop dx
add dl,30h
mov ,dl
inc di
loop s2
pop dx
pop cx
pop bx
pop ax
ret
printstr:
push bx
push cx
push si
push di
mov bx,0b800h
mov si,160*11+60
mov es,bx
s: mov cl,
mov ch,0
jcxz over
mov ch,2
mov es:,cx
inc di
add si,2
jmp s
over:
pop di
pop si
pop cx
pop bx
ret
code ends
end start
</code></pre>
<p>实验结果:<br>
<img src="https://tva4.sinaimg.cn/large/006rkvIPgy1glm2a651zdj305901da9v.jpg" alt="和" loading="lazy"></p>
<p>将line3改成:<br>
strdb 'another try',0</p>
<p>line 12改成:<br>
mov al,4</p>
<p>结果:<br>
<img src="https://tvax4.sinaimg.cn/large/006rkvIPgy1glm2c4v7yaj305301k3yc.jpg" alt="h3" loading="lazy"></p>
<p>基于运行结果,理解源代码,以及,组合使用转移指令call和ret实现子程序的原理与方法。具体地,在 line18-40中:</p>
<ul>
<li>
<p>line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?</p>
</li>
<li>
<ul>
<li>line 19-22是为了防止寄存器在子程序和主程序起冲突而设置的,以便在主程序中恢复原先的寄存器中的内容;line36-39就是寄存器内容恢复的过程</li>
</ul>
</li>
<li>
<p>line30的功能是什么?</p>
</li>
<li>
<ul>
<li>line30是像实验1中的一样,将分别表示颜色和数值的cx送入显存空间之中,以便显示出来;</li>
</ul>
</li>
</ul>
<h2 id="实验任务3">实验任务3</h2>
<p>使用任意文本编辑器,录入汇编源程序task3.asm;</p>
<p>子程序num2str:</p>
<ul>
<li>功能:把0~2559之间的任意整数转换成数字字符串,例如,把1984转换成'1984'
<ul>
<li>入口参数 要转换的整数 —> ax</li>
<li>数字字符串的起始地址 —> ds:di (其中:数字字符串所在段的段地址—> ds,字符串 起始地址的偏移地址—>di)</li>
</ul>
</li>
<li>出口参数:无</li>
</ul>
<p>代码:</p>
<pre><code class="language-asm">assume cs:code,ds:data
data segment
xdw 1984
str db 16 dup(0)
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,x;此处的标号x表示x处的内容,等同于ds:
mov di,offset str
call num2str
mov ax,4c00h
int 21h
num2str:
push ax
push bx
push cx
push dx
mov cx,0;用来进行计数,以方便之后进行字符串的处理
mov bl,10
s1:div bl
inc cx
mov dl,ah;ah存储的是每一次的余数
push dx
mov ah,0
cmp al,0;当除法的结果为0的时候结束循环
jne s1;不相等继续循环
s2:pop dx
add dl,30h
mov ,dl
inc di
loop s2
pop dx
pop cx
pop bx
pop ax
ret
code ends
end start
</code></pre>
<p>子任务1</p>
<p>对task3.asm进行汇编、链接,得到可执行程序后,在debug中使用u命令反汇编,使用g命令执行 到line15(程序退出之前),使用d命令查看数据段内容,观察是否把转换后的数字字符串'1984'存放 在数据段中str标号后面的单元。</p>
<p>debug结果如下:</p>
<p><img src="https://tvax3.sinaimg.cn/large/006rkvIPgy1glta6vm60ej30hq0c1dg3.jpg" alt="h1" loading="lazy"></p>
<p>子任务2</p>
<p>对task3.asm源代码进行修改、完善,把task2.asm中用于输出以0结尾的字符串的子程序加进来, 实现对转换后的字符串进行输出。</p>
<p>代码:</p>
<pre><code class="language-asm">assume cs:code,ds:data
data segment
xdw 1234,1234
str db 16 dup(0)
data ends
code segment
start:
mov ax,data
mov ds,ax
mov ax,x;此处的标号x表示x处的内容,等同于ds:
mov di,offset str
call num2str
mov si,offset str
call printstr
mov ax,4c00h
int 21h
num2str:
push ax
push bx
push cx
push dx
mov cx,0;用来进行计数,以方便之后进行字符串的处理
mov bl,10
s1:div bl
inc cx
mov dl,ah;ah存储的是每一次的余数
push dx
mov ah,0
cmp al,0;当除法的结果为0的时候结束循环
jne s1;不相等继续循环
s2:pop dx
add dl,30h
mov ,dl
inc di
loop s2
pop dx
pop cx
pop bx
pop ax
ret
printstr:
push bx
push cx
push si
push di
mov bx,0b800h
mov es,bx
mov di,11*160+64
s: mov cl,
mov ch,0
jcxz over
mov ch,2
mov es:,cx
inc si
add di,2
jmp s
over: pop di
pop cx
pop cx
pop bx
ret
code ends
end start
</code></pre>
<p>效果:</p>
<p><img src="https://tva2.sinaimg.cn/large/006rkvIPgy1glm9wpjtctj30hv0bz0sz.jpg" alt="h4" loading="lazy"></p>
<p>修改line3的值效果:</p>
<p><img src="https://tvax4.sinaimg.cn/large/006rkvIPgy1glm9u1ji8sj30hv0bx74g.jpg" alt="h2" loading="lazy"></p>
<p><img src="https://tva4.sinaimg.cn/large/006rkvIPgy1glm9unbfyxj30hs0bzt8v.jpg" alt="h3" loading="lazy"></p>
<h2 id="实验任务4">实验任务4</h2>
<p>使用任意文本编辑器,录入汇编源程序task4.asm。</p>
<p>代码:</p>
<pre><code class="language-asm">assume cs:code, ds:data
data segment
str db 80 dup(?)
data ends
code segment
start:
mov ax, data
mov ds, ax
mov si, 0
s1:
mov ah, 1
int 21h
mov , al
cmp al, '#'
je next;如果输入字符等于#跳转到next处进行输出
inc si
jmp s1
next:
mov cx, si;一个字符占一个字节,因此,si就是字符数;
mov si, 0
s2: mov ah, 2
mov dl,
int 21h
inc si
loop s2
mov ah, 4ch
int 21h
code ends
end start
</code></pre>
<p>效果截图:<br>
<img src="https://tva4.sinaimg.cn/large/006rkvIPgy1glma10olklj306101s741.jpg" alt="h5" loading="lazy"></p>
<p>line12-19行的作用是:读入字符,直到遇见#为止;</p>
<p>line21-27实现的作用是:输出从键盘输入的字符串;</p>
<h2 id="实验任务5">实验任务5</h2>
<p>在visual studio集成环境中,编写一个简单的包含有函数调用的c程序。代码如下:</p>
<pre><code class="language-c">#include<stdio.h>
int sum(int, int);
int main()
{
int a=2,b=7,c;
c=sum(a,b);
return 0;
}
int sum(int x,int y)
{
return (x+y);
}
</code></pre>
<p>在line7, line13分别设置断点,在调试模式下,查看反汇编代码。</p>
<p>分析反汇编代码,从汇编的角度,观察高级语言中参数传递和返回值是通过什么实现的,以及,参数入 栈顺序,返回值的带回方式,等等。</p>
<p><img src="https://tvax2.sinaimg.cn/large/006rkvIPgy1glmaz99m3oj30bz05174a.jpg" alt="h6" loading="lazy"></p>
<p>从上面的汇编代码我们可以看到,在函数的入口,将和中的值先后push进栈中,然后通过call命令跳转到sum函数处。待函数完成后通过mov指令,将eax赋值给完成函数运算。</p>
<p><img src="https://tva2.sinaimg.cn/large/006rkvIPgy1glmb76yesvj30f60cwgm2.jpg" alt="h7" loading="lazy"></p>
<p>int sum到return (x+y)的反汇编代码我们可以看出,显示将重要的寄存器入栈保护,return操作就是将的值加到中去,然后寄存器出栈进行寄存器恢复,然后ret返回主函数,完成运算。</p>
<h2 id="实验结论">实验结论</h2>
<ul>
<li>本次实验使我了解了此程序的编写过程,以及8086中80*25彩色模式的显示原理。</li>
<li>能够熟练的使用jmp、loop、jcxz等跳转指令,根据跳转的不同原理完成各项功能。</li>
<li>能够综合使用ret、call、je、jz、cmp等指令完成综合的子程序,循环分支结构代码的编写。</li>
<li>通过对c语言的返汇编实验,将汇编与高级语言结合起来,更加加深了对汇编语言的理解。</li>
</ul>
</div>
<div id="MySignature" role="contentinfo">
We Turn Not Older With Years ,But Newer Everyday!<br><br>
来源:https://www.cnblogs.com/lszz/p/14129032.html
頁:
[1]