汇编语言实验4
1. 实验任务1
编程:在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串'welcome to masm!'。
assume cs:code
data segment
db 'welcome to masm!'
data ends
code segment
start: mov ax,data
mov ds,ax
mov ax,0b800h
mov es,ax
mov si,0
mov di,12*160+80
mov cx,16
s1: mov al,ds:[si]
mov ah,00000010B
mov es:[di],ax
inc si
inc di
inc di
loop s1
mov si,0
mov di,13*160+80
mov cx,16
s2: mov al,ds:[si]
mov ah,00100100B
mov es:[di],ax
inc si
inc di
inc di
loop s2
mov si,0
mov di,14*160+80
mov cx,16
s3: mov al,ds:[si]
mov ah,01110001B
mov es:[di],ax
inc si
inc di
inc di
loop s3
mov ax,4c00h
int 21h
code ends
end start
汇编、运行程序:


2. 实验任务2
编写子程序printStr,实现以指定颜色在屏幕上输出字符串。调用它,完成字符串输出。
assume cs:code, ds:data data segment str db 'try', 0 data ends code segment start: mov ax, data mov ds, ax mov si, offset str mov al, 2 call printStr mov ah, 4ch int 21h printStr: push bx push cx push si push di mov bx, 0b800H mov es, bx mov di, 0 s: mov cl, [si] mov ch, 0 jcxz over mov ch, al mov es:[di], cx inc si add di, 2 jmp s over: pop di pop si pop cx pop bx ret code ends end start
汇编、运行程序:


对源程序做如下修改:
把line3改为:
str db 'another try', 0
把line12改为:
mov al, 4

line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?
为了避免子程序和主程序调用时使用寄存器的冲突,在子程序执行前,将数据存入栈中,在子程序返回之前,将栈中数据弹出恢复到寄存器中。
line30的功能是什么?
将cx寄存器中保存的字符和颜色信息移入到显存区域显示
3. 实验任务3
使用任意文本编辑器,录入汇编源程序task3.asm。
assume cs:code, ds:data data segment x dw 1984 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 ah, 4ch 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 cmp al, 0 jne s1 s2: pop dx or dl, 30h mov [di], dl inc di loop s2 pop dx pop cx pop bx pop ax ret code ends end start
子任务1:
对task3.asm进行汇编、链接,执行


使用u命令反汇编

使用g命令执行

使用d命令查看数据段内容

子任务2
对task3.asm源代码进行修改、完善
assume cs:code, ds:data data segment x dw 1984 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 si, offset str mov al,2 call printStr mov ah, 4ch 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 cmp al, 0 jne s1 s2: pop dx or dl, 30h mov [di], 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, 0 s: mov cl, [si] mov ch, 0 jcxz over mov ch, al mov es:[di], cx inc si add di, 2 jmp s over: pop di pop si pop cx pop bx ret code ends end start

将line3中整数改成0~2559之间的任意数值,运行测试,观察结果。
x dw 2000

4. 实验任务4
使用任意文本编辑器,录入汇编源程序task4.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 [si], al cmp al, '#' je next inc si jmp s1 next: mov cx, si mov si, 0 s2: mov ah, 2 mov dl, [si] int 21h inc si loop s2 mov ah, 4ch int 21h code ends end start
运行结果:

line12-19实现的功能是?
从键盘接收输入的字符串,并将字符串保存在data段中,当接收到字符"#"时,接受结束并跳转到next下面的程序。
line21-27实现的功能是?
将从键盘中输入的字符串从data段中取出,并显示在屏幕上。
5. 实验任务5
在visual studio集成环境中,编写一个简单的包含有函数调用的c程序。
在line7, line13分别设置断点



分析反汇编代码,从汇编的角度,观察高级语言中参数传递和返回值是通过什么实现的,以及,参数入 栈顺序,返回值的带回方式,等等。
高级语言中函数的参数传递
在汇编的角度看,参数x,y的数据从右至左入栈,保存在栈中,通过call指令调用函数sum,然后通过访问保存在栈中的x,y的数据进行运算,然后将结果(返回值)保存在寄存器中出栈。
通过ret返回主函数地址。

浙公网安备 33010602011771号