"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
In a high-level language, a program often takes one or more parameters when it is started on the command line, and this feature can be implemented in assembly language. Before implementing this feature, let's look at how Linux executes programs from the command line. when each application starts executing, the system allocates a chunk of memory for the program, and each program allocates the same virtual memory address. The virtual memory address is mapped by the operating system to the physical memory address. In Linux, the program's virtual memory address starts at 0x80480000 and ends at address 0xbfffffff. The Linux operating system places the program in the virtual memory address in a specialized format. Such as:
Memory Area The first area contains the BSS segment and data segment of the assembler. The second area is the program stack, previously mentioned, and the stack grows from the last downward of the memory area. Each time the program starts, the stack pointer is not set to 0XBFFFFFFF, and Linux puts some content here, such as command-line arguments, before loading the program. When the program starts, Linux stores 4 information in the program stack: the number of command-line arguments, the name of the program, the command-line arguments that the command line contains, and all the current Linux environment variables when the program starts. When the program starts, the general layout of the stack is as follows:
now that you know where the command-line arguments are located in the stack, write a simple program to access them. The following example runs in debug to view all command-line parameter values, and all command-line arguments are stored as string values in the stack.
# arg.s.section Text.globl _start_start: nop movl $, %eax MOVL $ ,%ebx int $0x80
run the program in the debugger as follows:
(GDB) r 101 102 #运行时带入三个参数Starting Program:/HOME/ALLEN/AS/I_ARG/ARG-101 102Breakpoint 1, _start () at Arg.s:55 NOP (GDB) S6 Movl,%eax (gdb) print $esp #打印堆栈栈顶地址 $ = (void *) 0xbffff3d0 (gdb) x/20x 0xbffff3d0 #查看堆栈向 The data for the last 20 addresses, where the first data is the number of command-line arguments followed by 0xbffff566 0xbffff57f 0xbffff583 0xbffff587 The address of the parameter for the command line parameter (GDB) r 101 1020xbffff3d0: 0x00000004 0xbffff566 0xbffff57f 0xbffff5830xbffff3e0:0xbffff587 0x00000000 0xbffff58b 0xbffff5ac0xbffff3f0: 0XBFFFF5CB 0xbffff5ec 0xbffff5fc 0xbffff6070xbffff400:0xbffff615 0xbffff666 0xbffff6a0 0xbffff6b20xbffff410: 0xbffff6c8 0xbffff6e6 0xbffff6fd 0xbffff708 (gdb) x/d ($ESP) 0xbffff3d0:4 (gdb) x/s 0xbffff566 # parameter for program name 0xbffff566: "/hom E/allen/as/i_arg/arg "(gdb) x/s 0xbffff57f #查看第一个参数内存地址内的内容0xbffff57f:" (gdb) x/s 0xbffff583 #查看第二个参数内存地址内的内 Capacity 0xbffff583: "101" (GDB) x/s 0xbffff587 #查看第三个参数内存地址内的内容0xbffff587: "102" (GDB)
Note that the parameter will never be 0 because the program name also counts as a parameter. After the command-line arguments, a null value of 4 bytes is stored on the stack, which separates the parameter pointer from the pointer to the environment variable.
Linux Platform x86 compilation (15): Using command-line arguments