8086 assembly language compiler and link learning (d) of the 8086 assembler

1,8086 assembler source process from writing to be executed

  Introduced earlier, to write and debug assembly programs through the debug mode. But with the in-depth study, written assembler will be more complex, and the A through debug commands one by one to write the assembly instructions are very inefficient.

  Thus, there is going to introduce the concept of 8086 compilation of source code, so that we can write assembler by way of text, and through a series of measures will be converted into the final binary executable program.

A compilation of source code from writing to perform can be divided into several stages:

  1. developers to write text in the form of assembler source

  2. assembler source code to compile and generate the target file, the target file link and generate an executable file

  3. Run the executable file

Here a simple example will be described in detail (masm5 windows operating system environment in the Examples):

1. Preparation of assembler source

  Written in text form through the system comes with Notepad or a more advanced text editor, assembler source, will be named demo.asm, the program reads as follows:

the ASSUME cs: codesg 

codesg segment 

    mov AX, 0123H
     mov BX, 0456H
     the Add   AX, BX
     the Add   AX, AX 
    
    mov AX, 4c00H 
     int 21H; Interrupt exit the program 

codesg ends 

End

2. compilation of source code to compile, link

Compile:

  Through the command line to run masm.exe, local file address assembler source files let the user input need to be compiled (Source filename  : ). If the file can enter a file name (for example demo.asm) only in the current working directory, otherwise you need to enter an absolute location path (such as c: \ MASM \ ASM \ demo.asm) . In addition, if the default source file suffix expansion called [.ASM], if a class of .txt files, you need to enter the full name (for example, c: \ MASM \ ASM \ demo.txt) .

  Next will be stored and the name of the path (Object filename user input generated .obj object file  : ), for example, c: \ MASM \ ASM \ demo.obj . Source listing behind and Cross-reference masm allows the compiler to generate an intermediate file during compilation of the results, where you can skip directly enter, do not let the compiler generates.

  If the error log (0 Errors) does not appear, then you can find the target file generated in the Object filename specified path.

  

link:

  Link.exe executed from the command line, let the user needs to enter the linked object file (Object Modules : ) such as c: \ MASM \ ASM \ demo.obj . Absolute path / relative path and file when compiling the rules consistent use and masm.exe full name .

  Then the storage path name and user needs to input the generated .exe executable file (the Run File  : ), for example, c: \ MASM \ ASM \ demo.exe . Back List File and Libraries used to specify and obj files and links subroutine library program , because the program here is very simple, do not need to use, choose Enter to skip.

  Barring unforeseen circumstances, demo.exe executable file will appear in the specified location. ( NO Stack segment warning (not an error) program does not stack segment, this is a bad habit, but here temporarily ignored)

   

3. Run the executable file

  Command line window run demo.exe, will find no effect. This is because our program is only a simple operation of the CPU registers, the program immediately after the execution is over, and not permanent.

  For this execution is completed immediately exits the assembler, it can be debugged by debug.exe to understand the implementation process in the middle.

  Use the debug command to execute the executable file path as a parameter, you can debug the executable file (for example: debug c: \ MASM \ asm \ DEMO.EXE) . Can be seen from the screenshot, the U decompile instruction, the source text is accurate conversion to binary executable program .

  

2,8086 assembler source syntax description

  The assembler source program content can be roughly divided into two types, one is the machine-executable instructions corresponding to the instruction (e.g.: mov ax, 0123H add ax, bx , etc.), the other is a directive (e.g.: codesg segment).

  伪指令和可执行指令的区别在于,可执行指令是和CPU的硬件挂钩的,会被汇编器转换为对应的机器指令;而伪指令的作用是为了控制编译过程而存在的,其仅由汇编器处理,没有对应的机器指令,不会被CPU执行

assume cs:codesg

codesg segment

    mov ax,0123H
    mov bx,0456H
    add  ax,bx
    add  ax,ax
    
    mov ax,4c00H
    int 21H  ;中断退出程序
codesg ends 

end

  以前面的源程序举例分析:一个好的源程序需要具备基础的层次结构,而汇编源程序通过源程序层面上的段来进行层次划分的。可以在一个段中存放代码,数据或者将某一个段当作栈来使用,这也和前面介绍的CPU的几种类型的段寄存器对应。

  因为程序是结构化的,所以要理解上面这段程序不能完全从上到下的进行理解,而是要进行有机的拆解:

  首先,我们定义了一个段,名为codesg,段的程序界限由segmentends这对关键字控制,也就是 codesg segment  。。。 codesg ends,在这两行语句中间的所有内容都属于这个段。codesg段是作为代码段来使用的,所以内容都是程序指令。

  接着明确的告诉汇编器,将codesg作为程序段(cs)来使用,也就是第一行assume cs:codesg的意义,关键字为assume cs:

  最后指明程序的结束,也就是最后一行end关键字的作用。

  这里segment 、ends、end等关键字构成的指令都是伪指令

其它需要注意的点:

  1. 8086汇编的注释是通过分号 ;来识别的,汇编器会将每一行分号后面的内容当作注释,自动忽略后面一整行的内容。  

  2. 在codesg的段内容中出现了一个前面没有出现过的指令int。int指令代表着中断(interrupt),由于当前程序是由DOS调用执行的,而mov ax,4c00H int 21H这两句指令就是通过中断结束当前程序并返回到DOS中。关于中断的内容会在后续的博客里做详细介绍。

Guess you like

Origin www.cnblogs.com/xiaoxiongcanguan/p/12341417.html