In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the disassembly of global variable code". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. disassembly of global variable codes
1. Source file
"gcd.s"
.text .global _ start _ start: ldr sp,=0x70000000 / * get stack top pointer*/ b main
B main
/ * * main.c * * Created on: 2020-12-12 * Author: pengdan * / int xx=0; int yy=0; int zz=0; int main (void) {xx=0x11; yy=0x22; zz=0x33; while (1); return 0;}
"map.lds"
OUTPUT_FORMAT ("elf32-littlearm", "elf32-littlearm", "elf32-littlearm") / * OUTPUT_FORMAT ("elf32-arm", "elf32-arm", "elf32-arm") * / OUTPUT_ARCH (arm) ENTRY (_ start) SECTIONS {. = 0x40008000;. = ALIGN (4); .text: {gcd.o (.text) * (.text)}. = ALIGN (4); .rodata: {* (.rodata)}. = ALIGN (4); .data: {* (.data)}. = ALIGN (4); .bss: {* (.bss)}}
"Makefile"
TARGET=gcd TARGETC=main all: arm-none-linux-gnueabi-gcc-O1-g-c-o $(TARGETC). O $(TARGETC). C arm-none-linux-gnueabi-gcc-O1-g-c-o $(TARGET). O $(TARGET). S arm-none-linux-gnueabi-gcc-O1-g-S-o $(TARGETC). S $(TARGETC). C arm-none-linux-gnueabi-ld $(TARGETC). O $(TARGET). O-Tmap.lds-o $(TARGET). Elf arm-none-linux-gnueabi-objcopy-O binary-S $(TARGET). Elf $(TARGET). Bin arm-none-linux-gnueabi-objdump-D $(TARGET). Elf > $(TARGET). Dis clean: rm- rf *. O *. Elf *. Dis *. Bin
[cross-compilation tool, search for installation]
two。 Disassembly result:
As can be seen from the picture above, it takes "8 bytes" to store an int global variable.
"literal pool (text pool) occupies 4 bytes"
The essence of literal pool is a block of memory in the code section of ARM assembly language that holds constant data rather than executable code.
The reason for using literal pool (text pool) when you want to use a 4-byte constant data in an instruction (this data can be a memory address or a numeric constant), because the ARM instruction set is fixed-length (ARM instruction 4 bytes or Thumb instruction 2 bytes), it is impossible to encode this 4-byte constant data in a compiled instruction. At this point, the ARM compiler (compiling C source program) / assembler (compiling assembler) allocates a piece of memory in the code section and stores the 4-byte data constant here, and then uses an instruction to load the 4-byte numeric constant into the register to participate in the operation. In the C source code, the allocation of the text pool is arranged by the compiler at compile time. When designing the assembler, the developer can allocate the text pool by himself. If the developer does not arrange the text pool, then the assembler will do it.
"bss segment occupies 4 bytes"
For every visit to a global variable, a total of 3 instructions are required, and "12 instructions" are used to access the global variable for 3 times.
14. By offsetting the current pc value 40008018 by 32 bytes, find the link address 40008038 of the xx variable, and then take out its content 40008044 and store it in R3, which is the address of xx in the bss segment 15. 0. By assigning the immediate number 0x11, or # 17, to r 2 16. Write the contents of R2 to the memory pointed to by R3, that is, the memory corresponding to the xx label.
II. Disassembly of structural code
1. Modify the main.c as follows:
/ * 2 * main.c 3 * 4 * Created on: 2020-12-125 * Author: Linux 6 * / 7 struct 8 {9 int xx; 10 int yy; 11 int zz; 12} peng; 13 int main (void) 14 {15 peng.xx=0x11; 16 peng.yy=0x22; 17 peng.zz=0x33 18 19 while (1); 20 return 0; 21}
two。 The disassembly code is as follows:
The picture can be seen from the picture above:
The structure variable peng is located in the bss section and the address is 4000802c
To access the structure members, you also need to use pc to find the address 40008028 in the text pool corresponding to the structure variable peng, and then find the structure variable peng address 4000802c indirectly.
Compared to being defined as three global variables, the advantages are:
All members of the structure share the same address in literal pool; each global variable has an address in literal pool, "saving 8 bytes."
When accessing other members of the structure, you do not need to load the base address again, but only need 2 instructions to realize the assignment; to access 3 members, you need a total of "7 instructions" and "save 5 instructions"
So for functions that require a large number of access to structure members, all access structure members only need to load the base address once.
Using the structure can greatly save the instruction cycle, and saving the instruction cycle is self-evident to improve the running efficiency of cpu.
"so, say the important question three times."
"use structures as much as possible."
Third, continue to optimize
So can there be fewer instructions? The answer is yes. Modify the Makefile as follows:
TARGET=gcd TARGETC=main all: arm-none-linux-gnueabi-gcc-Os-lto-g-c-o $(TARGETC). O $(TARGETC). C arm-none-linux-gnueabi-gcc-Os-lto-g-c-o $(TARGET). O $(TARGET) .s arm-none-linux -gnueabi-gcc-Os-lto-g-S-o $(TARGETC). S $(TARGETC). C arm-none-linux-gnueabi-ld $(TARGETC). O $(TARGET). O-Tmap.lds-o $(TARGET). Elf arm-none-linux-gnueabi-objcopy-O binary-S $(TARGET). Elf $(TARGET). Bin arm-none-linux-gnueabi-objdump-D $(TARGET). Elf > $(TARGET). Dis clean : rm-rf * .o * .elf * .dis * .bin
Still use the main.c file in Chapter 2
Execution result
You can see that the code has been optimized to 5.
14. Load the address 40008024 of peng into R3. R0 write immediate number 0x11 16. R1 write immediate number 0x22 17. R0 write immediate number 0x33 18. Through the stm instruction to write the values of R0, R1, R2 in order to 40008024 memory, "what is the disassembly of global variable code" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.