The Netwide Assembler (NASM) Assembly language notes

Source: Internet
Author: User
Tags derick

In assembly language learning to find a good entry tutorial, so excerpt some of the more important points 1. An assembler language program can be divided into 3 parts: 1. Data area

Used to define initialization variables (but the variables defined here are not allowed to change during program execution, so they are "variables" in a narrow sense)

From the C language point of view, these macros, import Declaration file library, etc. should be within this area. Other instructions, such as equ,db, DW, DD, DQ , and DT , can be used here.

Section. Data message:db ' Hello world! '; Defines a "variable" message with the value ' Hello world! ' (Does not contain single quotes) Msglength:equ 12; Define the variable "msglength" value of BUFFERSIZE:DW 1024; Defines the variable "buffersize", the data type is a word, and the value is 1024
2). BSS area

This area defines all of the variables , and you can use resb, RESW, RESD, Resq , and REST instruction operations to request space in memory for initialization.

FILENAME:RESB 255; Application for 255 bytes Number:resb 1; Application for 1 byte BIGNUM:RESW 1; Request 1 Word (1 Word = 2 bytes) Realarray:resq 10; Request a 10 (reals) array of real numbers
3). Text Area

All operations of assembly language are defined in this area. The global _start must be defined at the beginning of the. Text to tell the kernel where the entry of the program is (similar to the main function in C or Java, except that it is not a function, but a starting point)

Section. Text global _start _start:pop ebx; This is the starting point of the program ...
2. Call Linux kernel

Linux system kernel calls the same way as DOS.

1. Write the kernel call code into EAX (for example, in 32-bit systems)

2. Write the parameters required by the kernel call into EBX, ECX, etc.

3. Call system kernel termination instruction (DOS is 21h, Linux is 80h)

4. The results of the kernel call are returned to the EAX


There are generally 6 registers used to invoke the system kernel. The first parameter is written in EBX, the second is written in ecx, then Edx,esi, EDI is EBP.

mov	eax,1; call system kernel Exit Function mov ebx,0 exit parameter is 0 int	80h; terminate 80h, like saying to the system: "Yo, do this" of course the system kernel is available The function of the call is so much that we can find the Unistd.h file (or possibly Unistd32.h or unistd64.h) in the/usr/include/asm/directory that
contains all the kernel calls that your system supports. Or view the Linux System Call table table that the author edits.
3. First Assembler HelloWorld

View plain section .data       hello:     db  ' hello world! ',10    ;  ' hello world! '  plus a linefeed character       hellolen:  equ  $-hello             ; Length  of the  ' hello world! '  string                                            ;  (I ' Ll explain soon)    Section .text        global _start   _start:        Mov eax,4            ; the system  call for write  (sys_write)        mov ebx,1             ; file descriptor 1 - standard output        mov ecx,hello        ;  put the offset of hello in ecx       mov edx, Hellolen     ; hellolen is a constant, so we don ' t  need to say                             ;  mov edx, [Hellolen] to get it ' s actual value       int 80h               ; Call the  Kernel &nbsP     mov eax,1             ; the system call for exit  (sys_exit)        mov  ebx,0            ; Exit with  return code of 0  (no error)        int 80h   

Save the above code in a new file and name the file as Hello.asm.

Note: The analysis of the hellolen:equ $-hello line:

The dollar sign in NASM represents the line at the beginning of the instruction line (that is, the end of the previous line), and the address at the forward end minus the hello flag gets the length of the string that Hello represents

compiling and linking libraries

A. Compiling programs

$ nasm-f Elf Hello.asm

A hello.o file is generated


B. Link library

$ ld-s-O Hello hello.o

Generate final Computer executable code


C. Running the program

$./hello


Congratulations, your first maiden program was successfully run. Of course, this is only preliminary, to really understand the assembly, then more practice it.

Note: If you do not have NASM to compile the software, you can sudo apt-get install NASM installation

3 Processing of parameters when running the program

As opposed to DOS, the invocation of parameters entered when running the program is simpler and more intuitive. Because all the parameters are stored on a stack after the program starts running, all you have to do is use the pop command to take out the required parameters. Here's an example (a program with 3 parameters):

./program Foo Bar 42

The corresponding stack can be explained by the following diagram

4
Program
Foo
Bar
42
Number of arguments, note that program names are also treated as a parameter (ARGC)
Program name (Argv[0])
First parameter (argv[1])
Parameter 2 (argv[2])
Parameter 3 (argv[3]) (Note: This is a string "42" instead of number 42)

Original address: Http://www.cin.ufpe.br/~if817/arquivos/asmtut/index.html#intro

Thank the author Derick Swanepoel (derick@maple.up.ac.za)


from:http://blog.csdn.net/jiangsq12345/

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.