Write the first assembler hello world in x86 assembly language

Reference article: Write a program in assembly language to output "Hello World!"

This article is updated synchronously in the blog garden, because CSDN does not support the syntax highlighting of assembly language, for a better reading experience, welcome to visit: nefu_ljw blog garden .

1. Prepare the operating environment

First prepare the assembly language operating environment, download it here: https://download.csdn.net/download/ljw_study_in_CSDN/12997354

Open DOSBox0.74-win32-installer, install.

Two, write assembly code

In assembly language, a semicolon ;is used to indicate a comment, similar to C/C++'s //comment.

The fixed syntax format of an assembler:

;数据段
data segment
	;此处定义数据变量类型
data ends

;代码段
code segment
assume cs:code,ds:data 
start:
	mov ax,data
	mov ds,ax
	;------
	;此处写需要实现的功能
	;------
	mov ah,4ch ;4ch表示从用户程序返回操作系统,结束程序
	int 21h
code ends
	end start

Code to output "hello world":

;数据段
data segment
	;定义字节大小(8位)的字符串,变量名为string
	;db表示字节
	;0dh,0ah表示回车换行
	;$表示字符串终止符
	string db 'Hello World!',0dh,0ah,'$' 
data ends

;代码段
code segment
assume cs:code,ds:data 
start:
	;push ds
	;mov ax,0
	;push ax
	mov ax,data
	mov ds,ax
	;------
	lea dx,string
	mov ah,09h ;ah是ax的高8位,功能号09h表示输出dx指向的字符串string
	int 21h ;中断指令,调用用户程序,执行ah中设置的09h号功能
	;------
	mov ah,4ch ;功能号4ch表示从用户程序返回操作系统,结束程序
	int 21h
code ends
	end start

Because the Markdown of the CSDN blog does not support x86asmthe highlighting of the formatted code blocks, the code I posted has no color (obsessive-compulsive disorder is very uncomfortable!)

Notepad++ can be used locally, the language is set to A-Assembly, the syntax highlighting of assembly language is supported, and it will look more comfortable.
Insert picture description here
The syntax highlighting displayed by the notepad++ text editor is very beautiful:
Insert picture description here
By the way, when will CSDN support assembly language syntax highlighting? The Matlab language was not supported last time, so let's learn more from other people's blogs!

Three, generate and execute the assembler

Save the masm5 folder downloaded in the first step in D drive, for example, the path is D:\masm5, and then save the written code file as hello.asmand save in D:\masm5.
Insert picture description here
Open the first step installed DOSBox 0.74, and then enter the command:

mount c d:/masm5
c:
dir

As shown below:
Insert picture description here
Then enter the command:

masm hello.asm //再按三下回车
link hello.obj //再按三下回车
hello

The result is as follows:
Insert picture description here
Done! (It's not easy to write a hello world in assembly language...)

Guess you like

Origin blog.csdn.net/ljw_study_in_CSDN/article/details/109190182