 In this video, we're going to explain how does it work, the memory in a computer system. Basically, the memory in a computer system can be seen as this huge table, which is made out of what we call little cells like this one. So it's basically a very large table with a very, typically very large number of these cells. Now, this table has these specific properties. First, each one of these boxes, we call it a cell, and it has the property of being able to remember, that's why we call it memory, remember one value. What kind of value does it remember? This is what we call the size of that cell. And typically, the sizes of these cells are one by two bytes, so on and so forth. But typically, the most typical one is one byte size. The other property of memory is that all these cells have a number. They're starting from zero, one, two, three, four, and all the way down here. And this number is called the address. Now this address is going to allow me to refer to each one of these cells by a number. How many of these we have is actually the size. So if we have size equal s, let's call it this memory has s cells, clearly the first one will be number zero, and the last will be s minus one. Okay, now that we have this table over here, this is how memory works in a computer system. Let's talk about the two operations that can be done with this table. And the two operations are the first one is a write operation. Now a write operation receives things, an address, and a value. And what it does, it doesn't return anything, but intuitively the value over here is written in the cell, the address of which is given here. So given these two elements, an address and a value, this table, make sure that this value is stored in that box. The other operation, of course, is a read operation. Read operation, the only thing it receives, it's an address. And it returns the value that is currently stored in there. And little rule to remember, initially, by initially what I mean is when you turn on one of these memory chips in a computer systems, all cells have a stored garbage. In other words, when you turn on one of these chips, you cannot assume anything about the values that are stored in all these cells. Now let's talk briefly about memory size. As you can see, the size of one of these tables depends on the cell size. And the formula is to obtain the memory size is the number of cells that you have in your memory times the cell size. And as we said before, the cell size typically is either one byte, perhaps two bytes, or maybe sometimes even four bytes. Now once you know the cell size and the number of cells, then the size of a memory is just the product. So in this case would be the size of these cells times s cells that we have here because our address value goes from 0 to s minus. Now the units in which memory is typically measured are in bytes. But when we have 1,024, which is 2 to the 10 bytes, that unit we call it a kilobyte. In other words, 2 to the 10 bytes. Now when we have 1,024 kilobytes, that unit we call 1 megabyte. Or if you do the math, 2 to the 20 bytes. If again we have 1,024 megabytes, that unit we call it a gigabyte. And it's 2 to the 30 bytes. And again if we have 1,024 gigabyte, we have what we call a terabyte. 2 to the 40 bytes. And so on. There are several additional prefixes that you can use to address different memory sizes. Alright, so after we cover memory size, we cover operations here, and we cover the structure over here. Let's talk now about examples. Let's see how do we perform data storage. How do we store data in a memory? And for that, let's assume I have here just a portion of memory, okay? I have certain cells above me, and I'm going to draw some of them, and additional ones. And we're going to assume that the size of each cell is 1 byte, or 8 bits. And let's also assume that the first cell that I drew over here has the address 131312. This is actually an arbitrary number. It can be anything. So let's start with the example. Let's see the easiest one. How do we store 1 byte in memory? Well, this is a piece of cake, right? 1 byte we say is 8 bits, and typically what we do is the following. The byte could be represented by the 8 bits. Let's say 0, 1, 1, 0, 1, 0, 1, 1. But you'll agree with me that this is a little bit cumbersome to manipulate. And the trick we learned before is that we can group these bits in groups of 8 and translate them into the hexadecimal notation. This is a 6, and this is a B. This is hexadecimal. Well this notation is typically used when we refer to how the data is stored in memory. And typically what happens is that if I execute the operation right 13126B, this translates into the following action. In cell 1312, the address I store, the value 6B, which is the hexadecimal representation of this byte, which corresponds with 8 bits, which is exactly the width of the cell. So this is a result of storing 1 byte in address 1312. Things are a little bit more complicated. What if I want to store a 2 byte integer in memory? Let's assume, for example, it's the number 5,689. How do I store this integer into memory? And let's assume that I want to store that integer in position 1313. How do I do that? Well, I basically apply the same trick here, but with a little bit of a twist. However, I need to know what is the binary representation of this number, because my memory is always storing binary numbers. If I translate this to binary, if you do the math, then you end up with the following sequence of bits, 0001, 0110, 0011, and 1001. As you can see, this is the first byte, and this is the second byte. That important thing, the most significant byte is in the left-hand side, and the least significant byte is in the right-hand side. If we apply the same trick as we did before, and we manage the hexadecimal encoding, then you'll see that this corresponds with the value 1 for these 4 bits, 6 for these 4 bits over here, 3 and 9. And again, this is the hexadecimal. Now, how do we store this integer in memory, bearing in mind that this integer needs 2 bytes? Well, clearly, one option is to use this first cell and store here 16, which corresponds to the first 8, and in the next cell, store the number 39. That way, this number is now stored in these 2 cells over here. However, this is the most significant byte, this is the least significant byte. However, another possibility would be to store it following the other order. So we follow this order over here, first this byte, second this byte, but then we can also follow this convention, store first this byte and then this byte. In other words, first the thine, always hexadecimal, remember, and then 16. Well, it turns out the 2 choices are equally valid, and the 2 choices are sometimes used in different memory chips, in different computer systems. When we store first the most significant bit, we call this notation big, endian, or this policy. When we store starting from the least significant bit, like 39 first and 16 later, we call this little, and the trick to remember this is that big or little endian is always referring to the first byte that is being stored. If it is the most significant one, then it's big endian. If it is the least significant byte, then it's the little endian. What does it matter? It only matters if I tell you that in position 1313, I have previously stored a 2 byte integer, then I need to tell you also if the representation or the policy to store the number is little or big endian, so that you know which is the right order to get your bytes. The important thing to remember here is that the 2 byte integer takes 2 consecutive cells in memory, 1313 and 1314. We would say still that the number is stored in position 1313, 1313, but we have to remember that it's a 2 byte integer and we need to remember also which convention did we use, big or little endian. If a computer system chooses one of these two policies, it uses all that policy throughout its life. Let's get the last example. Let's see how do we store the string made out of five letters, hello. And again, we apply the same trick. We know that all the symbols that a computer manipulates are always translated to, first, its binary representation. In this case, I need to treat each letter separately and let's assume I'm using the ASCII convention. If I look up the encoding, I know that the H is encoded as 48, E is encoded as 65, L is encoded as 6C for the L again, and finally O is encoded as 6F. Now how do I manage to store this information in memory? Again, if I choose to store it in position 1315, then I would store the following bytes, 65, remember though that all these are hexadecimal representation, 6C, 6C, and 6F. Now the conclusion here is that the string made out of five letters will occupy actually all five cells from 1315, address 1315, to 1316, 1317, 1318, 1319. So remember, when you have to store any data in memory, what you have to remember is the address of the first cell and, very important, the size of what you are storing, and if you are storing an integer, you also have to remember which one of the two schemes, little and the unrepentant, have been used.