Chapter 7 NASM Usage Tutorial (X86 Assembly Tutorial)

Publisher:心灵清澈Latest update time:2015-05-15 Source: 51heiKeywords:NASM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This chapter will explain how to use the NASM assembly compiler.
 
First, download NASM: http://www.51hei.com/f/nasm.rar 
        Don't rush to run it... Maybe someone has already run it and found that a black window flashes and then nothing happens. This is normal. Please don't think that NASM is broken.
Well, don't touch it for now, let's write a code first.
        First, you need to set your system to display file extensions. What is a file extension? It is an additional name for a file. A dot is added to the end of the file name, followed by the extension, which is usually composed of 1 to 4 English letters (3-letter extensions are the most common). It is used to indicate the type of file and is usually not displayed.
        If you have already set it, please skip this step. 32-bit XP setting display extension method:
 
        First, open any folder, find "Tools" in the menu, and find "Folder Options" in the pop-up menu.

 
 

Open this window (see the picture below↓)

1: Click "View",

2: Uncheck the option [Hide known file extensions].

3: Click the "Apply" button at the bottom of the window.

4: Click the "OK" button again. 
 


 

Then look at the files in your computer. The file names all show the extensions. Please familiarize yourself with the extensions first. Be careful when changing the file names in the future. Changing the wrong extension will cause the file type to be lost (of course, if you remember the extension, you can change it back)! [page]

        OK, open Notepad and create a new text file. Save it and you will get a file with the extension TXT. Change the extension to ASM!


 

        Have you finished changing it? What? You said the icon is different from mine? That's normal, please don't mind it....
 

Well, ASM files are assembly code files used to write assembly code. You can edit it with Notepad. Of course, you can also find a special assembly code editing tool (this is why the icon after I changed the extension is different from yours, the special assembly code editor has its own icon!), but I think Notepad is enough for beginners, and special editing tools will dazzle you!

        OK, open it with Notepad and write the following code:
 

ORG 0100H; This line of code has not been mentioned before, but will be mentioned later!
 

MOV AX,0B800H
 

MOV BX,AX
 

MOV CX,BX
 

MOV DX,CX
 

OK, save the code to a file.

Next, we compile it and create a text file again. We still need to change the extension. Change it to BAT (see the figure below↓):

 


 

Use Notepad to write the following syntax:

NASM "code file name. extension" -o "file name to be generated. extension"

Note the spaces!

Example:

NASM "New text document.ASM" -o "1.COM"
 


 

After writing, save and unzip NASM to the same folder: 


 

Double-click to run the BAT file. If you have done all the previous steps correctly, you will find an extra COM file. Don't rush to run it! This is what your code is compiled from! Did you know that Debug can also debug compiled programs? Let's debug it with Debug. Create a BAT file again and use Notepad to write the content according to the following syntax:

DeBug The file name and extension to be debugged

Note: There is a space in front of the file name, and the file name itself cannot have spaces!

Example:

DeBug 1.COM


 

        After running, the Debug window will appear. Enter the R command and look at the first line of code:


 

        You may have been shocked to find that the first line of code is not the first line of the code: ORG 0100H, but the second line MOV AX,0B800H

In fact, the reason is this:

        The ORG is not actually an assembly instruction. It is a flag that tells the compiler where this code may be loaded into memory in the future (please note the offset address of the MOV instruction in Debug, yes, it is 0100H!) 

        Note: This 0100H is not set by the ORG pseudo-instruction! (In fact, the CPU does not have it at all. The instructions specifically given to the compiler are called pseudo-instructions.) ORG just tells NASM that this code will be loaded into 0100H so that NASM can prepare it (you will know this later)
 

        OK, let's try running it: T command, it works, B800 is put into AX, and then AX is copied into BX.


Keywords:NASM Reference address:Chapter 7 NASM Usage Tutorial (X86 Assembly Tutorial)

Previous article:Chapter 7 NASM Usage Tutorial (X86 Assembly Tutorial)
Next article:Chapter 5 Hands-on Operation - Use of DEBUG 1 (X86 Assembly Tutorial)

Recommended ReadingLatest update time:2024-11-16 15:19

SiFive launches IP core comparable to x86 and Cortex-A78
This article is translated from theverge SiFive says its fastest RISC-V processor cores are closing the gap with mainstream computing architectures like x86 and Arm. James Prior, SiFive's senior director of product marketing and communications, told The Register that in terms of single-core performance, SiFive's c
[Embedded]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号