实验四 汇编应用程序和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结尾的字符串的子程序加进来,实现对转换后的字符串进行输出。

   源代码:

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

输出结果如下:

    

   把task3.asm源代码中,line3中整数改成0~2559之间的任意数值,运行测试,观察结果。

    line3中整数改成2020:

   

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

汇编、链接、运行程序,输入一个字符串并以#结束(比如,2020,   bye#)观察运行结果。

 

结合运行结果,理解程序功能,了解软中断指令。具体地:

  • line12-19实现的功能是?

    通过跳转指令不断使用int 21中的1号子功能,从键盘输入单个字符,若该字符不为‘#’,则将其存入到指定的内存单元中。

  • line21-27实现的功能是?

     通过loop指令循环输出先前存入的字符。

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);
}

在line7, line13分别设置断点,在调试模式下,查看反汇编代码。

 设置断点:

在调试模式下查看反汇编代码:

分析反汇编代码,从汇编的角度,观察高级语言中参数传递和返回值是通过什么实现的,以及,参数入栈顺序,返回值的带回方式,等等。

答 :高级语言中参数传递和返回值是通过寄存器实现的,函数的返回值放在eax中返回;由上图反汇编后的代码可以看出,参数b先入栈,参数a后入栈,所以参数入栈的顺序为从右向左入栈;由上面的反汇编代码还可以看出,在程序的末尾会使用pop恢复原来寄存器的值,后面紧跟着数条指令,通过查阅资料得知:以main函数的反汇编为例,后面的指令功能分别为:add esp,0E4h:恢复esp,与上面的sub esp,0E4h相对应;cmp ebp,esp:检查esp是否正常,不正常就进入下边的call里面debug;call _RTC_CheckEsp(0441217h):处理可能出现的堆栈异常,如果有的话,就会陷入debug;mov esp,ebp   pop ebp:恢复原来的esp和ebp,让上一个调用函数正常使用;ret:将返回地址存入eip,转移流程。

posted @ 2020-12-17 20:08  ViVoi  阅读(153)  评论(1)    收藏  举报