实验4 汇编应用编程和c语言程序反汇编分析
一、实验目的
1. 理解80×25彩色字符模式显示原理
2. 理解转移指令jmp, loop, jcxz的跳转原理,掌握使用其实现分支和循环的用法
3. 理解转移指令call, ret, retf的跳转原理,掌握组合使用call和ret/retf编写汇编子程序的方法,掌握 参数传递方式
4. 理解标志寄存器的作用
5. 理解条件转移指令je, jz, ja, jb, jg, jl等的跳转原理,掌握组合使用汇编指令cmp和条件转移指令实 现分支和循环的用法
6. 了解在visual studio/Xcode等环境或利用gcc命令行参数反汇编c语言程序的方法,理解编译器生成 的反汇编代码
7. 综合应用寻址方式和汇编指令完成应用编程
二、实验准备
实验前,请复习/学习教材以下内容:
第9章 转移指令的原理
第10章 call和ret指令
第11章 标志寄存器
三、实验内容
1. 实验任务1 教材「实验9 根据材料编程」(P187-189)
编程:在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串'welcome to masm!'。
代码如下:
assume cs:code, ds:data
data segment
db 'welcome to masm!'
db 2,36,113
data ends
code segment
start:
mov ax, data
mov ds, ax
mov ax,0b800h
mov es,ax
mov di,0720h
mov cx,3
mov bx,0
mov si,0
s0: push cx
mov cx,16
mov si,0
s1: mov al,ds:[si]
mov ah,ds:[bx+16]
mov es:[di],ax
inc si
add di,2
loop s1
inc bx
pop cx
add di,128
loop s0
mov ah,4ch
int 21h
code ends
end start
运行截图:

2. 实验任务2
编写子程序printStr,实现以指定颜色在屏幕上输出字符串。调用它,完成字符串输出。
使用任意文本编辑器,录入汇编源程序task2.asm
代码如下:
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的功能是什么?
答:将字符与颜色信息写入显存。
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
反汇编:


使用g命令运行,并查看结果:

子任务2:
代码如下:
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
结果截图:

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实现的功能是?
答:若输入#,跳转至next;否则继续循环。
line21-27实现的功能是?
答:输出输入的字符。
5. 实验任务5
在visual studio集成环境中,编写一个简单的包含有函数调用的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);
}
设置断点:

反汇编:



浙公网安备 33010602011771号