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

1. 实验任务1

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

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,0b800h
    mov es,ax
    mov si,1840-16
    mov cx,3
    mov bx,0
s:    push cx
    mov cx,16
    mov di,0
s0:    mov al,ds:[di]
    mov ah,ds:[bx+16]
    mov es:[si],ax
    inc di
    inc si
    inc si
    loop s0
    inc bx
    add si,160-32
    pop cx
    loop s
    mov ah,4ch
    int 21h
code ends
end start

  

 

 

 

  思路:首先在data段中以字节的形式存入字符串`welcome to masm!`,和数字 2h,24h,71h(数字中存的是要设置显示颜色的三个对应颜色含背景,详细见书189页)。代码要将`welcome to masm!`这十六个字节的数据复制到显示缓冲区中,并且显示三次,显然涉及到嵌套循环,在处理嵌套循环的时候需要注意将外循环的cx存入栈中,然后重新设置内层循环的cx,内层循环结束以后,将之前存的值出栈到cx中,实现两层循环。接下来就是把数据放入显示缓存区的操作:显示缓冲区中间的三行对应偏移地址为1760-1919,1920-2079,2080-2239,第一行的中间位置为1840,要找到放入字符的第一个位置,一共16个字符,1840前面应该放8个字符就正好能实现左右对称的效果,显示1字符需要两个字节的单元(高位颜色,低位数据),所以首位应该是1840-16。接下来就是将ds:[0~16]中的数据,和ds:[17-19]中的颜色值复制到显示缓冲区中,分别将他们存入al和ah,然后放入es:[si](显示缓冲区的位置)中即可。每次存入后si+2指向下一个显示数据的地方,bx+1寻找下一个颜色,si由1840-16,经过了16次加2,移动到了字符串的末尾,为了让位置移到下一行字符开始的位置,加上160-32。

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

 

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

   基于运行结果,理解源代码,以及,组合使用转移指令call和ret实现子程序的原理与方法。

  具体地,在 line18-40中: line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?

    push命令是为了保存之前操作过的的相关寄存器的值到栈中,pop命令是在程序相关操作结束后,将这些值出栈到  对应的寄存器,和实验1中将cx入栈再出栈的原理一样。

  line30的功能是什么?

    将str段的数据存入低位,颜色存入高位,然后放入显示缓冲区中。

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

3. 实验任务3

  使用任意文本编辑器,录入汇编源程序task3.asm。 子程序num2str: 功能:把0~2559之间的任意整数转换成数字字符串,例如,把1984转换成'1984' 入口参数 要转换的整数 —> ax 数字字符串的起始地址 —> ds:di (其中:数字字符串所在段的段地址—> ds,字符串 起始地址的偏移地址—>di) 出口参数:无

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
        pop ax
        jmp ax

code ends
end start

  阅读源代码,理解子程序num2str的汇编实现。

    子任务1 对task3.asm进行汇编、链接,得到可执行程序后,在debug中使用u命令反汇编,使用g命令执行到     line15(程序退出之前),使用d命令查看数据段内容,观察是否把转换后的数字字符串'1984'存放 在数据段中str标号后   面的单元。

     子任务2 对task3.asm源代码进行修改、完善,把task2.asm中用于输出以0结尾的字符串的子程序加进来, 实现对  转换后的字符串进行输出。 预期输入结果如下:

 

 反汇编:

 

子任务2

assume cs:code, ds:data
data segment
        x dw 2000
        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
        pop ax
        jmp ax

code ends
end start

 

 

4. 实验任务4 使用任意文本编辑器,录入汇编源程序task4.asm。

  使用任意文本编辑器,录入汇编源程序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 21h中的1号子功能 功能:从键盘输入单个字符,读入键盘输入,遇到#跳转到next执行

    line21-27实现的功能是?

      用int 21h中的2号子功能在屏幕上输出单个字符,即将输入的内容输出。

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

  设置断点调试

 

  反汇编

  

 

  在汇编语言中,在进行参数传递时,将参数的值保存到寄存器中,然后将寄存器依次压入栈中,如本题中将参数a,b传到eax寄存器和ecx寄存器中,将寄存器入栈。调用sum函数时,跳到sum函数处执行, 将寄存器eax中的值赋值给c。多个参数的入栈顺序是从右往左入栈。

 

 

 

 

 

 

 

  

 

posted @ 2020-12-17 23:55  wallsl  阅读(82)  评论(1)    收藏  举报