September 5th, 2016
kiennt [12:44 AM]Open in archives
Today at 12:44:34 AM
dap lai quan ngai cua @huytd, a se dig deep vao phan Memory Model

kiennt [12:45 AM]Open in archives
Today at 12:45:00 AM
phan nay thuoc phan tim hieu them trong chuong 1 cua sach

huydx [12:45 AM]Open in archives
Today at 12:45:27 AM
:clap:

huydx [12:45 AM]Open in archives
Today at 12:45:34 AM
phan nay to chua viet ki~ dc trong blog

huydx [12:45 AM]Open in archives
Today at 12:45:37 AM
vi no kha dai` va complex

huydx [12:45 AM]Open in archives
Today at 12:45:58 AM
kiennt lam the nao de moi ng hieu dc thi qua sieu

kiennt [6:24 AM]Open in archives
Today at 6:24:56 AM
@huydx cau tung doc về assembly rồi đúng không, cho tớ hỏi trên một CPU với kiến trúc 32 bit, liệu có thể có các lệnh với độ dài bất kỳ không? nếu không độ dài của các lệnh có phải follow theo 1 rule nào không? tớ đang nghĩ là độ dài của 1 lệnh sẽ phải là bội của 8

kiennt [6:25 AM]Open in archives
Today at 6:25:06 AM
hoặc là phải bằng đúng 32 bit

huydx [6:25 AM]Open in archives
Today at 6:25:56 AM
@kiennt ý cậu "độ dài của lệnh" là sao?

kiennt [6:27 AM]Open in archives
Today at 6:27:06 AM
để tớ giải thích như này xem có đúng không nhé
1 lệnh của assembly sẽ có dạng
<COMMAND_CODE> <VAR1> <VAR2>

kiennt [6:27 AM]Open in archives
Today at 6:27:30 AM
cái <COMMAND_CODE> là một lệnh của CPU

kiennt [6:27 AM]Open in archives
Today at 6:27:50 AM
<VAR> sẽ là các biến được lưu trong các thanh ghi, hoặc là trên RAM

kiennt [6:28 AM]Open in archives
Today at 6:28:28 AM
câu hỏi của tớ là có rule gì đê control size của  <VAR> không?

anhduy [6:29 AM]Open in archives
Today at 6:29:12 AM
:thinking_face:

kiennt [6:29 AM]Open in archives
Today at 6:29:35 AM
t đang suy nghĩ là size của <VAR> sẽ phải bằng size của word of computer, ví dụ máy CPU 32 bit, thì size = 32 bit

huydx [6:30 AM]Open in archives
Today at 6:30:03 AM
có mấy điểm

huydx [6:30 AM]Open in archives
Today at 6:30:16 AM
đầu tiên VAR không phải là tren RAM, mà chỉ là trên register thôi

huydx [6:30 AM]Open in archives
Today at 6:30:34 AM
nó là stack variable của CPU, chỉ có quanh đi quẩn lại mấy cái thôi, tuỳ vảo architecture

huydx [6:30 AM]Open in archives
Today at 6:30:52 AM
thứ 2 là về size thì không liên quan đến cấu trúc CPU đâu

huydx [6:31 AM]Open in archives
Today at 6:31:03 AM
giả sử cậu có 1 biến 128bit

huydx [6:31 AM]Open in archives
Today at 6:31:12 AM
mà cấu trúc CPU lại chỉ có 8bit

huydx [6:31 AM]Open in archives
Today at 6:31:18 AM
CPU 8bit ở đây có nghĩa là

huydx [6:31 AM]Open in archives
Today at 6:31:27 AM
cái đường bus từ CPU đến RAM

huydx [6:31 AM]Open in archives
Today at 6:31:30 AM
nó có 8 cái dây

huydx [6:31 AM]Open in archives
Today at 6:31:45 AM
tức là tại mỗi 1 thời điểm cậu chỉ lấy đc 8 bit data từ RAM về CPU thôi

huydx [6:31 AM]Open in archives
Today at 6:31:56 AM
nên nếu var có độ dài > CPU arch

huydx [6:32 AM]Open in archives
Today at 6:32:04 AM
thì nó phải đi đi lại lại nhiều lần

huydx [6:32 AM]Open in archives
Today at 6:32:07 AM
--> costly

kiennt [6:33 AM]Open in archives
Today at 6:33:03 AM
đầu tiên VAR không phải là tren RAM, mà chỉ là trên register thôi
=> tớ nghĩ phải có một cái lệnh để load data từ địa chỉ trên RAM chứ?

kiennt [6:35 AM]Open in archives
Today at 6:35:10 AM
với cả về vụ kiến trúc CPU, tớ nghĩ ngoài việc kiến trúc CPU = số day nối trên bus từ CPU tới RAM, nó còn là size của thanh ghi mà CPU có thể thực hiện phép tính toán trên đó. cụ thể phép tính toán ở đây sẽ là các phép toán với bit.
VD: CPU 32 bit, tức là với 1 lệnh CPU có thể thực hiện được phép cộng 2 số 32 bit

kiennt [6:36 AM]Open in archives
Today at 6:36:40 AM
câu hỏi của tớ là về hương băn khoăn ngược lại: nếu CPU là 32 bít, thì CPU có hộ trợ việc thực hiện phép với với số 1 bit hay không?

kiennt [6:38 AM]Open in archives
Today at 6:38:12 AM
thật ra câu hỏi đúng hơn sẽ phải là, liệu CPU có hỗ trợ việc load N bit từ RAM vào thanh ghi của CPU hay không thì đúng hơn (với N < CPU arch)

huydx [6:39 AM]Open in archives
Today at 6:39:33 AM
>tớ nghĩ phải có một cái lệnh để load data từ địa chỉ trên RAM chứ?
cái VAR trong ví dụ của cậu là VAR nhìn từ phía assembly, tức là nó được hiểu là trên register rồi, việc chuyển từ 1 var tren memory, ví dụ long x, lên thành một biến trên assembly thực hiện bởi compiler

huydx [6:39 AM]Open in archives
Today at 6:39:51 AM
nếu CPU là 32bit, có thể thực hiện với số 1 bit không

huydx [6:40 AM]Open in archives
Today at 6:40:04 AM
tớ nghĩ là khi đó mình phải biểu diễn cái 1bit đó đưới dạng 32 bit

huydx [6:40 AM]Open in archives
Today at 6:40:20 AM
còn hỗ trợ load "đúng" N bit, với N < arch

huydx [6:40 AM]Open in archives
Today at 6:40:24 AM
thì tớ nghĩ là không

huydx [6:40 AM]Open in archives
Today at 6:40:45 AM
có một số register nó có cho load 1/2 N

huydx [6:41 AM]Open in archives
Today at 6:41:00 AM
ví dụ register là 64bit, nó cho load 32bit trên và 32 bit dưới riêng biệt

unrealhoang [6:41 AM]Open in archives
Today at 6:41:14 AM
So #hardcore

kiennt [6:41 AM]Open in archives
Today at 6:41:33 AM
tớ đang có 2 giả thiết:
1 là phải đùng là arch CPU
2 là phải là bội của 8 (tức là bytes)

kiennt [6:42 AM]Open in archives
Today at 6:42:22 AM
> cái VAR trong ví dụ của cậu là VAR nhìn từ phía assembly, tức là nó được hiểu là trên register rồi, việc chuyển từ 1 var tren memory, ví dụ long x, lên thành một biến trên assembly thực hiện bởi compiler
cái này thì compiler cũng phải có lệnh chứ
VD: CPU hỗ trợ lệnh <LOAD> <dia-chi-trên-RAM> <Register>

kiennt [6:42 AM]Open in archives
Today at 6:42:25 AM
kiểu vậy?

unrealhoang [6:42 AM]Open in archives
Today at 6:42:27 AM
Size của register dài bằng cpu arch phải không nhỉ (edited)

kiennt [6:43 AM]Open in archives
Today at 6:43:45 AM
@unrealhoang  nice question :slightly_smiling_face:, nếu như CPU 32 bit, mà có 1 register 8 bit, thì chắc là sẽ hỗ trợ việc load 8 bit

huydx [6:44 AM]Open in archives
Today at 6:44:29 AM
vi du voi x86

huydx [6:44 AM]Open in archives
Today at 6:44:34 AM
thi moi reg deu co size la 32bit

unrealhoang [6:44 AM]Open in archives
Today at 6:44:36 AM
Yep, suy ra thì nó phụ thuộc vào register size hơn là cpu arch

unrealhoang [6:44 AM]Open in archives
Today at 6:44:58 AM
Cpu hỗ trợ cả x86 lẫn 64 thì sao nhỉ

kiennt [6:46 AM]Open in archives
Today at 6:46:05 AM
có cả loại CPU đó à?

huydx [6:46 AM]Open in archives
Today at 6:46:21 AM
đợi tí tắm cái rồi vào bàn tiếp

unrealhoang [6:46 AM]Open in archives
Today at 6:46:50 AM
http://superuser.com/questions/9083/can-a-32-bit-os-run-in-a-64-bit-processor

kiennt [6:47 AM]Open in archives
Today at 6:47:48 AM
> A 64 bit processor can run both 32 and 64 OS (at least an x64 can). A 32 bit processor can run only 32 natively.

anhduy [6:48 AM]Open in archives
Today at 6:48:09 AM
như huydx nói x64 có thể biểu diễn 32 được

anhduy [6:48 AM]Open in archives
Today at 6:48:13 AM
nên chạy được chắc

kiennt [6:48 AM]Open in archives
Today at 6:48:27 AM
@huydx
mov — Move (Opcodes: 88, 89, 8A, 8B, 8C, 8E, ...)
The mov instruction copies the data item referred to by its second operand (i.e. register contents, memory contents, or a constant value) into the location referred to by its first operand (i.e. a register or memory). While register-to-register moves are possible, direct memory-to-memory moves are not. In cases where memory transfers are desired, the source memory contents must first be loaded into a register, then can be stored to the destination memory address.
có cái lênh ⁠⁠⁠⁠mov⁠⁠⁠⁠ này để move data từ memory vào register này

kiennt [6:49 AM]Open in archives
Today at 6:49:14 AM
biểu diễn thì chắc chắn là được rồi @anhduy , giống như là CPU 32 bit, nhưng em vẫn thực hiện được phép cộng byte đó

anhduy [6:51 AM]Open in archives
Today at 6:51:36 AM
như vậy tại vì register max length 32 bit, từ đó cái VAR chỉ max được tới 32bit, nên đó là lí do x86 chỉ dùng được 4GB ram

anhduy [6:51 AM]Open in archives
Today at 6:51:39 AM
:thinking_face:

unrealhoang [6:51 AM]Open in archives
Today at 6:51:49 AM
Đúng rồi

unrealhoang [6:52 AM]Open in archives
Today at 6:52:13 AM
Có câu trả lời phía dưới khá hay đó

unrealhoang [6:52 AM]Open in archives
Today at 6:52:19 AM
3-4th gì đó

unrealhoang [6:52 AM]Open in archives
Today at 6:52:29 AM
Kể về lịch sử 64bit

kiennt [6:53 AM]Open in archives
Today at 6:53:08 AM
```Machine instructions generally fall into three categories: data movement, arithmetic/logic, and control-flow. In this section, we will look at important examples of x86 instructions from each category. This section should not be considered an exhaustive list of x86 instructions, but rather a useful subset. For a complete list, see Intel's instruction set reference.
We use the following notation:
<reg32>        Any 32-bit register (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP)
<reg16>    Any 16-bit register (AX, BX, CX, or DX)
<reg8>    Any 8-bit register (AH, BH, CH, DH, AL, BL, CL, or DL)
<reg>    Any register
<mem>    A memory address (e.g., [eax], [var + 4], or dword ptr [eax+ebx])
<con32>    Any 32-bit constant
<con16>    Any 16-bit constant
<con8>    Any 8-bit constant
<con>    Any 8-, 16-, or 32-bit constant
```

unrealhoang [6:54 AM]Open in archives
Today at 6:54:40 AM
Có code register ko nhỉ

kiennt [6:55 AM]Open in archives
Today at 6:55:23 AM
```32 bits :  EAX EBX ECX EDX
16 bits : AX BX CX DX
8 bits : AH AL BH BL CH CL DH DL
```

unrealhoang [6:56 AM]Open in archives
Today at 6:56:14 AM
Cpu đọc lệnh từ đâu nhỉ

kiennt [6:56 AM]Open in archives
Today at 6:56:54 AM
anh nhờ là có 1 thanh ghi, hình như là PC hay là PCX gì đó (Program Control)

huydx [6:57 AM]Open in archives
Today at 6:57:00 AM
```Some examples of mov instructions using address computations are:
mov eax, [ebx]    ; Move the 4 bytes in memory at the address contained in EBX into EAX
mov [var], ebx    ; Move the contents of EBX into the 4 bytes at memory address var. (Note, var is a 32-bit constant).
mov eax, [esi-4]    ; Move 4 bytes at memory address ESI + (-4) into EAX
mov [esi+eax], cl    ; Move the contents of CL into the byte at address ESI+EAX
mov edx, [esi+4*ebx]        ; Move the 4 bytes of data at address ESI+4*EBX into EDX
```

kiennt [6:57 AM]Open in archives
Today at 6:57:10 AM
Programer Counter

huydx [6:57 AM]Open in archives
Today at 6:57:15 AM
ví dụ như mov này chỉ move 32 bit một thôi

huydx [6:57 AM]Open in archives
Today at 6:57:22 AM
hoặc adr cũng thế

huydx [6:57 AM]Open in archives
Today at 6:57:34 AM
nên câu hỏi của kiennt về move đc < N bit không

huydx [6:57 AM]Open in archives
Today at 6:57:38 AM
thì tớ nghĩ là ko

huydx [6:57 AM]Open in archives
Today at 6:57:50 AM
cũng k liên quan đến bội của 8 luôn

kiennt [6:58 AM]Open in archives
Today at 6:58:21 AM
có những thanh ghi 8 bits, thì phải support move 8 bits chứ @huydx?

unrealhoang [6:58 AM]Open in archives
Today at 6:58:49 AM
Program counter em nghỉ chỉ là pointer của curent execution thôi @kiennt

kiennt [6:59 AM]Open in archives
Today at 6:59:23 AM
PC sẽ trỏ tới 1 lệnh đó @unrealhoang

unrealhoang [6:59 AM]Open in archives
Today at 6:59:39 AM
Phải có 1 register nạp lệnh đó cho cpu đọc chứ

kiennt [6:59 AM]Open in archives
Today at 6:59:42 AM
thật ra là trở tới address của lệnh tiếp theo

huydx [6:59 AM]Open in archives
Today at 6:59:50 AM
vậy vấn đề là

huydx [6:59 AM]Open in archives
Today at 6:59:52 AM
8bit register

huydx [6:59 AM]Open in archives
Today at 6:59:57 AM
có trên 64bit CPU k

huydx [7:00 AM]Open in archives
Today at 7:00:01 AM
đúng k nhỉ

huydx [7:00 AM]Open in archives
Today at 7:00:11 AM
giả sử với 64bit linux đi

unrealhoang [7:00 AM]Open in archives
Today at 7:00:21 AM
Address trên RAM à? @kiennt

huydx [7:00 AM]Open in archives
Today at 7:00:23 AM
tất cả đều là 64bits register

kiennt [7:00 AM]Open in archives
Today at 7:00:36 AM
@huydx
```Modern (i.e 386 and beyond) x86 processors have eight 32-bit general purpose registers, as depicted in Figure 1. The register names are mostly historical. For example, EAX used to be called the accumulator since it was used by a number of arithmetic operations, and ECX was known as the counter since it was used to hold a loop index. Whereas most of the registers have lost their special purposes in the modern instruction set, by convention, two are reserved for special purposes — the stack pointer (ESP) and the base pointer (EBP).

For the EAX, EBX, ECX, and EDX registers, subsections may be used. For example, the least significant 2 bytes of EAX can be treated as a 16-bit register called AX. The least significant byte of AX can be used as a single 8-bit register called AL, while the most significant byte of AX can be used as a single 8-bit register called AH. These names refer to the same physical register. When a two-byte quantity is placed into DX, the update affects the value of DH, DL, and EDX. These sub-registers are mainly hold-overs from older, 16-bit versions of the instruction set. However, they are sometimes convenient when dealing with data that are smaller than 32-bits (e.g. 1-byte ASCII characters).
```

kiennt [7:01 AM]Open in archives
Today at 7:01:17 AM
@unrealhoang đúng rồi em, vì 1 program thực chất sẽ được load vào RAM, và có 1 PC nhảy từng cái 1 để instruct CPU thực hiện lệnh

huydx [7:02 AM]Open in archives
Today at 7:02:33 AM
cái ở trên kiennt nói là nó lấy 1 phần của 1 cái general và coi là 1 cái register nhỏ  hơn

huydx [7:02 AM]Open in archives
Today at 7:02:38 AM
tớ không chắc là giả sử

huydx [7:02 AM]Open in archives
Today at 7:02:46 AM
ADD <8bit reg> <32bit reg>

huydx [7:02 AM]Open in archives
Today at 7:02:50 AM
thì nó có chạy ko

kiennt [7:02 AM]Open in archives
Today at 7:02:57 AM
à, cái này thì tớ nghĩ là không

kiennt [7:04 AM]Open in archives
Today at 7:04:30 AM
thực tế vì những thanh ghi 8bit, 16bit kia về mặt vật lý vẫn là những thanh ghi 32 bit

huydx [7:04 AM]Open in archives
Today at 7:04:47 AM
đúng rồi

huydx [7:04 AM]Open in archives
Today at 7:04:51 AM
tớ vừa xem lại x86

huydx [7:04 AM]Open in archives
Today at 7:04:55 AM
lệnh MUL

huydx [7:04 AM]Open in archives
Today at 7:04:57 AM
multiply

huydx [7:05 AM]Open in archives
Today at 7:05:07 AM
nó hỗ trợ * 2 số cùng bit

huydx [7:05 AM]Open in archives
Today at 7:05:11 AM
16 * 16

huydx [7:05 AM]Open in archives
Today at 7:05:13 AM
8 * 8

huydx [7:05 AM]Open in archives
Today at 7:05:15 AM
32 * 32

huydx [7:05 AM]Open in archives
Today at 7:05:23 AM
chứ không hỗ trợ cross multiply

kiennt [7:08 AM]Open in archives
Today at 7:08:21 AM
ok, vậy để tớ tống kết lại nhé:
1. các thanh ghi của CPU có độ dai là 8, 16, 32, 64 bit
2. CPU hộ trợ việc load dự liệu từ RAM lên thanh ghi
3. CPU cung cấp các phép toán, tham số của các phép toán này là các thanh ghi, và chỉ các thanh ghi phải có cùng size

huydx [7:08 AM]Open in archives
Today at 7:08:54 AM
4. con số cụ thể tuỳ thuộc vào loại CPU

kiennt [7:09 AM]Open in archives
Today at 7:09:48 AM
giờ có một câu hỏi nhé, những thứ ở trên liên quan gì tới Memory Visiblity của Java (và các ngôn ngữ khác nữa)

huydx [7:10 AM]Open in archives
Today at 7:10:41 AM
hmm interesting

huydx [7:11 AM]Open in archives
Today at 7:11:33 AM
tớ ko diễn đạt cụ thể được, thực tế cũng chưa hiểu đủ rõ để diễn đạt, chỉ biết là memory visiblity phụ thuộc vào việc cpu có nhìn thấy data không

huydx [7:11 AM]Open in archives
Today at 7:11:56 AM
mà cái đó phụ thuộc vào location của data: cache, L1/L2, memory hay là disk

kiennt [7:16 AM]Open in archives
Today at 7:16:48 AM
Xét trường hợp một nhé: dữ liệu lớn hơn thanh ghi
```long a = 0; 

Thread thread1 = new Thread() {
   a = 100;
}

Thread thread2 = new Thread() {
  System.out.println(a + 1);
}
```
(edited)

kiennt [7:17 AM]Open in archives
Today at 7:17:11 AM
giả sử chạy đoạn code trên trên máy 32 bit, điều gì sẽ xảy ra?

kiennt [7:17 AM]Open in archives
Today at 7:17:24 AM
⁠⁠⁠⁠a⁠⁠⁠⁠ có thể nhận những giá trị nào?

huydx [7:19 AM]Open in archives
Today at 7:19:13 AM
:thinking_face:

huydx [7:19 AM]Open in archives
Today at 7:19:56 AM
a ko được sync

huydx [7:19 AM]Open in archives
Today at 7:19:59 AM
cũng ko volatile

huydx [7:20 AM]Open in archives
Today at 7:20:19 AM
vậy thì chỉ phụ thuộc vào thằng nào chạy trc thôi

huydx [7:20 AM]Open in archives
Today at 7:20:25 AM
cái này la undetermine

kiennt [7:20 AM]Open in archives
Today at 7:20:35 AM
những giá trị nào mà :smile:

huydx [7:20 AM]Open in archives
Today at 7:20:35 AM
thế nên có thể là 1 hoặc 101

kiennt [7:25 AM]Open in archives
Today at 7:25:16 AM
giờ nếu thay ⁠⁠⁠⁠100⁠⁠⁠⁠ bằng một số 64 bít thì sao?

kiennt [7:26 AM]Open in archives
Today at 7:26:24 AM
ví dụ ⁠⁠⁠⁠a = 2^64 - 1⁠⁠⁠⁠

kiennt [7:27 AM]Open in archives
Today at 7:27:21 AM
tớ đang nghĩ tới phép gán giữa một biến 64 bit và 1 giá trị 64 bit khác trên máy 32 bit sẽ được biểu diễn thành 2 lệnh ⁠⁠⁠⁠mov⁠⁠⁠⁠

kiennt [7:27 AM]Open in archives
Today at 7:27:58 AM
và trong khi thread1 đang chay được lệnh ⁠⁠⁠⁠mov⁠⁠⁠⁠ số 1, thì thread2 chạy luôn, và chạy trước xong trước khi thread1 chạy lệnh ⁠⁠⁠⁠mov⁠⁠⁠⁠ còn lại

huydx [7:28 AM]Open in archives
Today at 7:28:56 AM
uh tớ cũng đoán là kiennt đang ví dụ case này

huydx [7:29 AM]Open in archives
Today at 7:29:05 AM
về vụ này thì hình như trong java memory visibility

huydx [7:29 AM]Open in archives
Today at 7:29:12 AM
có một rule về atomicity

huydx [7:29 AM]Open in archives
Today at 7:29:29 AM
tức là state của 1 object phải đảm bảo là 1 atomic operation

huydx [7:29 AM]Open in archives
Today at 7:29:55 AM
cái hành động mov mà gồm 2 hành động mov nhỏ đó có thể coi là thay đổi state

huydx [7:30 AM]Open in archives
Today at 7:30:13 AM
nên sẽ được đảm bảo là 1 thằng xong thì thằng khác mới nhìn đc

kiennt [7:33 AM]Open in archives
Today at 7:33:56 AM
tớ nghĩ là vụ 64 bit for long and double là chưa support huy ơi

huydx [7:34 AM]Open in archives
Today at 7:34:16 AM
aaa

huydx [7:34 AM]Open in archives
Today at 7:34:22 AM
interesting

huydx [7:34 AM]Open in archives
Today at 7:34:29 AM
hoá ra nó chừa lại đúng case này

huydx [7:34 AM]Open in archives
Today at 7:34:30 AM
lol

kiennt [7:35 AM]Open in archives
Today at 7:35:02 AM
giờ tiếp nhé, xét trường hợp còn lại, size của dữ liệu nhỏ hơn thanh ghi

kiennt [7:40 AM]Open in archives
Today at 7:40:25 AM
tớ đưa ra ví dụ chung nhé:
Giả sử CPU hỗ trợ việc load dữ liệu từ RAM với size nhỏ nhất là N
Giờ programmer define 1 loaị dữ liệu với size < N, gọi là T đi.
```T[] a = new T[10];
a[0] = 0;
a[1] = 0;

Thread t1 = new Thread() {
   a[0] = 10;
}

Thread t2 = new Thread() {
  System.out.println(a[1])
}
```

kiennt [7:40 AM]Open in archives
Today at 7:40:41 AM
đoạn code sau có thể ra những kết quả như nào?

huydx [7:45 AM]Open in archives
Today at 7:45:08 AM
hmm

huydx [7:45 AM]Open in archives
Today at 7:45:13 AM
để nghĩ xem nào

huydx [7:50 AM]Open in archives
Today at 7:50:24 AM
không nghĩ ra :smile:

huydx [7:50 AM]Open in archives
Today at 7:50:38 AM
@kiennt cho lời giải đi

kiennt [7:52 AM]Open in archives
Today at 7:52:22 AM
tớ gợi ý nhé, để đơn giản, giả sử size của T là 6 bit,  giờ translate cái ⁠⁠⁠⁠a[0] = 10;⁠⁠⁠⁠ thành CPU instruction, với chú ý là các thanh ghi phải có size là 8, 16, 32, 64

huydx [7:58 AM]Open in archives
Today at 7:58:22 AM
có dùng dữ liệu là T[10] là continous memory k kiennt

kiennt [8:00 AM]Open in archives
Today at 8:00:05 AM
hehe, do dung la point cua t do

kiennt [8:00 AM]Open in archives
Today at 8:00:39 AM
vi la array nen thuc chat T 10 la 1 chuoi byte lien nhau

anhduy [8:01 AM]Open in archives
Today at 8:01:08 AM
:thinking_face:

kiennt [8:01 AM]Open in archives
Today at 8:01:42 AM
nen khi assign kieu a[0] thuc chat la co can thiep ca vao 2 bit cua a[1]

anhduy [8:02 AM]Open in archives
Today at 8:02:21 AM
cho em ý kiến tẹo, em tưởng size của data type phải là 1 2 4 8 ..

anhduy [8:02 AM]Open in archives
Today at 8:02:27 AM
đâu có 6 giữa chừng được ạ

kiennt [8:03 AM]Open in archives
Today at 8:03:39 AM
dung la vi du nay hoi "vo ly" voi Java, that ra no chi dung voi 1 so loai du lieu cua Java thoi, Vi du nhu Bitset

kiennt [8:04 AM]Open in archives
Today at 8:04:14 AM
nen trong document cua BitSet cung noi toi no la k thread safe, phai dung vilotile

kiennt [8:04 AM]Open in archives
Today at 8:04:26 AM
con voi mot so ngon ngu khac, vi du C

kiennt [8:04 AM]Open in archives
Today at 8:04:38 AM
thi co the tao ra kieu du lieu voi 6bit ma

kiennt [8:04 AM]Open in archives
Today at 8:04:54 AM
vi C cho phep set size cua bien

anhduy [8:05 AM]Open in archives
Today at 8:05:18 AM
rồi tiếp đi anh

anhduy [8:05 AM]Open in archives
Today at 8:05:33 AM
em lui vào hậu trường tiếp

huydx [8:05 AM]Open in archives
Today at 8:05:35 AM
đúng là ví dụ này hơi khó hình dung với java

huydx [8:05 AM]Open in archives
Today at 8:05:57 AM
:thinking_face:

huydx [8:06 AM]Open in archives
Today at 8:06:13 AM
cái này có liên quan đến memory visibility k kiennt

kiennt [8:06 AM]Open in archives
Today at 8:06:26 AM
uh, cai nay la Word tearing

kiennt [8:06 AM]Open in archives
Today at 8:06:41 AM
t dang tiep can theo huong la

kiennt [8:06 AM]Open in archives
Today at 8:06:43 AM
dua ra mot so kho khan cua compiler

kiennt [8:07 AM]Open in archives
Today at 8:07:52 AM
@huydx co lien quan chu, no noi len la voi nhung array cua kieu T

kiennt [8:08 AM]Open in archives
Today at 8:08:14 AM
thi phai dung synchronize do

huydx [8:08 AM]Open in archives
Today at 8:08:23 AM
day nhi

kiennt [8:08 AM]Open in archives
Today at 8:08:52 AM
dung rui

kiennt [8:08 AM]Open in archives
Today at 8:08:58 AM
cai nay co the dung voi ca cac ngon ngu khac nua

kiennt [8:09 AM]Open in archives
Today at 8:09:10 AM
vi du Go hoac Erlang

kiennt [8:09 AM]Open in archives
Today at 8:09:24 AM
(chua doc spec nen chua biet)

huydx [8:09 AM]Open in archives
Today at 8:09:57 AM
de mai doc ki~ ve vd ve word tearing cua java

huydx [8:10 AM]Open in archives
Today at 8:10:01 AM
vu nay hay day, ko de y ki

huydx [8:10 AM]Open in archives
Today at 8:10:04 AM
tks @kiennt

huydx [8:10 AM]Open in archives
Today at 8:10:27 AM
golang cung co model

huydx [8:10 AM]Open in archives
Today at 8:10:32 AM
chua doc ki model cua golang

kiennt [8:12 AM]Open in archives
Today at 8:12:55 AM
con mot van de nua cua Java Memory Visiblity do la viec reorder cac statements cua Compiler, moi nguoi co biet vi sao Compiler lai can reorder k? hay la reorder se dem lai kq gi?

huydx [8:13 AM]Open in archives
Today at 8:13:17 AM
reorder thi thong thuong la de optimize

kiennt [8:14 AM]Open in archives
Today at 8:14:28 AM
@huydx co the cho mot vi du, reorder se optimize duoc k?

huydx [8:14 AM]Open in archives
Today at 8:14:40 AM
vi du don gian nhat la cau. assign thua`

huydx [8:14 AM]Open in archives
Today at 8:14:43 AM
a = b

huydx [8:14 AM]Open in archives
Today at 8:14:45 AM
b = c

huydx [8:14 AM]Open in archives
Today at 8:14:48 AM
xong lai k dung` b

huydx [8:14 AM]Open in archives
Today at 8:14:52 AM
thi no viet lai thanh a = c

huydx [8:15 AM]Open in archives
Today at 8:15:16 AM
no co 1 so thuat toan noi tieng day

huydx [8:15 AM]Open in archives
Today at 8:15:25 AM
cai to hay nghe nhat la peephole

huydx [8:15 AM]Open in archives
Today at 8:15:47 AM
tuc la kieu cau nhin xung quanh 1 cai instruction, xem co gop. lai dc k

kiennt [8:16 AM]Open in archives
Today at 8:16:17 AM
vi du tren giong rewrite hon la reorder a

huydx [8:17 AM]Open in archives
Today at 8:17:00 AM
vi du de hon nhe

huydx [8:17 AM]Open in archives
Today at 8:17:12 AM
do { a = 1; b = b + a }

huydx [8:17 AM]Open in archives
Today at 8:17:24 AM
thi co the reorder lai la a = 1; do { b = b + a}

kiennt [8:17 AM]Open in archives
Today at 8:17:43 AM
x = a + 1
b = 10
c = a + 3

kiennt [8:18 AM]Open in archives
Today at 8:18:19 AM
sao tach a ra ngoai do lai optimize hon nhi?

huydx [8:18 AM]Open in archives
Today at 8:18:41 AM
no save assignment cycle ma

huydx [8:18 AM]Open in archives
Today at 8:18:44 AM
a = 1 do'

kiennt [8:20 AM]Open in archives
Today at 8:20:09 AM
a`, tuc la
a = 1; do { b = b + 1}
phai khong huy?

kiennt [8:20 AM]Open in archives
Today at 8:20:15 AM
cai nay van giong rewrite hon

kiennt [8:21 AM]Open in archives
Today at 8:21:11 AM
cai vi du cua t co ve hop ly hon
```x = a + 1 
b = 10
c = a + 3
```

se rewrite thanh
```x = a + 1
c = a + 3
b = 10
```

kiennt [8:21 AM]Open in archives
Today at 8:21:55 AM
li do la vi sau khi ⁠⁠⁠⁠x = a + 1⁠⁠⁠⁠, CPU thuc hien 1 phep cong 2 so, va 1 trong 2 so do la so ⁠⁠⁠⁠a⁠⁠⁠⁠, nen neu thuc hien tiep phep dong voi ⁠⁠⁠⁠a⁠⁠⁠⁠, thi do mat cong load a tu memory vao register

huydx [8:22 AM]Open in archives
Today at 8:22:14 AM
uh vi du cua cau hop ly hon

huydx [8:22 AM]Open in archives
Today at 8:22:18 AM
reorder ve gan` nhau

huydx [8:22 AM]Open in archives
Today at 8:22:24 AM
de load 1 lan

kiennt [8:22 AM]Open in archives
Today at 8:22:43 AM
cai rewrite giong nhu la remove dead code vay

kiennt [8:23 AM]Open in archives
Today at 8:23:11 AM
t nghi compiler se lam viec optimize do la layer cao hon memory model

kiennt [8:24 AM]Open in archives
Today at 8:24:37 AM
con 1 phan nua la cac luat ve reorder, thi t doc nhung ma van chua hieu :smile:

huydx [8:25 AM]Open in archives
Today at 8:25:23 AM
to nghi optimize nao thi cung o cung 1 phase day'

huydx [8:25 AM]Open in archives
Today at 8:25:33 AM
cai rewrite loop to noi o tren

huydx [8:25 AM]Open in archives
Today at 8:25:39 AM
to nghi no cung phai hieu context cua chuong trinh

huydx [8:25 AM]Open in archives
Today at 8:25:52 AM
tuc la vua phai y thuc ve memory model

huydx [8:25 AM]Open in archives
Today at 8:25:56 AM
vua phai y thuc ve context

huydx [8:26 AM]Open in archives
Today at 8:26:03 AM
thi moi optimize dc viec do

huydx [8:26 AM]Open in archives
Today at 8:26:11 AM
chi co phase sau khi da co instruction

huydx [8:26 AM]Open in archives
Today at 8:26:22 AM
ah ma ke ca phase do thi van can memory model

huydx [8:26 AM]Open in archives
Today at 8:26:32 AM
vi co 1 rule la cac instruction trong cung 1 thread thi phai giu thu tu

huydx [8:27 AM]Open in archives
Today at 8:27:11 AM
noi hoi kho hieu ti

huydx [8:27 AM]Open in archives
Today at 8:27:19 AM
tom tat: compiler du o phase nao cung phai care den memory model

kiennt [8:30 AM]Open in archives
Today at 8:30:00 AM
cai nay chua doc them ve cach implement thi chua biet, nhung t nghi la viec remove deadcode, co the chi can dung CFG (control flow graph) la co the lam duoc

kiennt [8:30 AM]Open in archives
Today at 8:30:23 AM
khong phai tat ca cac case nhung ma mot so case se lam duoc

huydx [8:30 AM]Open in archives
Today at 8:30:41 AM
uh dung roi

huydx [8:30 AM]Open in archives
Today at 8:30:48 AM
ma day la van de khac roi

huydx [8:30 AM]Open in archives
Today at 8:30:56 AM
thoi ngu da

huydx [8:30 AM]Open in archives
Today at 8:30:58 AM
vo goi

kiennt [8:31 AM]Open in archives
Today at 8:31:10 AM
(y)

kiennt [8:31 AM]Open in archives
Today at 8:31:30 AM
uh, t doc not cai reorder

kiennt [8:31 AM]Open in archives
Today at 8:31:38 AM
mai se report voi a e

huydx [8:32 AM]Open in archives
Today at 8:32:32 AM
:parrot: