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

1.实验任务1

 1 assume cs: code,ds: data
 2 
 3 data segment
 4 db 119,101,108,99,111,109,101,32,116,111,32,109,97,115,109,33  ;welcome to masm!(不包含属性值)
 5 data ends
 6 
 7 code segment
 8 start:
 9 mov ax, 0b800h
10 mov ds, ax     ;设置内存段地址
11 mov bx, data
12 mov es, bx    ;data段的段地址
13 
14 mov di, 0    ;第一行
15 mov bx, 1984  ;根据计算,在屏幕的正中间为160*12+64
16 mov cx, 16
17 s0:
18 mov al, es:[di]    
19 
20 mov [bx], al    ;ascii
21 inc bx
22 mov word ptr [bx], 2    ;feature, 0 000 0 010
23 inc bx
24 
25 inc di
26 loop s0
27 
28 mov di, 0  ;第二行
29 mov bx, 2144  ;在第一行的基础上+160
30 mov cx, 16
31 s1:
32 mov al, es:[di]    
33 
34 mov [bx], al    ;ascii
35 inc bx
36 mov word ptr [bx], 36    ;绿地红色 0 010 0 100
37 inc bx
38 
39 inc di
40 loop s1
41 
42 mov di, 0  ;第三行
43 mov bx, 2304  ;在第二行的基础上+160
44 mov cx, 16
45 s2:
46 mov al, es:[di]    
47 
48 mov [bx], al    ;ascii
49 inc bx
50 mov word ptr [bx], 113    ;白底蓝色 0 111 0 001
51 inc bx
52 
53 inc di
54 loop s2
55 
56 mov ax, 4c00h
57 int 21h
58 
59 code ends
60 end start

2.实验任务2

 1 assume cs:code, ds:data
 2 data segment
 3 str db 'try', 0
 4 data ends
 5 code segment
 6 start:
 7 mov ax, data
 8 mov ds, ax  ;设置入口参数ds
 9 mov si, offset str  ;设置入口参数si
10 mov al, 2  ;设置入口参数al
11 call printStr
12 mov ah, 4ch
13 int 21h
14 printStr:
15 push bx
16 push cx
17 push si
18 push di
19 mov bx, 0b800H
20 mov es, bx
21 s: mov cl, [si]
22 mov ch, 0
23 jcxz over
24 mov ch, al
25 mov es:[di], cx
26 inc si
27 add di, 2
28 jmp s
29 over:
30 pop di
31 pop si
32 pop cx
33 pop bx
34 ret
35 code ends
36 end start

修改line3,line12后的运行结果截图

 

 

 

这样用的目的是在子程序的开始将子程序中所有用到的寄存器中的内容都保存起来,在子程序返回前再回复,避免寄存器的冲突问题。

 

 

将当前内存中字符的值和属性等存入彩色字符模式显示缓冲区。

3. 实验任务3

 

 

 

 

 

 

 使用d命令查看数据段的数据,1984已经转换为字符串

 

 

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 show  ;调用task2的子程序

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

show:
push bx
push cx
push si
push di
mov bx, 0b800H
mov es, bx
mov di, 0
s3: 
mov cl, [si]
mov ch, 0
jcxz over
mov ch, al
mov es:[di], cx
inc si
add di, 2
jmp s3
over:
pop di
pop si
pop cx
pop bx
ret

code ends
end start

 运行测试截图

4. 实验任务4

 

 1 assume cs:code, ds:data
 2 data segment
 3 str db 80 dup(?)
 4 data ends
 5 code segment
 6 start:
 7 mov ax, data
 8 mov ds, ax
 9 mov si, 0
10 s1:
11 mov ah, 1
12 int 21h
13 mov [si], al
14 cmp al, '#'
15 je next
16 inc si
17 jmp s1
18 next:
19 mov cx, si
20 mov si, 0
21 s2: mov ah, 2
22 mov dl, [si]
23 int 21h
24 inc si
25 loop s2
26 mov ah, 4ch
27 int 21h
28 code ends
29 end start

 

 

 

 将从键盘输入的字符存入内存中,直到当前字符为“#”,则跳转到next子程序。

在屏幕上显示出line12-19存入内存的字符串。

 5. 实验任务5

观察反汇编过程,发现函数的参数入栈顺序与形参的顺序相反。例如sum函数的形参顺序是a,b,参数的实际入栈顺序为b,a,返回值通过寄存器(本实验中为eax)返回。

 

 

 

 

形参的传递是通过内存单元传递,将内存单元的值传递到子函数里。寄存器ebp的值经过一定的修改,作为地址寄存器。

 

 

 

 

posted @ 2020-12-13 14:45  candice12321  阅读(95)  评论(2)    收藏  举报