Introduction to 8086 Assembly Language - HelloWorld

    80x86 microprocessor assembly language programming. Learning any programming language inevitably has to cross the hurdle of HelloWorld. Machine-oriented assembly language is different from process/object-oriented high-level language. The details of register, memory allocation and other details in the coding process need to be concerned by programmers; and high-level language programs Such as C language, C++, even if you don't know how printf is implemented, as long as you master the usage method ( encapsulated into modules ). The helloworld program in C language is as simple as only one line of execution:

    printf("Hello,World!");    //Display a string of characters "Hello,World!" in the screen buffer

  The assembly language Helloworld is also very simple, but it makes many beginners puzzled; to use a simple analogy, the above printf(.....) is like a pistol, and the string "Hello, World!" is like a bullet , the function of shooting can be completed by attaching "bullets" to printf. Of course, you can also replace other bullets... Assembly language to output strings to the screen buffer and display, the work that needs to be done is not so intuitive and easy to understand. Assembly language programming is oriented to "registers". Registers are a kind of cache inside the computer CPU ( several times faster than memory, but the capacity is extremely small ), and various operations on data are carried out by "instructions". Also known as assembly instructions, such as ADD AX, BX means add the values ​​of registers AX and BX, and store the result in AX; AX/BX belong to 8086 general-purpose data registers. More assembly knowledge will be explained in later chapters.

  hello.asm  source program


;80x86 assembly language <starter program>
;YPP.20170928
;filenameHello.asm


DSEG SEGMENT
MESS DB 'Hello,World!' ,0DH,0AH,24H
DSEG ENDS


SSEG SEGMENT PARA STACK 
        DW 256 DUP(?)
SSEG ENDS


CSEG SEGMENT
        ASSUME CS:CSEG,DS:DSEG
BEGIN: MOV AX,DSEG
        MOV DS,AX
        MOV DX,OFFSET MESS
        MOV AH,9


        INT 21H
        MOV AH,4CH
        INT 21H
CSEG ENDS
        END BEGIN

 

The first thing a beginner needs to know is that an assembly language program consists of two basic elements: opcodes, operands

Opcode , indicating the operation to be performed, such as addition, subtraction, multiplication, division....

Operand , the object to be operated, can be a number, a string, a storage unit (variable), etc.

This article does not explain too much about the details of the code, because it requires a lot of pre-knowledge to better understand this code.

前置知识包括,二进制数制换算、CPU内存单元寻址、14个16位寄存器组、存储器分段、中断机制等等

    上面的代码编辑或复制到记事本中,另存为.asm文件,这是汇编语言的源代码文件,中间要进行汇编的操作,生成的.obj文件是目标文件,经过链接之后,才能形成最终的可执行文件.exe,整个过程与高级语言的运行过程是大致相仿的。

工具有很多,但还是推荐经典的Masm5.0,

注:编写80x86平台的汇编语言,不要在Win-Vista\7\8.1\10的系统上操作,因为16位程序会出现兼容性问题。

本文均在Windows XP sp3操作系统上编辑、汇编、链接、运行.

Masm5.0下载:http://pan.baidu.com/s/1hsjCcRi  密码:syqo

masm无需安装,解压到某磁盘根目录下,进入DOS操作

 

 Win+R输入CMD进入DOS,先用DOS命令进入MASM5.0文件目录下,同时hello.asm源文件也要复制到同一路径下。

汇编命令格式> masm  xxx.asm 回车   注:DOS下不区分大小写

连续回车,表示使用默认文件名,四个回车下来,会生成Hello.obj文件;如果出现错误应当更改源文件后重新汇编,

0 Warning Errors.....表示没有错误。

没有错误后,下一步进行“Link”链接,命令格式>link xxx

注:链接时要指定文件名,就是刚刚生成的Hello.obj  可以省略后缀,名字不要省略!不然会报错

link hello 回车 三次回车后,正常情况下会生成Hello.exe  那也就是程序的最终形态,直接输入hello就运行了这个程序(不要双击运行,不然DOS窗口一闪而过看不到执行结果)

 

 

HelloWorld字符串被成功输出出来,这个程序也就完成了

源文件Hello.asm——>目标文件Hello.obj——>可执行文件Hello.exe

这就是整个过程。

另外补充一点,由asm到obj在汇编语言中,这个过程叫做“汇编Assembly”;在高级语言中叫做“编译Compile

有的教程中会缺少堆栈段SSEG,没有堆栈段 在masm中也会报错,会提示no stack segment要注意。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324529956&siteId=291194637