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

1. 实验任务1

assume cs:code, ds:data
data segment
     str db 'welcome to masm!',0 ;仿照实验2实现思路以0结尾
data ends

code segment
start:
        mov ax,data
        mov ds,ax
        mov al,2H;传入口参数表示颜色
        mov si,offset str;传入口参数表示的是字符串开始地址
        mov di,720H;传入口参数表示的是写入内存位置的偏移地址
        call printStr;调用子程序
        mov al,24H;与上同,不再解释
        mov di,07C0H
        call printStr
        mov al,71H
        mov di,0860H
        call printStr
      

       mov ah,4ch
        int 21h

printStr:
        push bx;保存寄存器状态
        push cx
        push si
        push di

       mov bx,0b800H
        mov es,bx
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

image

2. 实验任务2

assume cs:code, ds:data
data segment
str db 'another try', 0
data ends
code segment
start:
mov ax, data
mov ds, ax 
mov si, offset str 
mov al,4
call printStr
mov al, 4ch
int 21h
printStr:
push bx
push cx
push si
push di
mov bx, 0b800H
mov es, bx
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

image

image

line19-22, line36-39,这组对称使用的push、pop,这样用的目的是什么?

这是在push指令的使用是在保存寄存器中的值,也就是保护现场,使得下面的子程序能够放心地使用这几个寄存器,而对称的pop指令,则是在子程序功能完成后,返回前,做现场恢复。

line30的功能是什么?

将一个已经按80*25彩色字符模式编辑好的字送入显存显示

3. 实验任务3

子任务1:

image

image

可见,数据已经放入了内存中

子任务2:

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
mov si,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
mov bx, 0b800H
mov di,0
mov es, bx
s:mov cl, [si]
mov ch, 0
jcxz over
mov ch,2
mov es:[di], cx
inc si
add di, 2
jmp s
over:pop dx
pop cx
pop bx
pop ax
ret
code ends
end start

image

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

image

4. 实验任务4

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


image

12-19:利用1号子功能实现循环输入字符串,没有到达输入尽头:#,则继续输入;否则跳转至输出。

21-27:利用2号子功能实现循环输出字符串。


5. 实验任务5

传递参数:

image

子程序内部:

image

显然,由下图知,返回值由eax带回:

image

posted @ 2020-12-17 11:10  bpcl  阅读(113)  评论(2)    收藏  举报