实验4 汇编应用编程和c语言程序反汇编分析

 

(一)实验任务一

教材「实验根据材料编程」(P187-189)
编程:在屏幕中间分别显示绿色、绿底红色、白底蓝色的字符串'welcome to masm!' 。 
源代码如下:

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 di,0
        mov si,0
        mov cx,16

    s0: 
        mov al,ds:[di]
        mov ah,02h
        mov es:[si+680h],ax

        mov al,ds:[di]
        mov ah,24h
        mov es:[si+720h],ax

        mov al,ds:[di]
        mov ah,71h
        mov es:[si+7C0h],ax

        add di,1
        add si,2
        loop s0

        mov ax,4c00h
        int 21h

code ends
end start

使用masmlink对task4_1.asm进行汇编、链接,得到并运行task4_1.exe文件,得到结果:

 

(二)实验任务二

编写子程序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

再次汇编、运行程序,观察运行结果

 

 两个问题:

1)这组对称使用的push、pop,这样用的目的是什么?

  目的是利于恢复各寄存器的值,以免出现意外情况;

2)line30的功能是什么?

  将数据段的当前字符复制到屏幕显示的目标地址中。

(三)实验任务三

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

对task3.asm进行汇编、链接,得到可执行程序后,在debug中使用u命令反汇编,使用g命令执行 到line15,使用d命令查看数据段内容:

 

 

对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
        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之间的任意数值,运行测试,结果如下所示

 

发现不同的数字字符的属性和前景于背景颜色都不一样

 

(四)实验任务四

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

汇编、链接、运行程序,输入字符串

1)line12-19实现的功能是?

  通过中断转移指令和条件跳转指令,判断输入的字符是否为#,若不为则读入将其保存在栈内

2)line21-27实现的功能是?

  通过循环指令,输出存在栈内的字符

 

(五)实验任务五

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

反汇编:

C语言中参数传递的方式是传递形参,汇编的角度是将形参存储在寄存器中然后进行相关运算;当进行函数调用的时候,对参数进行按自右向左的顺序,先存在寄存器中,利用寄存器进行入栈操作。

实验总结

1.通过本次实验我了解并掌握了如何对屏幕显示区域进行赋值操作;

2.了解了高级语言中参数传递,从汇编的角度是如何传递的,返回值是如何返回的;多个参数的入栈顺序是什么样的;函数调用栈,等等。

posted @ 2020-12-18 09:17  IvyLeaf  阅读(61)  评论(2)    收藏  举报