实验四 汇编应用程序和C语言程序反汇编分析
1.实验任务1
assume cs:code, ds:data data segment db 'welcome to masm!' db 2h,24h,71h data ends code segment start: mov ax,data mov ds,ax mov ax,0B872h mov es,ax mov cx,3 ;外层循环三次 mov bx,0 ;记录当前行数 s0: mov dx,cx ;记录下循环次数,也是颜色下标 mov cx,16 ;每行显示16个字符,内存循环16次 mov di,0 ;记录内存循环的次数 mov si,0 ;记录显示的位置 s1: mov al,ds:[di] ;低字节显示文字 mov ah,ds:[bx+16] ;高字节显示颜色 mov es:[si],ax add si,2 ;移动2个显存地址 inc di ;移动到下一个字符 loop s1 mov cx,dx ;恢复外循环的cx值 mov ax,es add ax,0AH mov es,ax ;切换到下一行的位置 inc bx ;移动到下一个颜色 loop s0 mov ax,4c00h int 21h code endsend start

2.实验任务2
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

将第3行的 str db 'try', 0 修改为 str db 'another try', 0 ,结果如下:

将第12行的 mov al, 2 修改为 mov al, 4 ,结果如下
:
基于运行结果,理解源代码,以及,组合使用转移指令call和ret实现子程序的原理与方法。具体地,在line18-40中:
- line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?
在运行子程序printStr前将寄存器bx,cx,si,di中的值保存起来,子程序运行结束后恢复,这样做可以更好地利用有限的寄存器。
- line30的功能是什么?
将cx中存放的字符及其颜色属性送入显存中,实现在屏幕上的显示。
3.实验任务3
使用任意文本编辑器,录入汇编源程序task3.asm。
子程序num2str:
- 功能:把0~2559之间的任意整数转换成数字字符串,例如,把1984转换成'1984'
- 入口参数
- 要转换的整数 —> ax
- 数字字符串的起始地址 —> ds:di (其中:数字字符串所在段的段地址—> ds,字符串起始地址的偏移地址—>di)
- 出口参数:无
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进行汇编、链接,得到可执行程序后,在debug中使用u命令反汇编,使用g命令执行到line15(程序退出之前),使用d命令查看数据段内容,观察是否把转换后的数字字符串'1984'存放在数据段中str标号后面的单元。
汇编、链接后进入debug:

反汇编:

使用g命令执行到line15(程序退出之前):

使用d命令查看数据段内容,观察是否把转换后的数字字符串'1984'存放在数据段中str标号后面的单元:

成功把转换后的数字字符串'1984'存放在数据段中str标号后面的单元。
- 子任务2
对task3.asm源代码进行修改、完善,把task2.asm中用于输出以0结尾的字符串的子程序加进来,实现对转换后的字符串进行输出。
源代码:







浙公网安备 33010602011771号