910 likes | 1.08k Views
SOCS. Hoofdstuk 1 Computerarchitectuur. Inhoud. Rijen in C Rijen in DRAMA Wijzers in C Wijzers in DRAMA. C. Overzicht. Eenvoudig C Arrays Klassiek gebruik For opdracht, Increment/Decrement Wijzers, Wijzers en Arrays Array van Wijzers Meerdimensionale Tabellen Wijzer naar Array
E N D
SOCS Hoofdstuk 1 Computerarchitectuur
Inhoud • Rijen in C • Rijen in DRAMA • Wijzers in C • Wijzers in DRAMA
C Overzicht • Eenvoudig C • Arrays • Klassiek gebruik • For opdracht, Increment/Decrement • Wijzers, Wijzers en Arrays • Array van Wijzers • Meerdimensionale Tabellen • Wijzer naar Array • Functies • Records • Dynamische gegevenstructuren
int som, i; int a[10]; main() { /* inlezen van array a */ i = 0; while( i<10 ){ a[i] = getint(); i = i + 1; } /* som van de elementen */ som = 0; i = 0; while( i<10 ){ som = som + a[i]; i = i + 1; } printint (som); } 0 1 2 3 4 5 6 7 8 9 C Arrays klassiek Voorbeeld 1 -13 • Declaratie • int a[10]; • geeft 10 elementen met indices 0 .. 9 • type van elk element = int • Selectie • a[6] • a[i] • a[3*i+4]
int som, i; int a[10]; main() { /* inlezen van array a */ i = 0; while( i<10 ){ a[i] = getint(); i = i + 1; } /* som van de elementen */ som = 0; i = 0; while( i<10 ){ som = som + a[i]; i = i + 1; } printint (som); } int som, i; int a[10]; main() { /* inlezen van array a */ for (i=0; i<10; i++) a[i] = getint(); /* som van de elementen */ som = 0; for (i=0; i<10; i++) som = som + a[i]; printint (som); } C For-opdracht
C Pre- & Post Increment/Decrement • Increment- en decrement-operatoren i++ i-- ++i --i • Waarde van de uitdrukking • i voor (i++) of na (++i) de verhoging • Voorbeeld i = 10; i = 10; j = i++; j = ++i; printint (i, j); printint (i, j); 11 10 11 11
C For-opdracht uitdr1; while (uitdr2) { opdracht; uitdr3; } for (uitdr1; uitdr2; uitdr3) opdracht; uitdr1; lus: if (! uitdr2) goto nawh; opdracht; uitdr3; goto lus; nawh: …
1 2 4 8 16 32 64 128 256 512 0 1 2 3 4 5 6 7 8 9 C Geïnitialiseerde rij int a[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; int a[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
Inhoud • Rijen in C • Rijen in DRAMA • Wijzers in C • Wijzers in DRAMA
1234567890 0000 … 1909278370 0999 0000000001 1000 1001 0000000002 … Rij ‘a’ in geheugenregistersmet adres 1000, 1001, …, 1009 0000000256 1008 0000000512 1009 0019283776 1010 … 7362516278 9999 Rijen in DRAMA • Rij # opeenvolgende geheugenregisters int a[10] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
Rijen in DRAMA • Niet geinitialiseerd: In C:int a[10]; In DRAMA: a: RESGR 10 • Geinitialiseerd: In C: int a[] = {64, 128, 256, 512 }; In DRAMA: a: 64; 128; 256; 512
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } Voorbeeld
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a| a 1000, …, 1009| i R1| som R0 Voorbeeld
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 Voorbeeld HIA.w R0,0 HIA.w R1,0
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 Voorbeeld lus: VGL.w R1,10 VSP GRG,ewh
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh Voorbeeld a[0] a[0] OPT R0,1000
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 Voorbeeld OPT.w R1,1
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 Voorbeeld SPR lus
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 SPR lus Voorbeeld ewh: DRU
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 SPR lus ewh: DRU Voorbeeld STP
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 SPR lus ewh: DRU STP 9 9 a[i] = a[0]+a[1]+…+a[9] a[0] = 10 * a[0] i=0 i=0 Voorbeeld Correct? NEEN!
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 SPR lus ewh: DRU STP Voorbeeld 1001 1002 1003 enz.
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000 OPT.w R1,1 SPR lus ewh: DRU STP Voorbeeld Hoe? 0: 1: 2: 3: 4: 5: 6: 7: 8: 7 2
| Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 0: HIA.w R0,0 1: HIA.w R1,0 2: VGL.w R1,10 3: VSP GRG,7 4: OPT R0,1000 5: OPT.w R1,1 6: SPR 2 7: DRU 8: STP 1111000000 0000 1111100000 0001 3111100010 0002 0003 3321200007 2131001000 0004 2111100001 0005 3221000002 0006 7299999999 0007 9999999999 0008 … Voorbeeld 1001 1002 1003 2131001001 2131001002 2131001003 enz.
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | Geen symb. adres voor a | a 1000, …, 1009| i R1| som R0 0: HIA.w R0,0 1: HIA.w R1,0 2: VGL.w R1,10 3: VSP GRG,7 4: OPT R0,1000 5: OPT.w R1,1 6: SPR 2 7: DRU 8: STP adreswijziging Rekenen met bevelen 10 5: HIA R2,4 6: OPT.w R2,1 7: BIG R2,4 8: 9: 10: 11:
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh adr: OPT R0,a HIA R2,adr OPT.w R2,1 BIG R2,adr OPT.w R1,1 SPR lus ewh: DRU STP a: 1;2;3;4;5;6;7;8;9;10 EINDPR adreswijziging Rekenen met bevelen a in het geheugen
| met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh adr: OPT R0,a HIA R2,adr OPT.w R2,1 BIG R2,adr OPT.w R1,1 SPR lus ewh: DRU STP a: 1;2;3;4;5;6;7;8;9;10 EINDPR 1111000000 0000 1111100000 0001 3111100010 0002 3321200007 0003 2131000012 0004 1131200004 0005 2111200001 0006 1221200004 0007 2111100001 0008 3221000002 0009 7299999999 0010 9999999999 0011 a[0] a[1] a[2] a[3] 0000000001 0012 0000000002 0013 0000000003 0014 0000000004 0015 … Voorbeeld a[2] a[3] a[0] a[1] 2131000013 2131000014 2131000015 0: 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: enz.
Rekenen met bevelen • Nadelen: • 3 extra bevelen voor elke adreswijziging • Programma wordt gewijzigd tijdens uitvoering • Genestelde lussen adres herstellen • Niet algemeen genoeg: k = 2 * i + 3; som += a[k];
Indexregister 0000000017 0000069812 R3 R3 Effectief adres = 217 Formeel adres Effectief adres Effectief adres = 12 Indexregisters • Adres aanpassen tijdens de uitvoering • Hoe? • ADRESDEEL + inhoud van een (index)register • In DRAMA: elke accumulator kan indexregister zijn HIA R2,200(R3) R2 Geheugen[ (200 + R3) % 10000) ]
int a[10]; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | a 1000,1001,…,1009 | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh Voorbeeld OPT R0,1000(R1)
int a[10]; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | a 1000,1001,…,1009 | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000(R1) OPT.w R1,1 SPR lus ewh: DRU STP EINDPR Voorbeeld
Tijdens de uitvoering: R1 = 0, 1, 2, ..., 9, (10) OPT R0,1000(R1) Achtereenvolgens 1000+0 = 1000 1000+1 = 1001 1000+2 = 1002 enz. som = a[0]+a[1]+…+a[9] | a 1000,1001,…,1009 | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,1000(R1) OPT.w R1,1 SPR lus ewh: DRU STP EINDPR Voorbeeld
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh Voorbeeld (2) OPT R0,a(R1)
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,a(R1) OPT.w R1,1 SPR lus ewh: DRU STP Voorbeeld (2) a: 1;2;3;4;5;6;7;8;9;10
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) { som += a[i]; i++; } printint (som); } | met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,a(R1) OPT.w R1,1 SPR lus ewh: DRU STP a: 1;2;3;4;5;6;7;8;9;10 EINDPR Voorbeeld (2)
int a[10] = ...; int k, som; main() { ... som += a[2]; ... som += a[k]; ... } | a, k in geheugen | som R1 Adresberekening
int a[10] = ...; int k, som; main() { ... som += a[2]; ... som += a[k]; ... } | a, k in geheugen | som R1 Adresberekening OPT R1,a+2
int a[10] = ...; int k, som; main() { ... som += a[2]; ... som += a[k]; ... } | a, k in geheugen | som R1 OPT R1,a+2 Adresberekening HIA R2,k OPT R1,a(R2)
Indexregisters • Waarde • HIA.w R0,5 • HIA.w R0,5(R1) • Geheugenadres • HIA R0,13 • HIA R0,13(R1) • HIA.d R0,13 • HIA.d R0,13(R1) • Registeroperand • HIA R0,R2 R0 5 R0 5 + R1 R0 Geheugen[13] R0 Geheugen[ (13+R1)%10000 ] Alternatieve schrijfwijze R0 R2 R0 0 + R2 = R2 • HIA.w R0,0(R2)
fc mode acc idx operand Interne Machinetaal • Modecijfers
Volgordebesturing (2) • C-programma dat volgordebesturing simuleert • Variabelen van de processor: • int BT; /* bevelenteller */ • int stop; • int BR; /* bevelenregister */ • int fccode,mode,acc,idx,operand; /*velden*/ • intmode1, mode2; /* mode-cijfers */ • int waarde; /* hulpregister */ • Functie: • expandeer(getal): 4 cijfers 10 cijfers4999 00000049995800 9999995800;
BT = 0; stop = 0; while (! stop) { /* haal bevel op */ BR = geheugen[BT]; BT = BT + 1; /* analyseer bevel */ fccode = BR / 100000000; mode = (BR%100000000) /1000000; mode1 = mode / 10; mode2 = mode % 10; acc = …; idx = …; operand = expandeer(BR % 10000); if (mode2 == 2) /* indexatie */ operand = operand + register[idx]; if (mode1 == 3) /* operand bevat adres */ operand = geheugen[ operand % 10000]; /* voer bevel uit */ switch (fccode) { case HIA: register[acc] = operand; break; Volgordebesturing (2)
BT = 0; stop = 0; while (! stop) { /* haal bevel op */ BR = geheugen[BT]; BT = BT + 1; /* analyseer bevel */ fccode = BR / 100000000; mode = (BR%100000000) /1000000; mode1 = mode / 10; mode2 = mode % 10; acc = …; idx = …; operand = expandeer(BR % 10000); case BIG: geheugen[operand%10000] = register[acc]; break; case OPT: register[acc] += operand; break; … case SPR: BT = operand % 10000; break; case STP: stop = 1; } } Volgordebesturing (2)
Autoincrement/decrement • Vaak indexregisters: • Tellen in programmalussen • Ophogen/verlagen van indices • DRAMA-computer: • (R1) R1 ongewijzigd gebruiken als indexregister • (+R1) R1 verhogen voor gebruik als indexregister • (R1+) R1 verhogen na gebruik als indexregister • (-R1) R1 verlagen voor gebruik als indexregister • (R1-) R1 verlagen na gebruik als indexregister • Merk op: wijziging indexregister heeft nooit invloed op ConditieCode
0000000000 R0 0000000020 R1 R2 0000000050 R3 9999999999 … … 0000001000 BT 0029 0000002000 0030 0000004000 0031 … Autoincrement/decrement • Voorbeelden HIA R0,10(R1+) R0 Geheugen[(10+20)%10000] = 2000; R1 20+1 = 21;
0000002000 R0 0000000021 R1 R2 0000000050 R3 9999999999 … … 0000001000 BT 0029 0000002000 0030 0000004000 0031 … Autoincrement/decrement • Voorbeelden HIA R0,10(R1+) R0 Geheugen[(10+20)%10000] = 2000; R1 20+1 = 21; HIA.w R3,1(-R2) R2 50-1 = 49; R3 1+49 = 50;
0000002000 R0 0000000021 R1 R2 0000000049 R3 0000000050 … … 0000001000 BT 0029 0000002000 0030 0000004000 0031 … Autoincrement/decrement • Voorbeelden HIA R0,10(R1+) R0 Geheugen[(10+20)%10000] = 2000; R1 20+1 = 21; HIA.w R3,1(-R2) R2 50-1 = 49; R3 1+49 = 50; SPR 100(+R1) R1 21+1 = 22; BT (100+22)%10000 = 122;
0000002000 R0 0000000022 R1 R2 0000000049 R3 0000000050 … … 0000001000 0122 BT 0029 0000002000 0030 0000004000 0031 … Autoincrement/decrement • Voorbeelden HIA R0,10(R1+) R0 Geheugen[(10+20)%10000] = 2000; R1 20+1 = 21; HIA.w R3,1(-R2) R2 50-1 = 49; R3 1+49 = 50; SPR 100(+R1) R1 21+1 = 22; BT (100+22)%10000 = 122;
int a[10] = {1,2,3,4,5,6,7,8,9,10}; int som, i; main() { som = 0; i = 0; while (i < 10) som += a[i++]; printint (som); } | met symb. adressen | i R1| som R0 HIA.w R0,0 HIA.w R1,0 lus: VGL.w R1,10 VSP GRG,ewh OPT R0,a(R1) OPT.w R1,1 SPR lus ewh: DRU STP a: 1;2;3;4;5;6;7;8;9;10 EINDPR Voorbeeld (3) a(R1+)
fc mode acc idx operand Interne Machinetaal • Modecijfers