汇编语言实验2 汇编源程序编写与汇编、调试

四、实验结论

1. 实验任务1

给出ex1.asm源代码

; ex1.asm
assume cs:code
code segment
    mov ax, 0b810h
    mov ds, ax

    mov byte ptr ds:[0], 1
    mov byte ptr ds:[1], 1
    mov byte ptr ds:[2], 2
    mov byte ptr ds:[3], 2
    mov byte ptr ds:[4], 3
    mov byte ptr ds:[5], 3
    mov byte ptr ds:[6], 4
    mov byte ptr ds:[7], 4
    mov ah, 4ch

    int 21h
    code ends
end

给出使用masm、link工具汇编、链接的命令行及运行结果截图

给出使用debug调试的截图。结合可执行文件加载后寄存器CX的值,使用u命令精确反汇编截图

查看PSP的命令及截图

使用g命令执行到line16退出执行之前,操作截图

2. 实验任务2

给出ex2.asm源代码

; ex2.asm
assume cs:code
code segment
    mov ax, 0b810h
    mov ds, ax

    mov bx, 0
    mov ax, 101H
    mov cx, 4
s:  mov [bx], ax
    add bx, 2
    add ax, 101H
    loop s

    mov ah, 4ch
    int 21h
code ends
end

给出使用masm、link工具汇编、链接的命令行及运行结果截图

给出使用debug调试的截图。结合可执行文件加载后寄存器CX的值,使用u命令精确反汇编截图

灵活使用t命令/p命令、g命令,对ex2.exe进行调试的截图

回答问题

结合上述实验和观察,分析、对比ex2.asm和ex1.asm,它们实现的是否是相同的功能和效果?在具体实现上有什么不同?

答:作用相同,都是向b810:0000开始的内存空间中写入0101 0202 0303 0404四个字。在具体实现上,ex1.asm使用8行(几乎)重复的代码完成写入操作,而ex2.asm使用了loop结构完成写入操作。

3. 实验任务3

给出源代码

; ex3.asm
assume cs:code
code segment
    mov ax, 0b800h
    mov ds, ax

    mov bx, 07b8h
    mov cx, 16

s:  mov [bx], 0237h
    inc bx
    inc bx
    loop s

    mov ah, 4ch
    int 21h
code ends
end

给出运行结果截图

把填充的字数据,从0237H 改成0239H,再次保存后,汇编、链接、运行,观察结果。

把填充的字数据,从0237H 改成0437H,再次保存后,汇编、链接、运行,观察结果。

猜测并分析,这个字数据中高位字节里存放的是什么信息,低位字节里存放的是什么信息。

容易想到,37h为字符'7'对应的ASCII码,39h为字符'9'对应的ASCII码。在网络上检索到相关资料:

Reference:(Belal Hashmi, Assembly Language Programming Lecture Notes, Chap. 6)

The video device is seen by the computer as a memory area containing the ASCII codes that are currently displayed on the screen ...... Therefore if that appropriate block of the screen is cleared, the screen will be cleared. If the ASCII of ‘A’ is placed somewhere in that block, the shape of ‘A’ will appear on the screen at a corresponding place ......  This correspondence must be defined as the memory is a single dimensional space while the screen is two dimensional having 25 rows and 80 columns. The memory is linearly mapped on this two dimensional space, just like a two dimensional is mapped in linear memory. There is one word per character in which a byte is needed for the ASCII code and the other byte is used for the character’s attributes discussed later. Now the first 80 words will correspond to the first row of the screen and the next 80 words will correspond to the next row. By making the memory on the video controller accessible to the processor via the system bus, the processor is now in control of what is displayed on the screen ......  It was fixed at the physical memory location of B8000. The first byte at this location contains the ASCII for the character displayed at the top left of the video screen ...... The second byte in the word designated for one screen location holds the foreground and background colors for the character. This is called its video attribute. So the pair of the ASCII code in one byte and the attribute in the second byte makes the word that corresponds to one location on the screen. The lower address contains the code while the higher one contains the attribute. The attribute byte as detailed below has the RGB for the foreground and the background.

由于0x07b8 = 1976 = 988*2 = (12*80+28)*2,因此向b800:07b8写入数据就相当于向显示器第13行第29个字符位置写入数据。同时,0x0237就表示前景颜色为绿色的字符'7';0x0239就表示前景颜色为绿色的字符'9';0x0437就表示前景颜色为红色的字符'7'。从下图也可以看出第一个字符出现的位置正是显示器第13行第29个字符。

同理,0x1737就表示前景颜色为白色、背景颜色为蓝色的字符'7'。

4. 实验任务4

程序源代码

 1 ; ex4.asm
 2 assume cs:code
 3 code segment
 4     mov ax, 0
 5     mov ds, ax
 6 
 7     mov bx, 200h
 8     mov al, 0
 9     mov cx, 40h
10 
11 ; clear memory
12 t:  mov byte ptr [bx], 0
13     inc bx
14     loop t
15     mov bx, 200h
16     mov cx, 40h
17 
18 s:  mov byte ptr [bx], al
19     inc bx
20     inc al
21     loop s
22 
23     mov ah, 4ch
24     int 21h
25 code ends
26 end

汇编、链接无误后,灵活使用debug的t命令、g命令、p命令调试,用d命令查看0:200~0:23F,确认是否将0~63传送至此段内存区域。

首先执行至Line 17,确保0:200h附近的空间数据为0;然后执行至Line 23,查看内存。

可见0~63被正确传送至此段内存区域。

5. 实验任务5

填空以后的源代码

 1 ; ex5.asm
 2 assume cs:code
 3 code segment
 4     mov ax,   cs  
 5     mov ds, ax
 6 
 7     mov ax, 20h
 8     mov es, ax
 9 
10     mov bx, 0
11     mov cx,   0  
12 
13 s:  mov al, [bx]
14     mov es:[bx], al
15     inc bx
16     loop s
17 
18     mov ah, 4ch
19     int 21h
20 code ends
21 end

以文字方式说明空白处填写依据

注意Line 11中cx首先被置为0。为了确定Line 1 - 16的指令长度,cx首先被置为0,接着通过对程序进行debug查看Line 1 - 16的指令长度,再将Line 11修改为该长度。将ax设为cs是因为确定代码段起始位置。

由上图看出,将cx修改为16h即可。修改之后再次编译、链接。

首先查看20:0附近的内存,可以看出内存中的数据仍为实验4中移动的数据。程序执行至Line 17,再次查看内存。

可以看出程序被成功复制。

6. 实验任务6

源代码

 1 ; example.asm
 2 
 3 global _start
 4 
 5 section .data
 6     msg db "a simple test", 0xa
 7     len equ $ - msg
 8 
 9 section .text
10 _start:
11     mov eax, 4
12     mov ebx, 1
13     mov ecx, msg
14     mov edx, len
15     int 0x80
16 
17     mov eax, 1
18     mov ebx, 7
19     int 0x80

对example.asm进行汇编、链接、运行。

将example.asm中line18行中寄存器ebx的值改为7,重新汇编、链接、运行。

五、实验总结

关于DOS程序运行结束后的返回

在以上ex1-ex5中,程序均包含以下两行代码

mov ah, 4ch
int 21h

翻阅书本,在P81有

这两条指令所实现的功能就是程序返回。

在目前阶段,我们不必去理解int 21h指令的含义,和为什么要在这条指令的前面加上指令mov ax, 4c00h。我们只要知道,在程序的末尾使用这两条指令就可以实现程序返回。

此两行代码涉及DOS中Interrupt(中断)的知识,详细内容可在书本Chap. 12-13被参照。此处仅做最基本的介绍。int为一条x86指令,其作用为产生软件中断,

在计算机科学中,中断Interrupt)是指处理器接收到来自硬件或软件的信号,提示发生了某个事件,应该被注意,这种情况就称为中断。软件中断。是一条CPU指令,用以自陷一个中断。由于软中断指令通常要运行一个切换CPU至内核态(Kernel Mode/Ring 0)的子例程,它常被用作实现系统调用。

int指令的格式为

int X

其中X用于指出具体的中断。int 21h表示调用系统服务。该指令根据ax寄存器的高字节(ah)中的数据,又可实现不同的功能。当ah为4ch时,表示终止程序。故程序均包含该两行代码以正常退出。对于其它的中断,读者可参照该网站以及该pdf获取更多信息。

posted @ 2020-10-31 23:05  xywei0905  阅读(401)  评论(2编辑  收藏  举报