Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The assembler takes you to play with the string, get in the car!

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

A string or string (String) is a string of characters consisting of numbers, letters, and underscores. It is generally marked as s = "a1a2 an" (n > = 0). It is the data type that represents text in a programming language. In programming, a string is a continuous sequence of symbols or values, such as a symbol string (a string of characters) or a binary number string (a string of binary digits).

You must be familiar with the type of String. After all, every coder comes from var str1 = "Hello World".

But is it really just like that? Listen to me.

First, thinking

In the process of using strings in Swift development, have you thought about the following questions?

How much memory does 1 string variable take up?

What is the difference between the underlying storage of the string str1 and str2?

If you splice str1 and str2, what will happen to the underlying storage of str1 and str2?

If you can answer the above questions accurately, you know the underlying storage mechanism of Swift strings.

Second, how much memory is occupied by 1 string variable?

Method 1:MemoryLayout

First of all, you can test it with the MemoryLayout that comes with Swift

Method 2: assemble

In addition, we can also use a powerful low-level analysis assistant-assembly language to peek into the underlying storage of String.

In fact, assembly language can be used to analyze the bottom of other grammars and system libraries.

For example, the principle of polymorphism, the principle of generics, the bottom layer of Array, the bottom layer of enumeration, and so on.

In addition, not only the underlying analysis of Swift,C, C++ and OC, but also assembly language can be used.

After all, every line of valid code you write will eventually be converted into machine instructions (0 and 1).

The machine instruction corresponds to the assembly instruction one by one, and each machine instruction can be translated into the corresponding assembly instruction.

Being able to read assembly instructions is equivalent to being able to read machine instructions and know exactly what CPU is doing (which registers are operated and which blocks of memory are operated)

The code for this tutorial runs directly on the command line (CommandLineTools) project of Mac

Therefore, the assembly code shown is based on the AT&T format of X64, not the ARM assembly of real iOS devices.

In fact, there are great similarities between different kinds of assemblers, except that some instructions are called differently.

Like Microsoft's Visual Studio, Xcode also has a very convenient disassembly function that makes it easy to view the assembly instructions corresponding to each sentence of code. the steps to open the disassembly interface are as follows

Put a breakpoint on a line of code that needs to be debugged (the disassembly interface will be displayed in the breakpoint debugging state)

Menu: Debug > Debug Workflow > Always Show Disassembly

Assembly is translated into assembly, Disassembly is translated into disassembly

Run the program and see the disassembly interface

If you have a lot of experience in disassembly, you can figure it out according to the assembly on lines 16 and 17. String takes up 16 bytes.

Because it uses rax and rdx registers to store the contents of the string str, while rax and rdx are 8 bytes

The content of the assembly is too much, because of the relationship between time and length, the article will not explain each assembly instruction in detail, but more to explain the importance of the assembly.

Third, the underlying storage of strings

Snoop on memory

Earlier, I wrote a gadget that can snoop on the memory of Swift variables: https://github.com/CoderMJLee/Mems

Now use it to peek into what data is stored in the 16 bytes of the string.

Mems.memStr (ofVal:) displays memory data in groups of 8 bytes by default

Pass the parameter alignment: .one to display memory data in groups of 1 byte

The ASCII value of the character '0characters' is 0x30~0x39. If you take a closer look at the original 16 bytes of str1, what do you find?

It directly stores the ASCII values of all characters in 16 bytes of str1

The 0xa in the last byte 0xea is the number of characters, which is also a total of 10 characters.

Splicing

It can be found that when splicing "ABCDE" to the str1

It finally stores the 15-character ASCII value of "0123456789ABCDE" in 16 bytes of str1.

The 0xf in the last byte 0xef is the number of characters, which is a total of 15 characters.

You can see that the 16 bytes are already full. What if you splice one more character?

As you can see, the data stored in str1 has changed so much that the ASCII value of each character is missing.

What exactly does the 16 bytes in it mean?

Where are the ASCII values of all the characters ('0characters 9',' A' to'F')?

Other circumstances

What if at the beginning of initialization (before splicing), the content of the string is more than 15 characters?

I'm sure you can guess this result.

There is no ASCII value of any character in these 16 bytes.

And these 16 bytes are still different from the str1 on line 27.

Although their string contents are all "0123456789ABCDEF"

If you splice the str2

It is not difficult to find that the 16 bytes of str2 have changed again, which is somewhat similar to the str1 on line 27.

How to solve the above questions?

The above questions can not be solved just by looking at the printed memory data, but all of them can be used. Assemble! To solve the problem, analyze the assembly instructions, and immediately draw a conclusion. Because the length of the article is limited and the usual work is relatively busy, I recorded the detailed analysis of the above problems into a video for more than 2 hours. Interested friends can watch it at 1.5 to 2 times the speed.

Link: https://pan.baidu.com/s/1AkS3K1ZKP8zyxhlhLRaBkA

Extraction code: kzrk

The video may be a little difficult for friends who do not have the basis of compilation. It is best to choose a clear-headed time to watch it.

After watching the video, I hope you can definitely feel the importance of assembly language, and don't stay at the level of writing high-level language code and indulging in syntax candy forever.

Fourth, finally

People often think that the scope of application of assembly language is very small, while ignoring its importance. In fact, assembly language is very important for everyone who wants to learn computer science and technology.

The value of assembly goes far beyond the snooping at the bottom of the string described in this article, and it will definitely benefit your program career for a lifetime (as do data structures and algorithms). After learning to assemble, you will have a better understanding of what the machine does when you program in a high-level language, and you can improve the efficiency of the algorithm by modifying the code of the high-level language.

The development of IT technology is changing with each passing day, new technologies emerge in endlessly, have good learning ability, can acquire new knowledge in time, supplement and enrich themselves at any time, and have become the core competitiveness of programmers' career development.

As a programmer, we need to enrich our knowledge base constantly. What we know is like a white circle, outside which is a dark unknown world. The larger the circle, the more dark parts it touches. Only when we keep learning can we break more darkness and find more light.

If you want to improve yourself and learn more programming skills such as iOS, data structures and algorithms, here are free learning materials. You are welcome to add Wechat: 19950277730 for more tips on technical upgrading. There are not only like-minded friends, but also countless free programming skills, learning videos and materials, plus Wechat to discuss learning techniques!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report