Select compiler
Nasm. Fasm Yasm. or MASM, gas or something.
The first three are free, open-source compilation compilers that use Intel's syntax in general. Yasm was developed on the basis of NASM, with NASM ancestry. Because the same syntax is used, the NASM code can be compiled directly with Yasm.
Yasm Although the update is slow, but the nasm some unreasonable places to improve. From this point of view, Yasm is better than NASM, and the NASM update is fast enough to support the updated instruction set. On the Windows platform, FASM is another good choice, platform support is good, can be used directly to develop programs on Windows, syntax is also more unique. In support of the Windows program architecture, FASM is the best in 3 free compilers.
MASM, the assembler compiler released by Microsoft, has now ceased to be released separately and is fused into Visual Studio products. Gas is a free, open-source compilation compiler on the Linux platform, using the assembler syntax of AT-T, which is cumbersome to use.
Since the example of this book is run directly on the naked machine, the author uses NASM, because its syntax is relatively concise, the use method is simple, the update speed is very fast. However, if you use NASM to write Windows programs is more painful, this aspect of the document is very few.
You can download the latest version from NASM's official website: Http://www.nasm.us/pub/nasm/releasebuilds/?C=M, or browse and download its documentation: http://www.nasm.us/docs.php. Machine Language
A machine instruction is identified by the corresponding binary number and can be identified directly by the machine. Before assembly language appeared, the use of machine instructions to write a program is to directly input binary number into the computer.
How c=a+b in C language should be expressed in machine language.
This is a cumbersome process, where a, B, and C are variables that should be expressed in machine language. C language can not be directly converted to machine language, first by the C compiler to translate the equivalent of assembly, and then generated by the assembler machine instructions, and ultimately by the linker will be the address of these variables to set down.
Let's look at how to convert machine instructions. First, it is expressed in the corresponding assembly language.
mov eax, [a]; The value of variable A is placed in the EAX register
add eax, [b]; Execute a+b
mov [c], eax; put it in C.
In a x86 machine, if two memory operands are to be added, they cannot add directly, and one of them must be a register and at least one operand should be placed in the register. This expression is already the simplest form, in fact, of course, not only so simple, but also to match the context of the program structure. If one of the variables is temporary, the C compiler may choose not to put in memory. So are these variables local or external variables? The compiler first determines the address of the variable.
mov eax, [ebp-4]; variable A is a local variable
add eax, [ebp-8]; execute a+b, variable B is also a local variable
mov [0x0000001c], eax; in C, the variable C may be an external variable
Variables A and B are on the stack. On most platforms, variable C is placed into the. Data section, but before the link is made, the address of C may be an offset, not a real address, and the linker will be responsible for replacing the offset value with the real address of the variable C.
The assembly language above is translated into machine languages as
8b FC; corresponds to mov eax, [ebp-4]
F8; Corresponds to add eax, [ebp-8]
A3 1c 00 00 00; Corresponds to MOV [0x0000001c], eax
x86 machine is a CISC (complex instruction set calculation) system, the length of the instruction is not fixed, such as the preceding two instructions are 3 bytes, the last instruction is 5 bytes.
The x86 machine instruction has a minimum length of 1 bytes and a maximum of 15 bytes.
Finally, assuming that the base address of the. Data section is 0x00408000, then the address of the variable c is 0x00408000+0x1c = 0x0040801c, and after linking, the last machine instruction becomes
A3 1c 80 40 00; Original assembly expression form: mov [c], eax
Directives also use Little-endian storage sequences, from low to high to store A3 1c 80 40 00 bytes, where 1c 80 40 00 is the Little-endian byte order of the address value 0x0040801c. Hello World
As a rule, let's take a look at the compilation of the "Hello, World" program.
Experiment 2-1:hello World Program
The following code is equivalent to the code in the C language main ().
Code Listing 2-1 (TOPIC02\EX2-1\SETUP.ASM):
Main: This is the entry point for the module code.
mov si, caller_message
Call puts; Print information
mov si, current_eip
mov di, caller_address
CURRENT_EIP:
Call get_hex_string; Convert to Hex
mov si, caller_address
Call puts
mov si, 13; Print Enter
Call PUTC
mov si, 10; Print line break
Call PUTC
Call Say_hello; Print information
JMP $
Caller_message DB ' now:i AM the caller, address is 0x '
Caller_address DQ 0
Hello_message db, ten, ' hello,world! ', 13,10
DB ' This was my first assembly program ... ', 13, 10, 13, 10, 0
Callee_message db "Now:i ' M Callee-say_hello (), address is 0x"
Callee_address DQ 0
In fact, this assembly language is equivalent to a few C language statements below.
int main ()
{
printf ("Now:i am the caller, address is 0x%x",
Get_hex_string (CURRENT_EIP));
printf ("\ n");
Say_hell0 (); /* Call Say_hello () */
}
In contrast, assembly language code is much larger. The following is the assembly code for Say_hello ().
Code Listing 2-2 (TOPIC02\EX2-1\SETUP.ASM):
;-------------------------------------------
; Say_hello ()
;-------------------------------------------
Say_hello:
mov si, hello_message
Call puts
mov si, callee_message
Call puts
mov si, Say_hello
mov di, callee_address
Call get_hex_string
mov si, callee_address
Call puts
Ret
This Say_hello () is only equivalent to the following C statements.
void Say_hello ()
{
printf ("Hello,world\nthis is my first assembly program ...");
printf ("Now:i ' m Callee-say_hello (), address is 0x%x",
Get_hex_string (Say_hello));
}
Code listings 2-1 and 2-2 make up the Hello World program, which is the assembly language version of our 16-bit real mode, and its running results on VMware are as follows.
Of course, only these two pieces of assembler code is far from achieving the above results, this example is supported by Boot.asm and Lib16.asm, Boot.asm used to start the MBR module of the machine, Lib16.asm is a 16-bit real-mode library (in the lib\ directory), Provides functionality similar to the C library.
The code for main () is loaded into memory 0x8000, and Lib16.asm's code is loaded into 0x8a00 as a shared library. All the code in this example is in the topic02\ex2-1\ directory, including boot.asm source files and setup.asm source files, while Lib16.asm is in the X86\source\lib\ directory. The module where main () is located is setup.asm.
16 bits. 32 bits. or 64-bit.
The processor works in 16-bit real mode when the machine starts. This Hello World program works in 16-bit real mode, and when you write code, you need to give NASM a code that indicates 16-bit compilation, using BITS 16 to indicate word declarations at the beginning of the code.
Bits 32 indicates that the compilation is 32-bit code, and Bits 64 indicates that the compilation is 64-bit code.
This article extracts from "X86x64 system exploration and programming"
Publishing Industry Publishing House
Deng Zhi