Video
Đăng bởi Ban biên tập OpenER ·
Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
[MUSIC PLAYING] Hi. Welcome back.
[NHẠC PHÁT] Xin chào. Chào mừng bạn quay trở lại.
We are continuing with this five assembly, so let's see those instructions that do data transfers to and from the memory.
Chúng ta tiếp tục với lắp ráp RISC-V này, vì vậy hãy xem những hướng dẫn thực hiện chuyển đổi dữ liệu tới và từ bộ nhớ.
Remember the picture from early in the course when we introduced the principal of memory hierarchy? The processor core was at the very top with its registers. Registers are extremely fast.
Hãy nhớ lại hình ảnh từ đầu khóa học khi chúng ta giới thiệu nguyên tắc phân cấp bộ nhớ. Lõi bộ xử lý nằm ở trên cùng với các thanh ghi của nó. Thanh ghi cực kỳ nhanh.
They share that precious real state with the processor core and, therefore, are extremely expensive. So we have a small number of them. To be precise, 32 in total.
Chúng chia sẻ không gian quý giá đó với lõi bộ xử lý và do đó cực kỳ đắt tiền. Vì vậy chúng ta có một số lượng nhỏ chúng. Để chính xác, tổng cộng 32 cái.
Now, on a separate chip, typically, there is the main memory. It is implementing a different technology. It is called DRAM, which stands for Dynamic Random Access Memory, and it comes in different flavors.
Bây giờ, trên một chip riêng biệt, thường là là bộ nhớ chính. Nó sử dụng công nghệ khác. Nó được gọi là DRAM, viết tắt của Dynamic Random Access Memory, và nó có nhiều dạng khác nhau.
You might have heard of double data rate, which is DDR.
Bạn có thể đã nghe nói về double data rate, đó là DDR.
It comes in different generations, three, four, five, or maybe high bandwidth memory, which is HBM, and that also has different generations too.
Nó có nhiều thế hệ khác nhau, thế hệ ba, bốn, năm, hoặc có thể là high bandwidth memory, đó là HBM, và nó cũng có các thế hệ khác nhau.
There is HBM, HBM2, and then now, we are getting to HBM3 generation. DRAM Is also fast, but not nearly as fast as the registers. It is priced reasonably.
Có HBM, HBM2, và bây giờ, chúng ta đang tiến tới thế hệ HBM3. DRAM cũng nhanh, nhưng không gần nhanh như các thanh ghi. Nó có giá hợp lý.
You get a lot of gigabytes for a few tens of dollars, and it has medium capacity compared to mass storage that we have in disks and solid state drives.
Bạn có được rất nhiều gigabyte với giá chỉ vài chục đô la, và nó có dung lượng trung bình so với bộ nhớ hàng loạt mà ta có trong các ổ đĩa và ổ trạng thái rắn.
But the question here is, how big is this gap in the pyramid? How much are registers really faster than the memory?
Nhưng câu hỏi ở đây là, khoảng cách trong kim tự tháp này lớn như thế nào? Thanh ghi thực sự nhanh hơn bộ nhớ bao nhiêu lần?
So given that there are 32 registers, 32 bits each, total of 1,024 bits, meaning 128 bytes total there.
Vậy với 32 thanh ghi, mỗi cái 32 bit, tổng cộng 1.024 bit, tức là 128 byte ở đó.
On the other hand, in DRAM, we may have in a low end laptop, about two gigabytes, in a high and laptop, up to 64 gigabytes of memory, and the server may have a terabyte of memory.
Mặt khác, trong DRAM, ta có thể có ở một laptop cấp thấp, khoảng hai gigabyte, ở một laptop cấp cao, tới 64 gigabyte bộ nhớ, và máy chủ có thể có một terabyte bộ nhớ.
And the physics dictates that smaller is faster, main claim to fame of Bruce Lee. For example, or typically, point guards are a lot faster than the centers in basketball.
Và vật lý chỉ ra rằng cái nhỏ hơn thì nhanh hơn, đó là điều Bruce Lee nổi tiếng vì. Ví dụ, hoặc thường xuyên, các tiền vệ nhanh hơn rất nhiều so với những tiền đạo trung phong trong bóng rổ.
How much faster are the registers and then DRAM really?
Thanh ghi thực sự nhanh hơn DRAM bao nhiêu lần?
So when you put that on a piece of paper, we will find out that the numbers work out, that there are about 50 to 500 times faster.
Vậy khi bạn vẽ cái đó trên một tờ giấy, chúng ta sẽ thấy rằng các con số phù hợp, có khoảng 50 đến 500 lần nhanh hơn.
So, that's in terms of latency of one access or the first access to DRAM than to get one individual data item.
Vậy, điều đó là về độ trễ của một lần truy cập hay lần đầu tiên truy cập DRAM so với việc lấy một dữ liệu riêng lẻ mục.
You know, that access takes a few tens of nanoseconds, and you compare that with the fraction of a nanosecond, that it takes us to access registers.
Bạn biết đó, lần truy cập đó mất vài chục nanogiây, và bạn so sánh điều đó với một phần của nanogiây, mà nó mất cho chúng ta để truy cập các thanh ghi.
Keep in mind that this is for one isolated access. Many succeed subsequent accesses to nearby memory locations will go faster.
Hãy nhớ rằng đây là cho một lần truy cập cô lập. Nhiều lần truy cập tiếp theo đến các vị trí bộ nhớ gần đó sẽ nhanh hơn.
To put this in perspective, a great picture to keep in mind is this picture of Jim Gray's storage latency analogy.
Để đặt điều này trong bối cảnh, một bức tranh tuyệt vời để ghi nhớ là bức tranh này về phép loại suy độ trễ lưu trữ của Jim Gray.
What we have on the left hand side are our registers, and they take about a nanosecond to access. Memory takes about two orders of magnitude more, so 100 nanoseconds.
Những gì chúng ta có ở bên trái là các thanh ghi của chúng ta, và chúng mất khoảng một nanogiây để truy cập. Bộ nhớ mất khoảng hai độ lớn hơn, tức là 100 nanogiây.
Let's try to make an equivalent to tasks that we do as humans.
Hãy thử tạo một phép tương tự với những công việc chúng ta làm hàng ngày.
So for example, if we would like to retrieve data from our heads, let's say that the retrieving data from a register, which is in our head, would take about a minute.
Chẳng hạn, nếu chúng ta muốn truy xuất dữ liệu từ trí óc của mình, giả sử truy xuất dữ liệu từ một thanh ghi, cái nằm trong trí óc của chúng ta, sẽ mất khoảng một phút.
So what is it the takes 100 times longer? Where would that data be?
Vậy thì cái gì mất 100 lần lâu hơn? Dữ liệu đó sẽ nằm ở đâu?
Well, when we are all in Berkeley and live and all that, it would be normal to make an analogy to driving to a town.
Khi chúng ta ở Berkeley và sống ở đó, v.v., việc tạo phép tương tự là lái xe đến một thành phố sẽ là chuyện bình thường.
For example, we forgot the piece of paper somewhere and we are going to go and retrieve it.
Chẳng hạn, chúng ta quên một mảnh giấy ở đâu đó và chúng ta sẽ đi lấy nó.
Let's say, the town is Sacramento, which is roughly 100 minutes away or an hour and a half away. That's big penalty on time.
Giả sử thành phố là Sacramento, cách đó khoảng 100 phút hay một tiếng rưỡi đi. Đó là một khoảng thời gian mất mát lớn.
You can do a lot of stuff in an hour and a half. Or if the gap is 500x, that it may be the case in some cases.
Bạn có thể làm được rất nhiều việc trong một giờ rưỡi. Hoặc nếu khoảng cách là 500 lần, điều đó có thể xảy ra trong một số trường hợp.
In 500 minutes, we can make it to Los Angeles and back. It would not be convenient to leave that piece of paper with our data in LA and having to retreat it.
Trong 500 phút, chúng ta có thể đi đến Los Angeles và quay trở lại. Sẽ không thuận tiện nếu để tờ giấy đó với dữ liệu của chúng ta ở LA và phải lấy nó lại.
They have to go to retrieve it, or Sacramento. So, this is there to try to illustrate how expensive it is to have to go get the data from the memory.
Họ phải đi lấy nó, ở Sacramento hoặc LA. Vì vậy, điều này nhằm minh họa nó tốn kém bao nhiêu để phải đi lấy dữ liệu từ bộ nhớ.
Let's try to see how these instructions look like on the simple example. So the first thing that we're going to look at involves loading from memory to register.
Hãy thử xem những lệnh này trông như thế nào trên một ví dụ đơn giản. Vì vậy điều đầu tiên mà chúng ta sẽ xem xét liên quan đến việc tải dữ liệu từ bộ nhớ vào thanh ghi.
So this is a simple code that we can see, and we'll try to translate it into RISC-V assembly. So let's go by it line by line.
Vậy đây là một đoạn code đơn giản mà chúng ta có thể thấy, và chúng ta sẽ cố gắng dịch nó sang hợp ngữ RISC-V. Vậy hãy đi từng dòng một.
So what we have here, we have a declaration of an integer, rate of 100 elements.
Vậy những gì chúng ta có ở đây là khai báo của một mảng số nguyên có 100 phần tử.
And then, the second line very simply takes element day three of that array and adds it to the variable age and store the result in a variable g.
Rồi, dòng thứ hai rất đơn giản lấy phần tử thứ ba của mảng đó và cộng nó vào biến age và lưu kết quả vào một biến g.
I'm using kind of assembly terminology again, but you guys know what I mean here.
Tôi đang dùng một loại thuật ngữ hợp ngữ ở đây, nhưng các bạn biết ý tôi rồi.
So, we know how to add things in registers, so we need to figure out a way how to get the value of this third element in the array that resides in memory to our register.
Vậy, chúng ta biết cách cộng các giá trị trong thanh ghi, nên chúng ta cần tìm cách để lấy giá trị của phần tử thứ ba của mảng đó nằm trong bộ nhớ về thanh ghi của chúng ta.
We need a new instruction there. So the new instruction is showing up right here, it is Load Word.
Chúng ta cần một lệnh mới ở đó. Vậy lệnh mới hiển thị ở đây, đó là Load Word.
So load word and RISC-V goes to the memory, and loads from the memory to a register. So the syntax is fairly straightforward to understand here. Take a look.
Vậy load word trong RISC-V sẽ đi tới bộ nhớ, và tải từ bộ nhớ vào một thanh ghi. Vậy cú pháp khá đơn giản để hiểu ở đây. Xem kỹ.
This is my destination register. So mnemonic is lw, destination register, and then we have this. What does that mean?
Đây là thanh ghi đích của tôi. Vậy mnemonics là lw, thanh ghi đích, rồi chúng ta có cái này. Điều đó có nghĩa gì?
We have to specify the base register, which is the pointer to the element A zero of the array, and then specify the offset to the element of the array that we would like to retrieve.
Chúng ta phải chỉ định thanh ghi cơ sở, là con trỏ tới phần tử A không của mảng, rồi chỉ định độ lệch tới phần tử của mảng mà chúng ta muốn lấy ra.
So the base pointer points with an offset of zero to the element A zero. We Would like to get the third element in the array. But remember, we are storing integers in this array.
Vậy con trỏ cơ sở trỏ đến với offset bằng không tới phần tử A không. Chúng ta muốn lấy phần tử thứ ba trong mảng. Nhưng hãy nhớ, chúng ta đang lưu trữ các số nguyên trong mảng này.
They are 32 bits wide, 32 bits is four bytes and RISC-V addresses each byte in memory. So we need to specify this offset in bytes.
Chúng có độ rộng 32 bit, 32 bit là bốn byte và RISC-V địa chỉ hóa từng byte trong bộ nhớ. Vì vậy chúng ta cần chỉ định offset này tính bằng byte.
Three words is equal to 12 bites, so that's why this offset is specified by this 12. That's why we have 12 here.
Ba từ bằng 12 byte, đó là lý do tại sao offset này được chỉ định bằng 12. Đó là lý do chúng ta có 12 ở đây.
So the actual address of the datum in the memory is calculated as the address of the Bain base pointer, which is the content of a register X15 plus 12.
Vậy địa chỉ thực tế của dữ liệu trong bộ nhớ được tính là địa chỉ của con trỏ cơ sở tức là nội dung của thanh ghi X15 cộng 12.
Very well. So keep in mind that this offset must be a constant known at the assembly time.
Rất tốt. Vậy hãy nhớ rằng offset này phải là một hằng số đã biết tại thời điểm lắp ráp.
So if you picture it, another thing to remember this syntax well is the direction of the data flow within the instruction that kind of mimics what we see in reality.
Vậy nếu bạn hình dung ra, một điều khác để ghi nhớ cú pháp này tốt là hướng của dòng dữ liệu trong lệnh, cách mà nó phản ánh những gì chúng ta thấy trong thực tế.
So data flow goes from right to left, like what we have seen in other instructions. So we add the instruction, also moves the data from right to left.
Vậy dòng dữ liệu chạy từ phải sang trái, giống như những gì chúng ta đã thấy trong các lệnh khác. Vậy là lệnh add, cũng dịch chuyển dữ liệu từ phải sang trái.
We add contents of registers x12 and x10 and store the result in x11.
Chúng ta cộng nội dung của các thanh ghi x12 và x10 và lưu trữ kết quả trong x11.
So, that should resemble a picture that we have had in the previous segment. We take data from the right in the memory and move it to the left, to the registers.
Vậy nên, điều đó phải giống như một hình ảnh mà chúng ta đã có trong đoạn trước. Chúng ta lấy dữ liệu từ bên phải trong bộ nhớ và dịch chuyển nó sang trái, vào các thanh ghi.
So we load from memory to registers. We are going from right to left. So to recap what we have seen here, we have essentially translated this C code.
Vậy là chúng ta tải dữ liệu từ bộ nhớ vào các thanh ghi. Chúng ta đang đi từ phải sang trái. Vậy để tóm lại những gì chúng ta đã thấy ở đây, chúng ta về cơ bản đã dịch mã C này.
One note to make here, there is no really assembly instruction.
Một điểm cần lưu ý ở đây là không có thực sự lệnh assembly nào.
For now, for the translates, the declaration of allocating this integer array, all what we are translating is this one scene instruction that adds h plus A3 and saves it as g.
Hiện tại, đối với các phép dịch, khai báo cấp phát mảng số nguyên này, tất cả những gì chúng ta đang dịch là lệnh cảnh này cộng h plus A3 và lưu nó dưới dạng g.
So what we have there, we first load the contents of this memory location specified by the base pointer plus the offset to a temporary register x10.
Vậy những gì chúng ta có ở đó, trước tiên chúng ta tải nội dung của vị trí bộ nhớ này được chỉ định bởi con trỏ cơ sở cộng với offset vào một thanh ghi tạm thời x10.
And then, we add the contents of x10 with x12 and store the result in x11.
Và sau đó, chúng ta cộng nội dung của x10 với x12 và lưu kết quả vào x11.
Notice that we could have been a little bit more optimized here and could have saved this, could have reused registers x10 for the variable g.
Lưu ý rằng chúng ta có thể đã được tối ưu hóa một chút ở đây và có thể lưu giữ cái này, có thể dùng lại thanh ghi x10 cho biến g.
Okay, hopefully this made sense. Let's see the other instruction. The other instruction that we need is to store from the register to the memory. So it goes the other way.
Được rồi, hy vọng điều này có ý nghĩa. Hãy xem lệnh khác. Lệnh khác mà chúng ta cần là lưu từ thanh ghi vào bộ nhớ. Nó diễn ra theo cách khác.
We have something in the register and you want to put it in a memory. And here is an example code, exact same code, except that we have changed a bit in it.
Chúng ta có cái gì đó trong thanh ghi và bạn muốn đặt nó vào bộ nhớ. Và đây là một ví dụ mã, mã chính xác giống nhau, ngoại trừ chúng ta đã thay đổi một chút.
Instead of saving it in a variable g, we will need to save it in another element in the array, which, in this case, is A10.
Thay vì lưu nó vào biến g, chúng ta sẽ cần lưu nó vào phần tử khác trong mảng, trong trường hợp này, là A10.
It is offset by 10 words from the base pointer. So the first two lines that are identical, as what you had before, we did get a little bit better here.
Nó được bù đắp bằng 10 từ từ con trỏ cơ sở. Vậy hai dòng đầu tiên giống nhau, giống như những gì bạn có trước, chúng ta đã cải thiện một chút ở đây.
We have learned. We are reusing x10 as a temporary variable. And now, here comes a new instruction.
Chúng ta đã học được. Chúng ta đang tái sử dụng x10 như một biến tạm thời. Và bây giờ, một lệnh mới xuất hiện.
This new instruction is store, oops — store word and RISC-V.
Lệnh mới này là store, ơi — store word trong RISC-V.
Does the word in RISC-V does the following, he takes the contents of a register x10, where we have saved our temporary result and moves it to a memory location that is addressed by the base pointer and the offset.
Lệnh word trong RISC-V thực hiện như sau, nó lấy nội dung của thanh ghi x10, nơi chúng ta đã lưu kết quả tạm thời và di chuyển nó đến một vị trí bộ nhớ được địa chỉ hóa bởi con trỏ cơ sở và độ lệch.
And in this case, since we want to write in the 10th element of the array, we have to specify the offset as 10 times four bytes away, so that is 40 is the opposite here.
Và trong trường hợp này, vì chúng ta muốn ghi vào phần tử thứ 10 của mảng, chúng ta phải chỉ định độ lệch là 10 lần 4 byte, nên 40 là giá trị ở đây.
So that's why we have 40 there.
Vì vậy đó là lý do tại sao chúng ta có 40 ở đó.
The data movement or data flow here is the other way around. We are storing from the registry in our picture to the memory.
Chuyển động dữ liệu hoặc luồng dữ liệu ở đây đi theo hướng khác. Chúng ta đang lưu trữ từ thanh ghi trong hình của chúng ta vào bộ nhớ.
And the data flow is going from left to right. Okay, very well. So that should hopefully keep confusion out of the way.
Và luồng dữ liệu đi từ trái sang phải. Được rồi, rất tốt. Vì vậy điều đó có thể giúp tránh sự nhầm lẫn.
We are always in this syntax.
Chúng ta luôn sử dụng cú pháp này.
Load operations are moving data from right to left in store operations, or storing from registered to memory and it is moving from left to right.
Các phép tải dữ liệu đang di chuyển dữ liệu từ phải sang trái trong các phép lưu trữ, hay lưu từ thanh ghi vào bộ nhớ và nó đi từ trái sang phải.
One thing to keep in mind, these offsets for loading and storing should be multiples of four.
Một điều cần ghi nhớ, các offset này cho phép tải và lưu trữ phải là bội số của bốn.
It's written here as must, because that's a really good practice.
Nó được viết ở đây là phải, bởi vì đó là thực hành rất tốt.
One slight disclaimer of RISC-V I see allows for misaligned memory accesses, meaning that it allows you to scribble, to start writing in the middle of one word, and then write a half for word in one memory location and another half in another memory location,
Một lưu ý nhỏ về RISC-V mà tôi thấy cho phép truy cập bộ nhớ không căn chỉnh, tức là nó cho phép bạn ghi, bắt đầu ghi ở giữa một từ, rồi ghi một nửa từ ở một vị trí bộ nhớ và nửa còn lại ở một vị trí bộ nhớ khác,
but that's not advisable. It is just there to support some legacy code. We should not do that, because that would be something that is very, very slow and it's really messy.
nhưng điều đó không được khuyến khích. Nó chỉ có để hỗ trợ một số mã cũ. Chúng ta không nên làm vậy, vì nó sẽ rất, rất chậm và nó thực sự rối rắm.
We don't want to be just scribbling all over the memory, so you should take this should really is a must.
Chúng ta không muốn chỉ ghi lộn xộn trên bộ nhớ, nên bạn nên coi điều này thực sự là phải.
Now keep in mind that these load boards are stored board instructions support essentially taking the entire 32 bits from a memory location, a word from memory, and putting it into a register, so they fit really nicely.
Bây giờ hãy nhớ rằng các lệnh tải và lưu trữ hỗ trợ về cơ bản lấy toàn bộ 32 bit từ một vị trí bộ nhớ một từ từ bộ nhớ và đưa nó vào một thanh ghi, nên chúng vừa vặn rất tốt.
32 bits and 32 bits, or when we are storing to the memory, we take 32 bits from a register and place it into a memory.
32 bit và 32 bit, hoặc khi chúng ta lưu vào bộ nhớ, chúng ta lấy 32 bit từ một thanh ghi và đặt nó vào bộ nhớ.
But often, we operate with different kinds of data types that are not necessarily 32 bits wide, so it would be wasteful to use 32-bit memory locations to store just eight bits worth of data.
Nhưng thường thì chúng ta làm việc với những loại dữ liệu khác nhau mà không nhất thiết phải rộng 32 bit, vì vậy sẽ lãng phí nếu dùng vị trí bộ nhớ 32 bit để chỉ lưu trữ tám bit dữ liệu.
Like when we are storing characters, when we are storing colored channels that are typically all eight bits wide.
Như khi chúng ta lưu trữ ký tự, khi chúng ta lưu trữ các kênh màu mà thường là tám bit rộng.
So RISC-V is conscious of that and supports shorter operants. So it allows for load byte and store byte.
Vì vậy RISC-V nhận thức được điều đó và hỗ trợ toán hạng ngắn hơn. Vì vậy nó cho phép tải byte và lưu byte.
The format is exactly the same as what we have seen in load word, stored word as we see here.
Định dạng hoàn toàn giống như chúng ta đã thấy trong load word, store word như được thể hiện ở đây.
So we have load byte, it loads our content from a memory location specified by the base pointer x11 with an offset of three bytes to a memory, to a register x10.
Vậy nên chúng ta có load byte, nó tải nội dung của chúng ta từ một vị trí bộ nhớ được chỉ định bởi con trỏ cơ sở x11 với một offset là ba byte vào bộ nhớ, vào thanh ghi x10.
So it is the same as what we have seen before with one difference, is that this offset now really doesn't have to be and shouldn't be four, because we should be able to pluck any byte from any part of a word and write it in our destination register.
Vậy nên nó giống như những gì chúng ta đã thấy trước đây với một điểm khác, là offset này bây giờ thực sự không cần và không nên là bốn, vì chúng ta nên có thể lấy bất kỳ byte nào từ bất kỳ phần nào của một từ và ghi nó vào thanh ghi đích của chúng ta.
Now, by convention, when we take that byte, no matter where it is stored in a memory, because all characters can be packed right next to each other as bytes in the world, so we would be indexing those memory locations by one, it will copy it to the low byte position of the register x10.
Bây giờ, theo quy ước, khi chúng ta lấy byte đó, không quan trọng nó được lưu trữ ở đâu trong bộ nhớ, vì tất cả các ký tự có thể được đóng gói ngay cạnh nhau dưới dạng các byte trên thế giới, vì vậy chúng ta sẽ lập chỉ mục những vị trí bộ nhớ đó theo từng cái, nó sẽ sao chép nó vào vị trí byte thấp của thanh ghi x10.
So if this is our registry x10 here, no matter where we pick the Python in this case, the opposite is three, so take the highest byte, the most significant byte from that memory location that takes 11 points to.
Vậy nếu đây là thanh ghi x10 của chúng ta, bất kể chúng ta chọn Python ở đâu trong trường hợp này, ngược lại là ba, vì vậy lấy byte cao nhất, byte có ý nghĩa nhất từ vị trí bộ nhớ đó mà nó trỏ tới.
It would write it into a low byte position of register x10. There is another important thing to keep in mind here.
Nó sẽ ghi nó vào vị trí byte thấp của thanh ghi x10. Có một điều quan trọng khác cần nhớ ở đây.
We don't know what kind of data types we are operating. But in many cases, we will be operating with signed numbers.
Chúng ta không biết chúng ta đang làm việc với kiểu dữ liệu nào. Nhưng trong nhiều trường hợp, chúng ta sẽ làm việc với các số có dấu.
So remember, in sign numbers, if it is a two's compliment number, this first bit of a byte determines what is the sign, whether that number is positive or negative by looking at it.
Vì vậy, hãy nhớ rằng, đối với các số có dấu, nếu đó là một số bù hai thì bit đầu tiên của một byte xác định dấu, liệu số đó là dương hay âm bằng cách nhìn vào nó.
If it is zero, the number is positive. If it is a one, the number is negative. So what should we do when we copy this byte to our register to preserve it's sign?
Nếu nó là không, thì số đó là dương. Nếu nó là một, thì số đó là âm. Vậy khi chúng ta sao chép byte này vào thanh ghi của mình, chúng ta nên làm gì để bảo toàn dấu của nó?
So, if we just write zeros in the upper three bytes, well, then no matter what their number was, we are going to make it look like it's a positive number.
Nếu chúng ta chỉ viết các số không vào ba byte phía trên, thì bất kể số đó là gì, chúng ta sẽ làm cho nó trông như một số dương.
We do have to preserve the sign. So the way how we preserve the sign, we take a note of that top bit, the most significant bit in the byte and copy it over.
Chúng ta phải bảo toàn dấu. Cách chúng ta bảo toàn dấu, chúng ta chú ý đến bit trên cùng đó, bit có ý nghĩa nhất trong byte và sao chép nó qua.
That copy over is called a sign extension.
Sao chép đó được gọi là mở rộng dấu.
And the way how it looks in this picture, we actually take that bit and smear it all over the upper bytes in the word.
Và cách nó hiển thị trong hình này, chúng ta thực sự lấy bit đó và trải nó trên khắp các byte phía trên trong từ.
So we take the most significant bit out of that low byte, and smear it over like avocado on a toast. Does that make sense?
Vậy chúng ta lấy bit có ý nghĩa nhất từ byte thấp đó, và trải nó như bơ trên bánh mì. Điều đó có hợp lý không?
So that's sign extension. We don't always want to do sign extension.
Vậy đó là mở rộng dấu. Chúng ta không phải lúc nào cũng muốn mở rộng dấu.
So if we don't want it, if you're certain this is not a sign number that we are transferring from memory, it is a character or something that is always a positive number, like color intensity.
Vậy nếu chúng ta không muốn, nếu bạn chắc chắn rằng đây không phải một số có dấu mà chúng ta đang truyền từ bộ nhớ, nó là một ký tự hay thứ gì đó luôn là một số dương, như cường độ màu.
RISC-V supports load bite unsigned, which is another instruction that copies a bite from a memory, loads of bite from memory to a register but does not do sign extension, so it just fills the upper bytes with zeros.
RISC-V hỗ trợ load bite unsigned, đó là một lệnh khác sao chép một byte từ bộ nhớ, tải một byte từ bộ nhớ vào một thanh ghi nhưng không mở rộng dấu, vì vậy nó chỉ điền các byte trên cùng bằng các số không.
A question for you, why there is no equivalent of that no unsigned store byte, so no SBU?
Một câu hỏi cho bạn, tại sao không có tương đương với việc không unsigned store byte, tức là không có SBU?
Well, it doesn't make sense. We are just taking a byte and plucking it into a memory location. There is no sign extension that is happening. We don't have to fill out anything there.
Vâng, nó không có ý nghĩa gì cả. Chúng ta chỉ lấy một byte và nhân nó vào một vị trí bộ nhớ. Không có mở rộng dấu nào xảy ra. Chúng ta không phải điền bất cứ gì ở đó.
Now, LBU has exactly the same syntax as lb.
Bây giờ, LBU có cú pháp y hệt như lb.
All right, so let's do a little more of an exercise here to see how all this clicks together.
Được, vậy chúng ta hãy làm thêm một bài tập ở đây để xem tất cả những cái này kết hợp như thế nào.
So the question here, in this example, is, what is in x12 after these three instructions are executed?
Vậy câu hỏi ở đây, trong ví dụ này, là, x12 chứa gì sau khi ba lệnh này được thực thi?
And feel free to pause here, work it out yourself, or you can just follow me as I do it. So these are the three instructions that are fairly straightforward.
Và hãy thoải mái dừng lại ở đây, tự mình giải nó, hoặc bạn có thể chỉ theo dõi khi tôi làm nó. Vậy đây là ba lệnh mà khá đơn giản.
The first one, essentially, stores three of five in register x11.
Lệnh đầu tiên, về cơ bản, lưu ba của năm vào thanh ghi x11.
The second round stores a word, source the content of x11 into a memory location that is pointed by the — pointed to by the base pointer x5 with no offsets, so it just takes that address that is stored in x5 and writes right there.
Lệnh thứ hai lưu một từ, lấy nội dung của x11 vào một vị trí bộ nhớ được trỏ tới bởi — được trỏ tới bởi con trỏ cơ sở x5 mà không có offset, nó chỉ lấy địa chỉ đó được lưu trữ trong x5 và ghi ngay tại đó.
And then finally, the last instruction, lb takes byte with offset of one from that same word and writes it in x12.
Và cuối cùng, lệnh cuối cùng, lb lấy một byte với offset là một từ cùng từ đó và ghi nó vào x12.
I think you got the picture. If you haven't, let me do the — let me walk you through that. So what we have in x11 after the first instruction is this.
Tôi nghĩ bạn đã hiểu rồi. Nếu bạn chưa hiểu, để tôi — để tôi hướng dẫn bạn qua điều đó. Vậy những gì ta có trong x11 sau lệnh đầu tiên là thế này.
We have F5 03 00 00. Let's say that x5 points to this location.
Ta có F5 03 00 00. Giả sử x5 trỏ tới vị trí này.
So then, this content of a register x11 will be written exactly there. So we are going to get F5 03 00 00.
Vì vậy, nội dung của thanh ghi x11 sẽ được ghi chính xác tại đó. Vậy nên chúng ta sẽ được F5 03 00 00.
Now, when we see load byte with an offset to one, which byte are we going to pick? So this is a word with an offset of zero.
Bây giờ, khi chúng ta thấy load byte với offset bằng một, chúng ta sẽ chọn byte nào? Vậy đây là một word với offset bằng không.
The next word in memory has offset of four, because we have byte access in the memory. So the byte that has an offset of one is this one.
Word tiếp theo trong bộ nhớ có offset bằng bốn, vì chúng ta có truy cập từng byte trong bộ nhớ. Vậy byte có offset bằng một là byte này.
So load byte is going to take the contents of this byte, of that word, second byte in that order, the second to the least significant byte in the word, and write it here.
Vậy load byte sẽ lấy nội dung của byte này, của word đó, byte thứ hai theo thứ tự này, byte thứ hai kể từ byte ít có nghĩa nhất trong word, và ghi nó ở đây.
So it will be 03. Well, take a look at the most significant bit in that word and sign extend it to fill the rest of the register with zeros.
Vậy nó sẽ là 03. Vâng, hãy nhìn vào bit có nghĩa nhất trong word đó và mở rộng dấu nó để lấp đầy phần còn lại của thanh ghi bằng các số không.
Hopefully, this is a good illustration and you get to practice a few of these afterwards.
Hy vọng đây là một minh họa tốt và bạn sẽ có cơ hội thực hành một số chúng sau này.
One little final note, we have only introduced about eight RISC-V assembly instructions.
Một lưu ý nhỏ cuối cùng, chúng ta vừa mới giới thiệu khoảng tám lệnh hợp ngữ RISC-V.
And one who is paying good enough attention here might notice that at least one of them is redundant. It's add immediate.
Và ai đó chú ý đủ kỹ ở đây có thể nhận thấy rằng ít nhất một trong số chúng là thừa. Đó là add immediate.
We really don't need add immediate. We can replace add immediate with the sequence of two instructions that we have here.
Chúng ta thực sự không cần add immediate. Chúng ta có thể thay thế add immediate bằng chuỗi hai lệnh mà chúng ta có ở đây.
We can store a constant in the memory, load it from the memory into a register, and then add that new immediate to the content of another register, and voila, that is add immediate.
Chúng ta có thể lưu trữ một hằng số trong bộ nhớ, tải nó từ bộ nhớ vào một thanh ghi, rồi sau đó cộng immediate mới đó với nội dung của một thanh ghi khác, và voilà, đó là add immediate.
So, what's up with that? Why do we have redundancy in RISC-V? Isn't reduced instruction set computer supposed to be really lean and mean?
Vậy, có vấn đề gì vậy? Tại sao chúng ta lại có tính thừa trong RISC-V? Máy tính tập lệnh rút gọn không phải được thiết kế thật gọn gàng sao?
Yeah, this does not change the fact that this reduced instruction set computer add immediate is necessary, because these two instructions — if we don't have add immediate, those instructions would be very slow.
Vâng, điều này không thay đổi thực tế rằng máy tính tập lệnh rút gọn này add immediate là cần thiết, vì hai lệnh này — nếu chúng ta không có add immediate, những lệnh đó sẽ chạy rất chậm.
We would have to go for add immediate to Sacramento to get it and then do the addition.
Chúng ta sẽ phải đi để lấy add immediate và sau đó thực hiện phép cộng.
That would be a really, really low performance machine, because add immediates are very frequent in assembly code.
Đó sẽ là một máy có hiệu năng rất, rất thấp, vì add immediate rất phổ biến trong mã assembly.
So, this is — here in the instruction, there is necessary to support the common case, and the common case is adding immediate.
Vì vậy, điều này — ở đây trong lệnh, việc này là cần thiết để hỗ trợ trường hợp phổ biến, và trường hợp phổ biến là add immediate. và trường hợp thường gặp là cộng với immediate.
There is one big difference between these two instructions. We'll touch on that in just a little bit, in a couple of segments, but the point here is immediates in the instruction field are limited.
Có một khác biệt lớn giữa hai lệnh này. Chúng ta sẽ đề cập tới điều đó trong chốc lát, trong vài phần tiếp theo, nhưng điểm cần biết ở đây là các số nguyên tức thời trong trường lệnh bị giới hạn.
They have to be less than 32 bits, because we have to store other information. So immediates are a short part of the instruction field.
Chúng phải nhỏ hơn 32 bit, vì chúng ta phải lưu trữ những thông tin khác. Vì vậy, số nguyên tức thời là một phần ngắn của trường lệnh.
So the range of immediates in addi is relatively short.
Vì thế, dãy số nguyên tức thời trong lệnh addi tương đối ngắn.
If you need a longer immediate, we would have to load it from a memory or there may be other mechanisms to generate it. But we'll pause here.
Nếu bạn cần một số nguyên tức thời dài hơn, chúng ta sẽ phải nạp nó từ bộ nhớ hoặc có thể có những cơ chế khác để tạo ra nó. Nhưng chúng ta sẽ tạm dừng ở đây.
I'll see you in a bit with more RISC-V instructions.
Tôi sẽ gặp bạn chốc lát nữa với thêm các lệnh RISC-V.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.