Udemy

String Representation in Assembly Language

A free video tutorial from xor pd
Low level technology training
Rating: 4.3 out of 5Instructor rating
2 courses
31,634 students
String Representation

Lecture description

In this video we explain how to represent text strings inside our own assembly programs. We also introduce two schools of strings: Length prefix and Null terminated strings.

Learn more from the full course

Assembly Language Adventures: Complete Course

Learn the language of your computer

29:09:32 of on-demand video • Updated November 2019

Learn to code on the x86 Architecture using Assembly Language
Gain solid understanding about low level concepts.
Understand how your computer works
Become a tough person
English [Auto]
Hi and welcome to another lesson about strings. Today's topic is string representation. Objectives of this lesson we will learn how to represent text strings inside our own assembly code. Representing text. How to express Hello world using Ascii. As you've probably guessed, we just have to go to the table and replace every symbol with the correct number. We are going to get something like this. By the way, all the numbers that you see here are in base 16 or hexadecimal. We call this kind of representation a string of bytes or just a string. But how can we know the size of the string? This is mostly an important question when you use strings in your program because you will probably want to know how much space to allocate for a string. Or maybe if you want to copy the string from one place to another place, you want to know how much space to allocate in the other place in the destination. So at any time you should know how long is the string? And there is some kind of a debate of how to know the size of a string. There are basically two schools of indicating the size of a string when stored in static memory. In fact, there are a bit more, but basically there are mostly two. The first one is called the length prefix or the Pascal style. In this method, we write the size of the string on the first byte. So hello is going to be represented as follows. The first byte is going to be five, which represents the amount of bytes in this string because we have five letters one, two, three, four, five. So the first byte is going to be the length and the next five bytes are going to be the actual string. So by reading the first byte, we can know the size of the string. This is the Pascal style or the length prefix. The other style is the C style or null terminated string. In this style, the string ends or terminates with the null Terminator character, which is zero. You probably remember it from the table. It is the first character. Hello is represented now as those five numbers which represent h e l l o. And then the null character or the zero Terminator. It has many names. These are basically the two ways to represent the string. There are some more presentation systems which are more complex. For example, linked list of ropes, and there are even more. Maybe in the future you will get to see some of those other presentations. But today we are only going to speak about those two. Some pros and cons of each method. In the length prefix, some pros are that the length can be calculated quickly. We just have to find out what is inside the first byte because the first byte has the total length. Important coin of the length prefix is that the size of the string can be limited. For example, if the length prefix is of size one byte, then the maximum size of a string is going to be 255 characters, which is not that long string. We could, for example, extend this limitation if we chose. The length prefix is going to be four bytes, for example, then we could have strings of a very long size. We could even choose, for example, a prefix of size 64 bits or eight bytes, which is really a lot. We will probably never have a string of such size. Let's take a look at null termination. An important pro for this method is that the size of the string is virtually unlimited. As long as we didn't see any null Terminator, we just keep reading the string so it can be of any size. But there are also important cons. It takes longer time to calculate length. For example, if I want to find out the length of a null terminated string, I have to go over all the characters of the string until I find the Zero Terminator, which means that I have to read the full string just to find out the size. Another thing is security issues. Strings that are represented using the null termination method can have no ending. Basically, if you forget to put the zero Terminator or maybe some error occurs and the Zero Terminator is gone, the string just keeps going until the next zero Terminator is found. And this could cause some kind of security issues. We are not going to get into this now, but you should have this in the back of your mind. In this course, we will work mostly with null terminated strings. And this is because of the following two reasons. Windows API functions expect null terminated strings, which means that it will be easier if we already work with null terminated strings. Another thing is that there are special assembly instructions to deal with null terminated objects or other kind of terminated objects. I want to show you those instructions and we will have to work with null terminated strings to work with those instructions. But don't feel obligated to just walk with this kind of string if you want to do something in a different way. This is assembly language. You can do whatever you want. It is just me that is going to work with null terminated strings. Declaring strings. The following declarations are equivalent. Let's take a look at some declarations for strings. Well inside the data section here, and we declare those five strings. The first one is a very simple one. We just use the DB or data byte operator to define the string. Hello world. You might be surprised that I can do something like this, because so far we have used the DB operator with numbers, with bytes. The assembler understands that this is a string and he will know to convert it to separate bytes. It will convert each character. For example, the H character will be converted to this number 48 and the E character will be converted to 65 and so on. There are some other ways in which we can declare the same string. Let's look at the other examples. String number two is declared mostly the same, like the first one, but we use different kind of quotes. Here we use double quotes and here we use single quotes. It doesn't matter. You can choose whatever you like. The cell. Example string number three. Here we just use numbers. You can do this if you want. Usually it is not very comfortable. In my opinion. This is much more readable, but maybe in some occasion you might want to use numbers. So you can use numbers. Take a look at the end of all those strings that we have seen so far. All of them contain the Zero Terminator. In the end, it is your responsibility as an assembly programmer to put the zero Terminator in the end. If you don't, the string will just continue. For example, if I didn't put this zero Terminator here, then string one will actually be Hello World, Hello World. There will be no separation between those two strings. Another example which emphasizes what can be done, the flexibility. I can write, for example, only part of the string and then continue on the next line. We are not going to have a new line character because I did it. This thing is just exactly this string or this string. All those strings represent exactly the same thing. If I want, I can just wrap it in the middle. I can break it to parts if I wanted to. I just have to make sure that I put the Zero Terminator in the end. Note that here I didn't put any Zero Terminator because this is the middle of the string. Another example, which is a bit strange. I write hello and then I add the number 20, which means space. And then I write world and I finish with the Zero Terminator. In this case, 20 just means space. New line character. Example. Let's assume that you have this text which is a bit more complex because it is made of more than one line walking in the realms of brightness, passing the wall of above, down into the soul. This is kind of very exciting song. We can see that we have two new line sequences. We have one here which breaks the line into the new line and we have another one here. Usually we don't refer to the end of the text as a new line. We will probably just put a zero Terminator here in the end of the text. Different representations in different operation systems or in different systems. A new line is represented in different ways. In Windows, a new line is marked by this sequence D and then A or maybe if you want 13 and then ten in base ten. In Linux. A new line is marked by a. If you use a different operation system, which is not Windows or Linux, check how the new line is represented in your system. Some historic note about why would it be two symbols to represent just one idea? One line. Historically those symbols D and A may be not symbols may be codes were used to represent an idea of how to print something. I remind you that the Ascii code was initially used for communication. It had directions for the remote printer about how to print the data. So the. Means carriage return or CR. It means return to the beginning of the current line. It is a command for a printer and Lf or line feed which is a means to advance the paper one line forward. So basically, if this is a pointer of a printer and we are at the end of the line on a page. Imagine, for example, that this is a page and the printer wants to print it. It follows the directions that it has. So assume, for example, that it sees this sequence D and then A it is going to first take the carriage, which is at the end of the line to the beginning of the line. And then it is going to advance the paper one line forward because of the line feed. Basically we get from here, which is the end of the previous line to the beginning of the new line. So historically, this sequence, men go to the beginning of the line and then go to the next line, which is just a new line. This is just how it was when those were physical directions. Now we have software and we don't really have to move any carriage to the beginning of the line. This is just an idea, an historic idea that was somehow preserved this day in Windows. You still use those two codes to represent a new line. Strange, but this is how it is. Declaring strings with new line. As you've probably guessed. We just have to add those two numbers D and A. For example, if we want to represent the previous song, we just have to add new line chords in the correct places. So we have the first line and then 13 and ten. Which is one way to do it. This is the base ten representation. This is the base 16 representation right after the second line. And finally, we have the last line and we just end it with null Terminator. We don't have to use again a new line because we just want to end the text. Another example which shows that we don't have to split the lines in our code. For example, we can just put the first line and then add the and A, which means new line and then add the second line. We don't have to separate the lines in our own source code. Summary of what we have seen this lesson. There are two basic ways to indicate the size of a string length prefix, which is the Pascal style and null termination, which is the C style. We are mostly going to work with null termination in this course. Strings are declared using the DB data syntax, just like we have declared separate bytes or bytes array. A new line is represented as the sequence D A in the Windows operation system or just the byte A in the Linux operation system. Exercise. You will see an assembly source file in the exercise folder. Assemble this file and then open the output, which will be a bin file using a hex editor. Finally, inside the hex editor, identify the strings. Just take the source file and put it against what you see in the hex editor and try to identify the strings. This is a pretty basic exercise just to make sure that you understand what the declaration of the string turns into in the final binary file. Have fun and see you in the next lesson.