Nasıl Duff'in cihaz işe yarıyor mu?
article on Wikipedia on the Duff's device okudum ve anlamadım. Gerçekten merak ediyorum, ama açıklama orada birkaç kez okudum ve hala Most Wanted cihazın nasıl çalıştığını anlamıyor.
Daha ayrıntılı bir açıklama ne olurdu?
CEVAP
İyi bir açıklama başka bir yerde vardır, ama ben bir deneyeyim. (Bu bir beyaz tahta üzerinde çok daha kolay!) İşte bazı notlar ile Wikipedia örnek.
Hadi 20 bayt taklit ediyorsun ki. İlk geçiş için program kontrol akış:
int count; // Set to 20
{
int n = (count 7) / 8; // n is now 3. (The "while" is going
// to be run three times.)
switch (count % 8) { // The remainder is 4 (20 modulo 8) so
// jump to the case 4
case 0: // [skipped]
do { // [skipped]
*to = *from ; // [skipped]
case 7: *to = *from ; // [skipped]
case 6: *to = *from ; // [skipped]
case 5: *to = *from ; // [skipped]
case 4: *to = *from ; // Start here. Copy 1 byte (total 1)
case 3: *to = *from ; // Copy 1 byte (total 2)
case 2: *to = *from ; // Copy 1 byte (total 3)
case 1: *to = *from ; // Copy 1 byte (total 4)
} while (--n > 0); // N = 3 Reduce N by 1, then jump up
// to the "do" if it's still
} // greater than 0 (and it is)
}
Şimdi, ikinci geçiş başlatmak, sadece belirtilen kodu çalıştırın:
int count; //
{
int n = (count 7) / 8; //
//
switch (count % 8) { //
//
case 0: //
do { // The while jumps to here.
*to = *from ; // Copy 1 byte (total 5)
case 7: *to = *from ; // Copy 1 byte (total 6)
case 6: *to = *from ; // Copy 1 byte (total 7)
case 5: *to = *from ; // Copy 1 byte (total 8)
case 4: *to = *from ; // Copy 1 byte (total 9)
case 3: *to = *from ; // Copy 1 byte (total 10)
case 2: *to = *from ; // Copy 1 byte (total 11)
case 1: *to = *from ; // Copy 1 byte (total 12)
} while (--n > 0); // N = 2 Reduce N by 1, then jump up
// to the "do" if it's still
} // greater than 0 (and it is)
}
Şimdi, üçüncü pas
int count; //
{
int n = (count 7) / 8; //
//
switch (count % 8) { //
//
case 0: //
do { // The while jumps to here.
*to = *from ; // Copy 1 byte (total 13)
case 7: *to = *from ; // Copy 1 byte (total 14)
case 6: *to = *from ; // Copy 1 byte (total 15)
case 5: *to = *from ; // Copy 1 byte (total 16)
case 4: *to = *from ; // Copy 1 byte (total 17)
case 3: *to = *from ; // Copy 1 byte (total 18)
case 2: *to = *from ; // Copy 1 byte (total 19)
case 1: *to = *from ; // Copy 1 byte (total 20)
} while (--n > 0); // N = 1 Reduce N by 1, then jump up
// to the "do" if it's still
} // greater than 0 (and it's not, so bail)
} // continue here...
20 bayt şimdi kopyalanır.
Not: Most Wanted orijinal Cihaz (yukarıda gösterilen) to
adrese g/Ç aygıtına kopyalanır. Böylece, artırmak için gerekli değildi işaretçi *to
. İki bellek arabellekleri arasında kopyalarken *to
kullanmak gerekir.
Nasıl 'dosyalarda grup' gerç...
Nasıl'In birincil e-posta adresin...
Nasıl Google çerçeveler (Oyun, Hesapla...
Nasıl karma bir tablo işe yarıyor mu?...
Nasıl'In LMAX topu desen işe yarı...