当前位置:网站首页>Which programming language is the most cumbersome to implement Hello world?

Which programming language is the most cumbersome to implement Hello world?

2022-06-25 04:42:00 Dafang teacher talks about SCM

Which programming language implements hello world Most cumbersome ?

explain :

· Because assembly is a language that directly faces the bottom layer , So the simplest program also involves many low-level details, which makes it obscure ( Unlike C Direct one printf Get it done );

· This article passes the simplest hello world Program , Understand registers 、 Memory 、 section 、 Instructions 、 system call , In the simple operation principle of the program ;

Talk is cheap, show me your code!

; Source file name :test.asm

; Execution file name :test

; Compiling method :

;nasm -f elf64 -g -F dwarf test.asm -l test.lst

;gcc -o test test.o -no-pie

section .data; stay .data Section writes data

msg: db "hello world",10;10 Corresponding ascii Is a newline

msgLen equ $-msg;equ It's a pseudo instruction , This line of code means msgLen Means msg The placeholder length of the string ( byte )

section .text; stay .text Write code

global main; The code entry label of the program is main( Enable the program to find the first instruction when executing )

main:; This label essentially refers to .text The memory address of the first instruction of section

; Screen printing

mov rax, 1;sys_write(x86-64)

mov rdi, 1;1 It's standard output

mov rsi, msg

mov rdx, msgLen; String length , barring 0

syscall;64 Bit int 0x80 Instructions

; Exit procedure

mov rax, 60;exit(x86-64)

mov rdi, 0; Parameters 0, Combined with the previous instruction is exit(0)

section sentence

To understand section The function of sentences , First of all, we should have a preliminary understanding of the execution principle of the program , as follows ,

4972d19e0552f9b00c0f2a9a55a304a8.png

· The compiler compiles the assembly code , According to Linux Of ELF(linux The executable file format of ) Compile ( For example, insert ELF header、program header、section header、got etc. );

· While the program is running , Load the code and some initialized data into the memory according to the format ( Distributed to all section);

·stack section , In the process of program running, data will be pushed or popped according to program logic (stack Section generally does not store code );

·heap section , An area for the most free data storage of a program ( For example, when C Of malloc This area is used when the function applies for memory );

·bss section , Used to store defined but uninitialized data ( for example C Of int i、char ch[10]), Commonly used in program data receiving buffer ;

·data section , Used to store data that has been defined and initialized ( Be similar to C The constant );

·text section , For storing code ( The code of the function is also stored in this area , It's not stored in stack Area );

in summary ,section Statement is used to distinguish regions of assembly code .

syscall And interrupt vector table

Introducing syscall Before the order , Need to introduce first linux Several key concepts of , as follows ,

· User space ( The essence of user space is the specified memory space . These spaces are used to run user programs , for example nginx、apache);

· Kernel space ( The essence of kernel space is also memory space , Kernel space is used to run the code of the operating system , In principle, user space applications cannot access kernel space , Or you can't directly access the kernel space , So the following concepts are introduced —— Interrupt vector table );

· Interrupt vector table ( The essence of interrupt vector table is an array , The elements of the array are memory addresses , These addresses point to kernel space )

syscall The relationship between the instruction and the interrupt vector table is as follows ,

2aa1d7bcec15f0d55cd2ecba67d3dd33.png A user process wants to call a system service ( For example, output to screen 、 Open file ), It is necessary to uniformly pass the 128 Interrupt vectors , Send the system call service request according to the established data structure

· Applications cannot access kernel space at will , Need to access through established rules , So the interrupt vector table is designed ( This design has safety significance );

· Because the interrupt vector table has “ indicator ” The direction meaning of , So it's called “ vector ” surface ;

in summary , At terminal ( It's usually the screen ) Print hello world, In essence, it is a process of calling system services .

linux system call (sys_write)

As mentioned above , Want to call system services , You need to follow the established data structure of this service , Then organize the data , Xiang di 128 The interrupt vector of No. sends a service call request , Here's the picture ,

1924508e86a561269a1bbc50cc73e162.png Organize what needs to be printed , adopt syscall Instruction to 128 No. interrupt sending “ standard output ” Service invocation request for

· register rax The content of should be set to 1. During a system call , This register is usually used to store the call number of the system service . The service number printed is “1”( To view the system call service number, view the file /usr/include/x86_64-linux-gnu/asm/unistd_64.h)

· register rdi(destination index), File descriptor used to specify the printout ( The file descriptor of the screen is 1);

· register rsi(source index), Used to specify the address of the output content ( The storage address of the string );

· register rdx, Used to specify the length of the output content ( This can be customized , You decide how long you want to print ; The last article mentioned , Strings from an assembly point of view , Just continuous memory storage space )

in summary , Describe printing in a colloquial way hello world The process of ( System service call procedure ), as follows ,

·rax Registers tell the operating system , What system services need to be called (1 No. service is “ standard output ” service );

·rdi Registers tell the operating system , To print there ( Generally, it is printed on the terminal , That's the screen );

·rsi Registers tell the operating system , Where can I find what needs to be printed ;

·rdx Registers tell the operating system , How long does it take to print ( byte );

Last , After the print service is called , Need to end the program , amount to C Of exit(0) function , The principle is not repeated , See the code provided above for details .

原网站

版权声明
本文为[Dafang teacher talks about SCM]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250323578149.html