2016-02-25 3 views
7

Ich implementiere einen Verschlüsselungsalgorithmus (für Bildungszwecke) und habe etwas merkwürdiges bemerkt. Ein Teil des Algorithmus verwendet s-Boxen Substitution zu tun, so dass ich zugewiesen const Arrays als Lookup-Tabelle wie folgt zu verwenden:Warum ist mein const-Array auf dem Stack statt im Textbereich gespeichert?

const unsigned char s0_lookup[4][4]={{1,0,3,2}, 
            {3,2,1,0}, 
            {0,2,1,3}, 
            {3,1,3,2}}; 
const unsigned char s1_lookup[4][4]={{0,1,2,3}, 
            {2,0,1,3}, 
            {3,0,1,0}, 
            {2,1,0,3}}; 

Da die Arrays verwenden, um den const Qualifier Ich dachte, sie sollte im Textbereich gespeichert werden anstatt auf dem Stapel. Allerdings, wenn ich die Ausgabe des Compilers ZERLEGEN Ich sehe dies:

0000000000000893 <s_des_sbox>: 
893: 55      push %rbp 
894: 48 89 e5    mov %rsp,%rbp 
897: 48 89 7d c8    mov %rdi,-0x38(%rbp) 
89b: c6 45 dd 00    movb $0x0,-0x23(%rbp) 
89f: c6 45 e0 01    movb $0x1,-0x20(%rbp) 
8a3: c6 45 e1 00    movb $0x0,-0x1f(%rbp) 
8a7: c6 45 e2 03    movb $0x3,-0x1e(%rbp) 
8ab: c6 45 e3 02    movb $0x2,-0x1d(%rbp) 
8af: c6 45 e4 03    movb $0x3,-0x1c(%rbp) 
8b3: c6 45 e5 02    movb $0x2,-0x1b(%rbp) 
8b7: c6 45 e6 01    movb $0x1,-0x1a(%rbp) 
8bb: c6 45 e7 00    movb $0x0,-0x19(%rbp) 
8bf: c6 45 e8 00    movb $0x0,-0x18(%rbp) 
8c3: c6 45 e9 02    movb $0x2,-0x17(%rbp) 
8c7: c6 45 ea 01    movb $0x1,-0x16(%rbp) 
8cb: c6 45 eb 03    movb $0x3,-0x15(%rbp) 
8cf: c6 45 ec 03    movb $0x3,-0x14(%rbp) 
8d3: c6 45 ed 01    movb $0x1,-0x13(%rbp) 
8d7: c6 45 ee 03    movb $0x3,-0x12(%rbp) 
8db: c6 45 ef 02    movb $0x2,-0x11(%rbp) 
8df: c6 45 f0 00    movb $0x0,-0x10(%rbp) 
8e3: c6 45 f1 01    movb $0x1,-0xf(%rbp) 
8e7: c6 45 f2 02    movb $0x2,-0xe(%rbp) 
8eb: c6 45 f3 03    movb $0x3,-0xd(%rbp) 
8ef: c6 45 f4 02    movb $0x2,-0xc(%rbp) 
8f3: c6 45 f5 00    movb $0x0,-0xb(%rbp) 
8f7: c6 45 f6 01    movb $0x1,-0xa(%rbp) 
8fb: c6 45 f7 03    movb $0x3,-0x9(%rbp) 
8ff: c6 45 f8 03    movb $0x3,-0x8(%rbp) 
903: c6 45 f9 00    movb $0x0,-0x7(%rbp) 
907: c6 45 fa 01    movb $0x1,-0x6(%rbp) 
90b: c6 45 fb 00    movb $0x0,-0x5(%rbp) 
90f: c6 45 fc 02    movb $0x2,-0x4(%rbp) 
913: c6 45 fd 01    movb $0x1,-0x3(%rbp) 
917: c6 45 fe 00    movb $0x0,-0x2(%rbp) 
91b: c6 45 ff 03    movb $0x3,-0x1(%rbp) 

Der Code bewegt wörtliche Konstanten ein leeres Array auf dem Stapel zu füllen! Das erscheint mir schrecklich ineffizient, wenn das ganze Array einfach als Konstante gespeichert werden könnte. Warum macht mein Code das?

+1

Wird diese Variable in einer Funktion deklariert? – RedX

+3

Ja, statisch ist es auch. –

+0

Ja. Die S-Box-Arrays sind innerhalb einer Funktion definiert. – rstif350

Antwort

5

Da es in einer Funktion deklariert ist und nicht statisch, wird es normalerweise auf dem Stapel zugeordnet. Da C Rekursion erlaubt, erhält jeder neue Aufruf Ihrer Funktion eine neue Kopie des Arrays, die zur Laufzeit gefüllt wird.

Um es nur einmal zum Zeitpunkt der Erstellung, sollten Sie machen es statisch machen initialisiert:

static const unsigned char s0_lookup[4][4]={{1,0,3,2}, 
            {3,2,1,0}, 
            {0,2,1,3}, 
            {3,1,3,2}}; 

Wie es deklariert ist const, Optimierung Verwendung der As-if-Regel machen könnte und kompilieren Sie es, wie Sie geschrieben hatte, static const ... aber nichts zwingt den Compiler, es zu tun.

Verwandte Themen