当前位置:网站首页>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 ,
· 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 ,
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 ,
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 .
边栏推荐
- Gbase 8s overall architecture
- Trigger for gbase 8s
- Unity Quad culls shaders with back faces and transparent parts
- GBASE 8s 索引R树
- Wechat likes to pay attention to the solution of invalid automatic reply
- 第九章 APP项目测试(2) 测试工具
- Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)
- CTF_ Web:8-bit controllable character getshell
- Bingbing's learning notes: implementation of circular queue
猜你喜欢
Xiaobai learns MySQL - Statistical 'opportunism'
Separation of storage and computing in Dahua cloud native database
Successfully solved: selenium common. exceptions. TimeoutException: Message: timeout: Timed out receiving message from
LabVIEW development gas regulator
Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
论文笔记: 多标签学习 ESMC (没看懂, 还没写出来, 暂时放这里占个位置)
What is the storage engine and the three common database storage engines for MySQL
js的arguments
大话云原生数据库中的存算分离
在 .NET 6 中使用 dotnet format 格式化代码
随机推荐
《牛客刷verilog》Part I Verilog快速入门
Thorough understanding of database transactions
成功解决:selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from
冰冰学习笔记:循环队列的实现
LabVIEW开发气体调节器
How to screen out words related to products and eliminate invalid words accurately
php封装curl发送get、post请求方法,并使用
2021.8.29 notes: register, bit operation, pointer, structure
Retrofit 源码分析
Codeforces Round #802 (Div. 2) C D
GBASE 8s 索引R树
Part I Verilog quick start
CTF_ Web: file contains pseudo protocol with PHP
i. Max development board learning record
Le chemin de l'apprentissage immutable - - Adieu à la copie traditionnelle
Concat() in JS
Trigger for gbase 8s
高效的NoSQL数据库服务Amozon DynamoDB体验分享
halcon之区域:多种区域(Region)生成(3)
Paper notes: multi label learning ESMC (I don't understand it, but I haven't written it yet, so I'll put it here for a place temporarily)