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] BORIVOJE NIKOLIC: Hey, welcome back. I have a question for you.
[NHẠC PHÁT] BORIVOJE NIKOLIC: Này, chào bạn trở lại. Mình có một câu hỏi cho bạn.
How do you use Patterson and Hennessy textbook to figure out if an egg is hard boiled or not? You take the book, use it as a flat surface, and spin an egg on it.
Bạn dùng sách giáo khoa Patterson và Hennessy để xác định trứng đã luộc chín hay chưa như thế nào? Bạn lấy cuốn sách, dùng nó làm bề mặt phẳng, và xoay một quả trứng trên nó.
When you stop the spinning, whether it wiggles or not determines whether the egg is hard boiled. In this case, it's most likely hard boiled.
Khi bạn dừng việc xoay, cái trứng có lắc lư hay không sẽ xác định xem trứng có luộc chín hay không. Trong trường hợp này, nó hầu như chắc chắn đã luộc chín.
But we are still talking about RISC-V instruction set architecture and assembly instructions. We have learned three instructions so far — add, sub, and add immediate.
Nhưng chúng ta vẫn đang nói về tập hợp lệnh RISC-V và các lệnh hợp ngữ. Cho đến nay chúng ta đã học ba lệnh — add, sub, và add immediate.
The book and the green sheet uses a very compact description to very compactly remind you what these instructions do. It is in a language called Verilog.
Sách giáo khoa và tờ giấy xanh sử dụng một mô tả rất gọn để một cách rất ngắn gọn nhắc nhở bạn những instruction này làm gì. Nó được viết bằng một ngôn ngữ gọi là Verilog.
We are not learning Verilog in this class, but it is a language that, in syntax, is very similar to C, although it has completely different stuff. It is used for describing hardware.
Chúng ta không học Verilog trong lớp này, nhưng nó là một ngôn ngữ mà về mặt cú pháp, rất giống C, mặc dù nó có những thứ hoàn toàn khác. Nó được dùng để mô tả hardware.
But given its simple syntax similar to C, we can infer what each instruction does from its compact description.
Nhưng dựa vào cú pháp đơn giản giống C, chúng ta có thể suy ra mỗi instruction làm gì từ mô tả ngắn gọn của nó.
For example, the add instruction adds the contents of the register rs2 to the contents of register rs1 and store it in the destination register rd.
Chẳng hạn, instruction add cộng nội dung của register rs2 với nội dung của register rs1 và lưu trữ nó trong register đích rd.
Conversely, the sub instruction takes the contents of a source register rs2, subtracts it from the source register rs1 — from the contents of the source register rs1 — and stores the result in the destination register rd.
Ngược lại, instruction sub lấy nội dung của source register rs2, trừ nó từ source register rs1 — từ nội dung của thanh ghi nguồn rs1 — và lưu trữ kết quả vào thanh ghi đích rd.
Our add immediate instruction is similar, but a little bit different. It takes the contents of the source register rs1 and adds an immediate value to it.
Lệnh add immediate của chúng ta tương tự, nhưng hơi khác một chút. Nó lấy nội dung của thanh ghi nguồn rs1 và cộng một giá trị immediate vào đó.
Immediate value is a constant that is encoded as a part of an instruction. So it is there in the instruction.
Giá trị immediate là một hằng số được mã hóa như là một phần của lệnh. Vì vậy nó ở trong lệnh.
The processor takes that number from the instruction and adds it to the contents of a register rs1 and stores the result in the destination register rd.
Bộ xử lý lấy số đó từ lệnh và cộng nó với nội dung của thanh ghi rs1, rồi lưu trữ kết quả vào thanh ghi đích rd.
There is a limited number of registers in every processor.
Có một số lượng hạn chế các thanh ghi trong mỗi bộ xử lý.
RISC-V architecture specifies 32 registers, out of which we can theoretically use up to 31 because register x0 is hardwired to 0, so we can use up to 31 of them for storing temporary variables in them.
Kiến trúc RISC-V chỉ định 32 thanh ghi, trong đó về lý thuyết chúng ta có thể sử dụng tối đa 31 thanh ghi vì thanh ghi x0 được cấu hình cứng thành 0, vì vậy chúng ta có thể sử dụng tối đa 31 thanh ghi để lưu trữ các biến tạm thời trong đó.
But what we'll find out, some of these registers, in more complex programs, have some designated use for something else. So we generally will be able to use less than 30 registers.
Nhưng những gì chúng ta sẽ phát hiện ra, một số thanh ghi này, trong các chương trình phức tạp hơn, có một số công dụng được chỉ định cho điều gì đó khác. Vì vậy nói chung chúng ta sẽ có thể sử dụng ít hơn 30 thanh ghi.
Registers are scarce, so it's important to be cautious in how we use them.
Thanh ghi rất thiếu, vì vậy điều quan trọng là phải thận trọng trong cách chúng ta sử dụng chúng.
We want our programs to be fairly compact, and we would like to reuse registers to store temporary variables.
Chúng ta muốn các chương trình của mình khá gọn gàng, và chúng ta muốn tái sử dụng các thanh ghi để lưu trữ các biến tạm thời.
So for example in the previous segment, the last example that I've done, you might have seen that we could have used one less register to store temporary values.
Vì vậy chẳng hạn trong phần trước, ví dụ cuối cùng mà tôi đã thực hiện, bạn có thể đã thấy rằng chúng ta có thể đã sử dụng ít hơn một thanh ghi để lưu trữ các giá trị tạm thời.
In general, it is a job of an optimizing compiler to minimize the registry usage.
Nói chung, công việc của một trình biên dịch tối ưu hóa là giảm thiểu việc sử dụng thanh ghi.
When we are done with an optimization, what we'll find out is that for a particular computational routine or for a compute kernel, we have to use certain number of registers.
Khi chúng ta hoàn thành một tối ưu hóa, chúng ta sẽ phát hiện ra rằng đối với một thủ tục tính toán cụ thể hoặc đối với một kernel tính toán, chúng ta phải sử dụng một số lượng thanh ghi nhất định.
That number of registers is called the register footprint for a particular kernel, but we have already seen that we work with larger amounts of data, so we'll have to spill out of the registers and spill over to memory.
Số lượng thanh ghi đó được gọi là dấu chân thanh ghi cho một kernel cụ thể, nhưng chúng ta đã thấy rằng chúng ta làm việc với lượng dữ liệu lớn hơn, vì vậy chúng ta sẽ phải đổ tràn ra khỏi thanh ghi và đổ tràn vào bộ nhớ.
So it's important to understand how does the memory work with the processor. We have seen this picture before.
Vì vậy, điều quan trọng là hiểu cách bộ nhớ hoạt động với bộ xử lý. Chúng ta đã thấy hình ảnh này trước đây.
This is a picture that will so frequently appear in this class that it is well worth remembering — perhaps getting a tattoo or a temporary tattoo until the end of the semester until it gets completely engraved in our memory.
Đây là một hình ảnh sẽ xuất hiện rất thường xuyên trong lớp này nên nó rất đáng để nhớ — có thể xăm hình hoặc xăm hình tạm thời cho đến hết học kỳ cho đến khi nó được khắc sâu hoàn toàn vào bộ nhớ của chúng ta.
On the left-hand side, we have a processor connected to the memory and there is input and the output. We are going to focus for now on the processor and the memory.
Ở phía bên trái, chúng ta có một bộ xử lý được kết nối với bộ nhớ và có đầu vào và đầu ra. Hiện tại chúng ta sẽ tập trung vào bộ xử lý và bộ nhớ.
Processor has its data path with the registers and ALU inside of it, and the memory is there.
Bộ xử lý có đường dẫn dữ liệu với các thanh ghi và ALU bên trong nó, và bộ nhớ ở đó.
Memory stores our program and the data that we work on.
Bộ nhớ lưu trữ chương trình và dữ liệu mà chúng ta làm việc với.
Memory can be viewed as a large one-dimensional array with the address acting as the index to that array, starting at 0.
Bộ nhớ có thể được xem như một mảng một chiều lớn với địa chỉ đóng vai trò là chỉ số cho mảng đó, bắt đầu từ 0.
To access a word in memory, processor must supply an address, and address is right here. And we are going to use a little different color for that.
Để truy cập một từ trong bộ nhớ, bộ xử lý phải cấp một địa chỉ, và địa chỉ ở đây. Và chúng ta sẽ sử dụng một màu khác một chút cho cái đó.
Address is there. So address is calculated by a processor and is used to point to a particular word in the memory.
Địa chỉ ở đó. Vì vậy địa chỉ được tính toán bởi một bộ xử lý và được sử dụng để chỉ đến một từ cụ thể trong bộ nhớ.
The address is typically specified as an offset to a base pointer.
Địa chỉ thường được chỉ định như là một độ lệch từ một con trỏ cơ sở.
So each data array that is laid out in the memory will have its base pointer, and when we would like to access a particular element in that array, we will have to calculate an offset with respect to that base pointer.
Vì vậy mỗi mảng dữ liệu được đặt trong bộ nhớ sẽ có con trỏ cơ sở của nó, và khi chúng ta muốn truy cập một phần tử cụ thể trong mảng đó, chúng ta sẽ phải tính toán một độ lệch với respect to that base pointer.
There are two operations that we would like to do once when we point to a particular word in the memory. We can read or we can write.
Có hai hoạt động mà chúng ta muốn làm một lần khi chúng ta chỉ đến một từ cụ thể trong bộ nhớ. Chúng ta có thể đọc hoặc chúng ta có thể ghi.
It's really important to remember the direction of these two operations. We always load from and store to the memory.
Thực sự rất quan trọng khi nhớ hướng của hai phép toán này. Chúng ta luôn tải từ và lưu trữ vào bộ nhớ.
The world is processor-centric. All the main action happens inside the processor and the memory is just out there. So when we are writing data, we are storing to a memory.
Thế giới là trung tâm xoay quanh bộ xử lý. Tất cả các hoạt động chính diễn ra bên trong bộ xử lý và bộ nhớ chỉ ở ngoài đó. Vì vậy khi chúng ta ghi dữ liệu, chúng ta lưu trữ vào bộ nhớ.
So remember this — store to memory — because it'll help us, really, if we ever get confused with the instructions, what do they do, store to the memory is going to help a lot.
Vì vậy hãy nhớ điều này — lưu trữ vào bộ nhớ — vì nó sẽ giúp chúng ta rất nhiều, nếu khi nào chúng ta bối rối với các lệnh, chúng làm gì, lưu trữ vào bộ nhớ sẽ giúp ích rất nhiều.
Similarly when we are reading, we are reading from the memory into the processor. So load from memory has a direction from the memory into the processor.
Tương tự khi chúng ta đọc, chúng ta đọc từ bộ nhớ vào bộ xử lý. Vì vậy tải từ bộ nhớ có hướng từ bộ nhớ vào bộ xử lý.
Store to has an arrow pointing from the processor to the memory. The other thing that is important to remember is the layout of the data in the memory.
Lệnh store có mũi tên chỉ từ bộ xử lý đến bộ nhớ. Điều quan trọng khác cần nhớ là cách bố trí dữ liệu trong bộ nhớ.
Memory is organized into words, and words in RISC-V that we are studying in this class are 32 bits wide.
Bộ nhớ được tổ chức thành từ, và những từ trong RISC-V mà chúng ta học trong lớp này rộng 32 bit.
They nicely match the register bits that we have in our processor. So the data that is stored in the register nicely fits in each memory word.
Chúng khớp hoàn hảo với các bit thanh ghi mà chúng ta có trong bộ xử lý. Vì vậy dữ liệu được lưu trữ trong thanh ghi thích hợp vừa vặn trong mỗi từ bộ nhớ.
But keep in mind that we frequently like to work with something that is shorter than 32 bits, like characters or values of colors in our images. They are 8 bits.
Nhưng hãy nhớ rằng chúng ta thường thích làm việc với cái gì đó ngắn hơn 32 bit, như các ký tự hoặc các giá trị màu trong hình ảnh của chúng ta. Chúng là 8 bit.
So we have to have an efficient way of putting together 4 bytes into a word such that we don't occupy too much unnecessary space with 0's.
Vì vậy chúng ta phải có một cách hiệu quả để gộp 4 byte thành một từ sao cho chúng ta không chiếm quá nhiều chỗ không cần thiết với các số 0.
Each word of 32 bits will fit 4 bytes, 8 bits each.
Mỗi từ 32 bit sẽ chứa 4 byte, mỗi byte 8 bit.
So when we store bytes in words in memory, which way do they go?
Vì vậy khi chúng ta lưu trữ byte trong từ trong bộ nhớ, chúng sắp xếp theo hướng nào?
In RISC-V architecture, they follow a particular convention that has a funny name called little-endian. What does that mean?
Trong kiến trúc RISC-V, chúng tuân theo một quy ước đặc biệt có cái tên hài hước gọi là little-endian. Điều đó có nghĩa là gì?
That means that the least significant byte in a word — this one over here — gets the smallest address inside that word. So it fits into the bits 0 to 7 in a word.
Điều đó có nghĩa là byte ít quan trọng nhất trong một từ — cái này ở đây — có địa chỉ nhỏ nhất bên trong từ đó. Vì vậy nó lọt vào các bit 0 đến 7 trong một từ.
It could have been done the other way. It would have been called differently, but it follows, in RISC-V, the little-endian convention.
Nó có thể được thực hiện theo cách khác. Nó sẽ được gọi khác đi, nhưng theo quy ước little-endian trong RISC-V.
So essentially, the least significant byte gets the lowest byte address, and then it goes to the next one, and the next one, and so on moving to the left.
Vì vậy về cơ bản, byte có ý nghĩa nhỏ nhất nhận địa chỉ byte thấp nhất, rồi chuyển sang byte tiếp theo, và byte tiếp theo nữa, và cứ tiếp tục sang trái.
And then the next address comes around, and we continue serpentining through the memory array. Words have their addresses. They inherit them, basically, from the bytes.
Rồi địa chỉ tiếp theo xuất hiện, và chúng ta tiếp tục lượn qua mảng bộ nhớ. Các từ có địa chỉ của chúng. Về cơ bản, chúng kế thừa địa chỉ từ các byte.
So this will be a word with another 0. This word would have an address 4, and this word would have an address 8, and this would have an address 12.
Vậy nên đây sẽ là một từ có địa chỉ 0. Từ này sẽ có địa chỉ 4, và từ này sẽ có địa chỉ 8, và từ này sẽ có địa chỉ 12.
So essentially, word address is the same as the address of rightmost byte — its least significant byte.
Vì vậy về cơ bản, địa chỉ của từ cũng giống như địa chỉ của byte ngoài cùng bên phải — byte có ý nghĩa nhỏ nhất của nó.
Hopefully, this was clear. To make it even more clear, let's understand different options how data can be written in the memory.
Hy vọng điều này đã rõ ràng. Để làm cho nó còn rõ ràng hơn, chúng ta hãy hiểu những tùy chọn khác nhau về cách dữ liệu có thể được ghi vào bộ nhớ.
We follow this convention of endianness.
Chúng ta tuân theo quy ước này về endianness.
And endianness, it's a funny word that computer scientists have associated with the novel from the 18th century from 1726 written by an Irish writer Jonathan Swift named Gulliver's Travels.
Và endianness, nó là một từ buồn cười mà các nhà khoa học máy tính đã liên kết với một cuốn tiểu thuyết từ thế kỷ 18 được viết năm 1726 bởi một nhà văn người Ireland Jonathan Swift có tên là Gulliver's Travels.
And in his travels, Gulliver encounters an imaginary society where there is an interesting rift going on, where there is basically a political issue should the eggs be broken — hard-boiled eggs should be broken little side or the big side.
Và trong những cuộc phiêu lưu của mình, Gulliver gặp một xã hội tưởng tượng nơi có một rạn nứt thú vị đang diễn ra, nơi về cơ bản là một vấn đề chính trị trứng có nên bị vỡ — trứng luộc cứng nên bị vỡ phía nhỏ hay phía lớn.
So the people who break the eggs on the little side are called Little-Endians and the people who break the eggs big side first are the Big-Endians.
Vì vậy những người phá trứng ở phía nhỏ được gọi là Little-Endians và những người phá trứng từ phía lớn trước được gọi là Big-Endians.
It's a kind of an interesting dispute. It sounds silly, until we see what kind of these political disputes we have in 21st century.
Đó là một tranh chấp khá thú vị. Điều này nghe có vẻ lạ lùng, cho đến khi chúng ta thấy những cuộc tranh chấp chính trị như thế nào trong thế kỷ 21.
But going back to RISC-V and instruction set architectures, RISC-V hopefully you'll remember forever is a little-endian — follows the little-endian convention.
Nhưng quay lại RISC-V và kiến trúc tập lệnh, RISC-V hy vọng bạn sẽ nhớ mãi mãi là little-endian — tuân theo quy ước little-endian.
So RISC-V always breaks the end from the little side. There are big-endian architectures out there, but let's make sure that we understand the main difference between them.
Vì vậy RISC-V luôn phá từ phía nhỏ. Có những kiến trúc big-endian ngoài kia, nhưng hãy đảm bảo rằng chúng ta hiểu sự khác biệt chính giữa chúng.
Now, endianness governs only the order of how bytes are stored in memory. Bits are always stored the same way.
Bây giờ, endianness chỉ kiểm soát thứ tự của cách các byte được lưu trữ trong bộ nhớ. Các bit luôn được lưu trữ theo cách giống nhau.
The least significant bit goes at the lowest bit position right there.
Bit có ý nghĩa nhỏ nhất đi vào vị trí bit thấp nhất ngay tại đó.
So if you would like to write C2, the more significant nibble 1100 always goes in the upper position in the bit and the less significant nibble 0010 is
Vì vậy, nếu bạn muốn viết C2, nibble có ý nghĩa lớn hơn 1100 luôn đi vào vị trí trên của bit và nibble có ý nghĩa nhỏ hơn 0010 được
positioned in the lower bits.
đặt ở các bit dưới.
Now when we talk about the endianness, this is about storing the bytes.
Bây giờ khi chúng ta nói về endianness, đó là về lưu trữ các byte.
So if you would like to write the number 1,025, which easily decomposes to 1,024 plus 1, we need to write 4 bytes.
Vì vậy, nếu bạn muốn viết số 1.025, số đó dễ dàng phân tích thành 1.024 cộng 1, chúng ta cần viết 4 byte.
The first two bytes are 0's and byte 1 would have 00000100.
Hai byte đầu tiên là 0 và byte 1 sẽ có 00000100.
The byte 0 would store a value of all 0's and 1 at the end. In little-endian convention, byte 0 is placed in the address 0, the least significant address.
Byte 0 sẽ lưu trữ giá trị là tất cả 0 và 1 ở cuối. Theo quy ước little-endian, byte 0 được đặt tại địa chỉ 0, địa chỉ ý nghĩa nhất thấp.
Byte 1 goes to the address 1, byte 2 to address 2, and byte 3 to address 3, and that's it.
Byte 1 đi tới địa chỉ 1, byte 2 tới địa chỉ 2, và byte 3 tới địa chỉ 3, và vậy là xong.
Big-endian convention stores byte 0 in address 3, byte 1 in address 2, byte 2 in address 1, and byte 3 in address 0.
Quy ước big-endian lưu trữ byte 0 tại địa chỉ 3, byte 1 tại địa chỉ 2, byte 2 tại địa chỉ 1, và byte 3 tại địa chỉ 0.
Majority of this world follows little-endian convention.
Đa số thế giới tuân theo quy ước little-endian.
Majority of processors out there, maybe 90%, are little-endian, but some important ones follow the big-endian convention, like IBM's [INAUDIBLE] servers and some of the microcontrollers in a lot of automotive processors.
Đa số bộ xử lý hiện nay, có thể 90%, là little-endian, nhưng một số những cái quan trọng tuân theo quy ước big-endian, như các máy chủ [INAUDIBLE] của IBM và một số những bộ điều khiển vi mô trong nhiều bộ xử lý ô tô.
But endianness is not just the matter of processor architectures. It exists in the real world.
Nhưng endianness không chỉ là vấn đề của các kiến trúc bộ xử lý. Nó tồn tại trong thế giới thực.
For example, my name in majority of the world and in the US is written as Borivoje Nikolic — my first name first, then my last name goes second.
Ví dụ, tên của tôi ở phần lớn thế giới và tại Mỹ được viết là Borivoje Nikolic — tên đầu của tôi trước, rồi tên họ của tôi đứng sau.
If I were to write it in China or in Hungary and some other places — Finland, I think, follows that as well — it would be my surname first, and then followed by my first name.
Nếu tôi viết tên ở Trung Quốc hoặc tại Hungary và một số nơi khác — Phần Lan, tôi nghĩ, cũng tuân theo cách đó — thì sẽ là họ của tôi trước, rồi theo sau là tên đầu của tôi.
Java packages follow kind of in a big-endian convention. Internet addresses, internet names follow little-endian convention.
Các package Java tuân theo kiểu big-endian. Các địa chỉ internet, các tên internet tuân theo kiểu little-endian.
One interesting thing are the dates.
Một điều thú vị là các ngày tháng.
So there is an international standard, ISO standard 8601, that specifies how the dates should be written as first four-digit years followed by two-digit months and two-digit days.
Có một tiêu chuẩn quốc tế, tiêu chuẩn ISO 8601, quy định cách viết các ngày tháng được viết dưới dạng bốn chữ số năm theo sau là hai chữ số tháng và hai chữ số ngày.
And that's a big-endian way of writing dates, and that's really convenient when you try to — that's how I save everything in my computer because it's a very easy way to figure out which pictures come first, how to order the pictures in time.
Và đó là cách viết ngày tháng kiểu big-endian, và nó thực sự tiện lợi khi bạn cố gắng — đó là cách tôi lưu mọi thứ trong máy tính của tôi vì đó là một cách rất dễ để tìm ra những bức ảnh nào đến trước, cách sắp xếp ảnh theo thời gian.
When I write them in European way, it would come as days first, months second, and the year last. A little harder to sort it that way.
Khi tôi viết theo cách Châu Âu, nó sẽ là ngày trước, tháng thứ hai, và năm cuối cùng. Khó sắp xếp hơn theo cách đó.
But the US way is middle-endian or mid-endian. We first write the months, then the days, then the years. Don't ask me why. Hard to understand that.
Nhưng cách của Mỹ là middle-endian hay mid-endian. Chúng ta viết tháng trước, rồi ngày, rồi năm. Đừng hỏi tôi tại sao. Khó hiểu điều đó.
Like some other things, not easy to understand. Anyway, there are other examples of big-endian of people who eat the crust first in a pizza.
Giống như những thứ khác, không dễ hiểu. Dù sao, có những ví dụ khác về big-endian của những người ăn vỏ ngoài trước khi ăn pizza.
The people who eat the thin part of the pizza first would be considered little-endians, and there are many other examples like that. We'll break here.
Những người ăn phần mỏng của pizza trước sẽ được coi là little-endian, và có nhiều ví dụ khác như thế. Chúng ta sẽ dừng ở đây.
We'll come back to talk about actual load and store instructions that work with the memory. See you there.
Chúng ta sẽ quay lại để nói về các chỉ thị load và store thực tế mà hoạt động với bộ nhớ. Gặp bạn lần sau.
Đă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.