The netwide Explorer (NASM) Assembly Language Notes (1)

Source: Internet
Author: User
Tags derick

This is a good introduction to tutorial in assembly language learning, so I have extracted some important points.

1. An assembly language program can be divided into three parts: 1). Data Zone

Used to define initialization variables (however, these variables cannot be changed during program execution, so they are "variables" in a narrow sense ")

From the perspective of C language, these macros, import declaration file libraries, and so on should all be in this area. You can useEqu, DB, DW, DD, DQ
AndDT
Other commands

Section. datamessage: DB 'Hello world! '; Defines the "variable" message with the value 'Hello world! '(Excluding single quotes) msglength: equ 12; defines the variable "msglength" value as 12 buffersize: DW 1024; defines the variable "buffersize", the data type is word, the value is 1024
2). BSS

Zone

 
This area defines all variables,
AvailableResb, resw, resd, resq

And
Rest
And other command operations in the memory to apply for the initial space.

Filename: resb 255; apply for 255 bytesnumber: resb 1; apply for 1 bytebignum: resw 1; apply for 1 word (1 word = 2 bytes) realarray: resq 10; apply for an array containing 10 (reals) Real Numbers
3)
. Text

Zone

All operations in the assembly language are defined in this region. . Text must startGlobal _ start

To tell the kernel where the program entry is (similar to the main function in C or Java, the difference is that it is not a function, but just a starting point)

Section. textglobal _ start_start: Pop EBX; this is the starting point of the program...
2. Call the Linux Kernel

The Linux kernel is called in the same way as DOS.

1. Write the kernel call code into eax (here, a 32-bit system is used as an example)

2. Write the parameters required for Kernel calling into EBX, ECx, etc.

3. Call the system kernel termination command (DOS is 21 h, while Linux is 80 h)

4. kernel call results will be returned in eax


Generally, there are 6 registers used to call the system kernel. The first parameter is written in EBX, the second is written into ECx, followed by EDX, ESI, and EDI, followed by EBP.

MoV eax, 1; call the exit function of the system kernel mov EBX, 0; the exit parameter is 0int 80 h; the end of 80 h is like saying to the system: "Yo, do this "of course, the system kernel has a lot of functions available for calling. We can find unistd in the/usr/include/ASM/directory. h file (or unistd32.h or unistd64.h)
It contains all kernel calls supported by your system. You can also view the Linux System Call Table edited by the author.
3. The first assembler helloworld

Section. Data <br/> Hello: DB 'Hello world! ', 10;' Hello world! 'Plus A linefeed character <br/> hellolen: equ $-Hello; length of the 'hello world! 'String <br/>; (I'll explain soon) <br/> section. text <br/> global _ start <br/> _ start: <br/> mov eax, 4; The system call for write (sys_write) <br/> mov EBX, 1; file descriptor 1-standard output <br/> mov ECx, hello; put the offset of hello in ECx <br/> mov edX, hellolen; hellolen is a constant, so we don't need to say <br/>; MoV edX, [hellolen] To get it's actual value <br/> int 80 h; call the kernel <br/> mov eax, 1; the system call for exit (sys_exit) <br/> mov EBX, 0; exit with return code of 0 (no error) <br/> int 80 h

 

Save the preceding code in a new file and name the file as hello. ASM.

Note: For hellolen: equ $-Hello
Row analysis:

In NASM, dollar sign $ indicates the first line of the command line (that is, the last line address of the previous line ), the Forward end address minus the address of the hello sign gets the length of the string represented by Hello.

 

Compiling and linking Libraries

A. Compile the program

$ NASM-F elf hello. ASM

A Hello. o file is generated.


B. Link Library

$ LD-S-O hello. o

Generate executable code for the final computer


C. Run the program

$./Hello


Congratulations! Your first assembly of virgins has been successfully run! Of course, this is only a preliminary step. If you really want to understand assembly, Let's do more.

 

Note
: If you do not have NASM compilation software, you can install sudo apt-Get install NASM.


 

 

 

 

 

Http://www.cin.ufpe.br /~ If817/arquivos/asmtut/index.html # intro

Thanks to Derick Swanepoel (derick@maple.up.ac.za)
)

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.