124
Struttura del Computer Struttura del Computer Unità di Input Unità di Output Memoria Unità Logico- Aritmetica (ALU) Unità di Controllo I/O CPU o Processore ità di Input accetta informazioni codificate dall’O nformazione può essere memorizzata o elaborata dall sequenza di passi necessaria ad elaborare l’inform ene determinata da un programma residente in memori i sono presentati all’esterno tramite le unità di O e queste operazioni sono coordinate dall’unità di c

Struttura del Computer

  • Upload
    yuri

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

Struttura del Computer. Unità Logico- Aritmetica (ALU). Unità di Input. Memoria. Unità di Output. Unità di Controllo. I/O. CPU o Processore. L’ Unità di Input accetta informazioni codificate dall’Operatore. Tale informazione può essere memorizzata o elaborata dall’ ALU. - PowerPoint PPT Presentation

Citation preview

Page 1: Struttura del Computer

Struttura del ComputerStruttura del Computer

Unità di Input

Unità di OutputMemoria

Unità Logico-Aritmetica (ALU)

Unità di Controllo

I/O CPU o Processore

L’Unità di Input accetta informazioni codificate dall’Operatore

Tale informazione può essere memorizzata o elaborata dall’ALU

La sequenza di passi necessaria ad elaborare l’informazione Viene determinata da un programma residente in memoria

I risultati sono presentati all’esterno tramite le unità di Output

Tutte queste operazioni sono coordinate dall’unità di controllo

Page 2: Struttura del Computer

Istruzioni e DatiIstruzioni e Dati

Istruzioni ( o istruzioni macchina)

Sono comandi che:

Governano il trasferimento di Informazioni siaall’interno del computer sia tra computer edispositivi di I/O

Specificano le operazioni Aritmetico-Logicheda effettuare

Un’insieme di istruzioni che svolgono un determinato compitoè un PROGRAMMA

Page 3: Struttura del Computer

Istruzioni e Dati (cont.)Istruzioni e Dati (cont.)

I DATI sono numeri o caratteri codificati (estesa)

Ogni numero, carattere o istruzione è codificata con unaUna stringa di cifre binarie dette BIT (BInary digiT) Ognuno dei quali può assumere il valore 0 oppure 1

0 = 0000 0*23+ 0*22+ 0*21+ 0*20

1 = 0001 0*23+ 0*22+ 0*21+ 1*20

2 = 0010 0*23+ 0*22+ 1*21+ 0*20

6 = 0110 0*23+ 1*22+ 1*21+ 0*20

Page 4: Struttura del Computer

Programma Alto LivelloProgramma Alto Livello

main(){

int a,b,i;int c[100];char *p;

a=100;b=200;

a=a+b;

for (i=0;i<100;i++){

c[i]=a+i;}b=0;

}

load a,r1load b,r2add r1,r2store r2,a

I1 PCPC MARUC (Read: Load a,r1) MDRMDR IRa MARUC (Read: a) MDRMDR R1

Page 5: Struttura del Computer

Programma Alto LivelloProgramma Alto Livello

main(){

int a,b,i;int c[100];char *p;

a=100;b=200;

a=a+b;

for (i=0;i<100;i++){

c[i]=a+i;}b=0;

}

Memoria

i1

i+1

a

i+n

b

cDati

Istruz. 1

………

Istruz. n

Istruz. 2 Progr.

100

200

i

**

….

**

**c+1

….

c+100

Page 6: Struttura del Computer

SommarioSommario

• Quali sono le istruzioni definite (Instruction Set)

• Come si definiscono i dati

• Come si accede ai dati da programma (Indirizzamento)

• Come si cambia in flusso di esecuzione di un programma in base a risultati di operazioni aritmetico-logiche (salti, condizioni)

Page 7: Struttura del Computer

Memoria PrincipaleMemoria Principale

Composta di Celle di 1 BIT

Accesso a gruppi di n BIT (word)

Ogni Operazione elementare legge o scrive 1 word

n = Lunghezza della word ([16-64]) n=32

Ogni word ha associato un indirizzo che è un numero binario a k BIT

k bit 2k numeri nell’intervallo 0 – (2k –1)

2k = Spazio di indirizzamento

Es: k=24 S.I. = 224 word 16 Mword k=32 S.I. = 232 word 4 Gword

Page 8: Struttura del Computer

Memoria (cont.)Memoria (cont.)

MEMORIA

Unità dicontrollo ALU

R0 R1 Rn-1

MDR

MAR

PC

IR

MDR = Memory Data RegisterMAR = Memory Address RegisterPC = Program CounterIR = Instruction Register

…..

CPU

Page 9: Struttura del Computer

Memoria Principale (cont.)Memoria Principale (cont.)

0123n-1n

• • • • • • •

n bit

0

1

2

i

2k-1

Word 0

Word 1

Word 2

Word i

Word 2k-1

Ogni word può contenere DATI (numeri, caratteri) o ISTRUZIONI

0123

Byte

Page 10: Struttura del Computer

NumeriNumeri

Modulo e Segno

0123031

Modulo

Bit di segno0 = Positivo1 = Negativo

Modulo è espresso in notazione binaria posizionale

Modulo = b30* 230+ b29* 229+…….+ b1* 21+ b0* 20

[-2.147.483.647,+2.147.483.647]

Page 11: Struttura del Computer

Es: 35 = 0* 230+ 0* 229+……+ 1* 25+ 0* 24+ 0* 23+ 0* 22+ 1* 21+ 1* 20

11000 0010

11001 0010

+35 =

-35 =

Numeri (cont.)Numeri (cont.)

00000 0000

00001 0000

+0 =

-0 =

?

AMBIGUITA’AMBIGUITA’

Page 12: Struttura del Computer

Numeri (cont.)Numeri (cont.)

AMBIGUITA’AMBIGUITA’

Complemento a 1:

Numeri Positivi = come modulo e segno

Numeri Negativi = si invertono i bit del corrispondente positivo

11000 0010

00111 1101

+35 =

-35 =

00000 0000

11111 1111

+0 =

-0 =

?

Page 13: Struttura del Computer

Numeri (cont.)Numeri (cont.)

Complemento a 2:Numeri Positivi = come modulo e segno

Numeri Negativi = Complemento a 1 + 1

11000 0010

00111 1101

+35 =

-351 =

10111 1101 -352 =

00000 0000+0 =

11111 1111-01 =

00000 0000-02 =

Page 14: Struttura del Computer

CaratteriCaratteri

ASCII (7 bit)

32 bit

8 bit8 bit 8 bit 8 bit

CarattereASCII

CarattereASCII

CarattereASCII

CarattereASCII

Es: “ALBA”

BA L A

0123

Page 15: Struttura del Computer

IstruzioniIstruzioni

32 bit

8 bit 24 bit

OpCode IndirizzoOperando

28 = 256 istruzioni

OPCODE

DESCRIPTION

FORMAT

PSEUDOCODE

0 halt - exit

1 add 1 R[d] <- R[s]

+ R[t]

2 subtract 1 R[d] <- R[s]

- R[t]

3 and 1 R[d] <- R[s]

& R[t]

4 xor 1 R[d] <- R[s]

^ R[t]

5 left shift 1 R[d] <- R[s]

<< R[t]

6 right shift 1 R[d] <- R[s]

>> R[t]

7 load address 2 R[d] <- addr

8 load 2 R[d] <- mem[addr]

9 store 2 mem[addr] <-

R[d]

A load indirect 1 R[d] <- mem[R[t]]

B store indirect 1 mem[R[t]] <-

R[d]

C branch zero 2 if (R[d] == 0) pc <- addr

D branch positive 2 if (R[d] > 0) pc <- addr

E jump register - pc <- R[d]

F jump and link 2 R[d] <- pc; pc <- addr

Load a , r1Add r1 , r2Store r2 , b

OpcodeOperandi

Page 16: Struttura del Computer

Problema 1 ?Problema 1 ?

0100111000011010101011110000100

Da una configurazione binaria contenuta in una locazionedi memoria, è possibile dire se si tratta di numero,carattereo istruzione ?

NORisposta?:

Page 17: Struttura del Computer

ConvenzioniConvenzioni

0123031

31302910

10 2 3

54 6 7

2k-32k-4 2k-2 2k-1

0

4

2k-4

Minima unità indirizzabile è il Byte

23 1 0

67 5 4

2k-22k-1 2k-3 2k-4

0

4

2k-4Big-Endian

Little-Endian

Page 18: Struttura del Computer

Problema 2 ?Problema 2 ?

Un calcolatore con memoria indirizzabile a Byte,Organizzato con word di 32 bit secondo lo schemaBig-Endian.

Se un programma legge da tastiera la parola “INFORMATICA” e la memorizza in byte consecutivia partire dalla locazione 1000. Quale sarà il contenutoDella memoria?

NI F O

MR A T

0

1004

2k-4

1000

I C A1008

Page 19: Struttura del Computer

Accesso alla MemoriaAccesso alla Memoria

Fetch (Lettura)

Copia il contenuto della memoria CPU

Store (Scrittura)

CPU Memoria

Page 20: Struttura del Computer

Linguaggi di ProgrammazioneLinguaggi di Programmazione

Linguaggi ad Alto Livello(Fortran, Cobol, C, C++, ecc.)

Linguaggio Macchina

Compilatore

AssemblerAssemblatore

00100010 0000000110001000010000LOAD R0, R1

Page 21: Struttura del Computer

Istruzioni Istruzioni

Istruzioni ( o istruzioni macchina)

Sono comandi che:

Governano il trasferimento di Informazioni siaall’interno del computer sia tra computer edispositivi di I/O

Specificano le operazioni Aritmetico-Logicheda effettuare

Un’insieme di istruzioni che svolgono un determinato compitoè un PROGRAMMA

Page 22: Struttura del Computer

IstruzioniIstruzioni

• Trasferimento dati tra Memoria e CPU

• Operazioni Aritmetiche e Logiche sui dati

• Controllo del flusso del programma

• Operazioni di I/O

Page 23: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

Indirizzi in memoria

A, B, LOC, VAR

Registri

R0, R1, ACCUM, IOSTAT, IOREAD

Trasferimento dati

R0 [LOC]

Operazioni

C = A + B C [A] + [B]

Page 24: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

C [A] + [B]

Istruzione a 3 Indirizzi

ADD A,B,C

ADD A

B

C

OpCode Source1,Source2,Destination

Page 25: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

C [A] + [B]

Istruzione a 2 Indirizzi

ADD A

B

OpCode Source1,Destination

ADD A,B B [A] + [B]

Page 26: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

C [A] + [B]

MOVE B,C C [B]ADD A,C C [A] + [C]

MOVE B

C

ADD A

C

Page 27: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

C [A] + [B]

MOVE B,C C [B]ADD A,C C [A] + [C]

MOVE B

C

ADD A

C

ADD A,B,C

ADD A

B

C

Page 28: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

Registri

Ogni CPU ne ha da 8 a 64

8 registri 3 bit per indirizzarli

64 registri 6 bit per indirizzarli

• Sono più veloci

• Indirizzamento con numero minore di bit

ADD R0, R1, R2

R0ADD R1 R2

Compilatore deve ottimizzare l’uso dei registri

Page 29: Struttura del Computer

Istruzioni (cont.)Istruzioni (cont.)

Istruzioni ad 1 Operando

OpCode Source

OpCode Destination

L’altro operando è implicito. E’ un registro fissato della CPU (ACCUMULATORE)

C [A] + [B]MOVE B ACC [B]ADD A ACC [A] + [ACC]STORE C C [ACC]

MOVE B

ADD A

CSTORE

Page 30: Struttura del Computer

Problema 3 ?Problema 3 ?

Scrivere un Programma che valuti l’espressione:

A*B + C*D

In un processore con Accumulatore, ipotizzandoche esistano le istruzioni:

Load, Store, Add, Multiply

Page 31: Struttura del Computer

Esecuzione di IstruzioniEsecuzione di Istruzioni

C [A] +[B]MOVE A, R0

ADD B, R0

MOVE R0, C

Programma

n bit

i

i+1

A

i+2

MOVE A, R0

ADD B, R0

MOVE R0, C

B

C

Dati

Esecuzione in 2 Fasi:

Fetch dell’Istruzione

Esecuzione dell’Istruzione

Page 32: Struttura del Computer

Sequenze LineariSequenze Lineari

∑n

1=iiNUM = C

i

i+1

i+n-1

i+2

MOVE NUM1, R0

ADD NUM2, R0

MOVE R0, C

C

NUM2

ADD NUM3, R0

•••

ADD NUMn, R0

i+n

NUM1

Page 33: Struttura del Computer

Controllo di Flusso: SaltoControllo di Flusso: Salto

∑K

1=iiNUM = C

i

i+1

LOOP

MOVE N, R1

CLEAR R0

MOVE R0, C

C

NUM2

Determino l’indirizzodel NUM successivo edEseguo ADD NUM,R0

DEC R1

NUM1

Branch > 0 LOOP

NUMK

N K

••

Page 34: Struttura del Computer

Controllo di Flusso: CondizioniControllo di Flusso: Condizioni

Registro di Stato

NZVC

N = 1 L’operazione corrente produce un risultato negativo

Z = 1 L’operazione corrente produce un risultato zero

V = 1 L’operazione corrente produce un overflow

C = 1 L’operazione corrente produce un riporto

Page 35: Struttura del Computer

Modalità di indirizzamentoModalità di indirizzamento

Indirizzamento a Registro

Operando contenuto in un registro della CPU.Il nome del registro è specificato nell’istruzione

ADD A, R0

Indirizzamento Assoluto

Operando è una locazione di memoria.L’indirizzo della locazione è specificato nell’istruzione

ADD A, R0

Page 36: Struttura del Computer

Modalità di indirizzamento (cont.)Modalità di indirizzamento (cont.)

Indirizzamento immediato

Operando definito esplicitamente nell’istruzione.

ADD #200, R0

Indirizzamento indiretto

Indirizzo dell’operando è contenuto in un registro ouna locazione di memoria (puntatori).

ADD (A), R0

Page 37: Struttura del Computer

Modalità di Indirizzamento (cont.)Modalità di Indirizzamento (cont.)

i MOVE (A), R0

C

•••

Indirizzamento indiretto (cont.)

A C

Operando

Page 38: Struttura del Computer

Modalità di Indirizzamento (cont.)Modalità di Indirizzamento (cont.)

∑K

1=iiNUM = C

i

i+1

LOOP

MOVE N, R1

CLEAR R0

MOVE R0, C

C

NUM2

Determino l’indirizzodel NUM successivo edEseguo ADD NUM,R0

DEC R1

NUM1

Branch > 0 LOOP

NUMK

N K

••

Move N, R1

Clear R0

Move #NUM1, R2

LOOP: Add (R2), R0

Inc R2

Dec R1

Branch > 0 LOOP Move R0,C

Page 39: Struttura del Computer

Problema 4 ?Problema 4 ?

Scrivere un programma per eseguire il calcolo

C = A1*B1 + A2*B2 + A3*B3

a) Programma che esegue una sequenza lineareb) Scrivere un programma con cicloc) Calcolare il numero di accessi alla memoria richiesti sia per a) che b).

Page 40: Struttura del Computer

Problema 5 ?Problema 5 ?

Scrivere un programma per eseguire il calcolo

Il valore n è memorizzato alla locazione N.

∑n

1=iii B*A = C

Calcolare i valori di k1 e k2 nella formula k1+k2nChe rappresenta il numero di accessi alla memoriaPer il programma scritto.

Page 41: Struttura del Computer

VerificaVerifica

R1

R2

100

C=1500

••

A=1200 1500

1000

500

ADD #200, R2

ADD A, R2

MOV #A,R1ADD (R1), R2

ADD (C), R2

1000 100

200

Page 42: Struttura del Computer

Problema 6 ?Problema 6 ?

Avere un gran numero di registri riduceGli accessi alla memoria?

Suggerire un semplice problema computazionaleChe evidenzi ciò e mostrare la validità su unaMacchina con 2 registri ed una con 4.

“ Dati due array di k numeri A e B trovare il massimo diA ed il massimo di B e sommare i due massimi. “

Page 43: Struttura del Computer

Soluzione Problema 6Soluzione Problema 6

Trovare il MAX di A

Trovare il MAX di B

Sommare i due MAX

START

END

CONT = K

MAX=0

MAX < A(CONT) ?

MAX=A(CONT)

CONT = CONT-1

CONT > 0 ?SI

NO

SI

NO

MAX_A=MAX

Page 44: Struttura del Computer

Soluzione Problema 6Soluzione Problema 6

Trovare il MAX di A

Trovare il MAX di B

Sommare i due MAX

START

END

CONT = K

MAX=0

MAX < B(CONT) ?

MAX=B(CONT)

CONT = CONT-1

CONT > 0 ?SI

NO

SI

NO

MAX_B=MAX

Page 45: Struttura del Computer

CONT = K

MAX=0

MAX < A(CONT) ?

MAX=A(CONT)

CONT = CONT-1

CONT > 0 ?SI

NO

SI

NO

MAX_A=MAX

R1 #0

R3 < 0 ?

R1 [(R2)]

R2 [R2] + 1

NO

SI

NO

R0 #K

R2 #A

R3 R1-[(R2)]

R0 [R0] - 1

R0 > 0 ?SI

MAX_A [R1]

Page 46: Struttura del Computer

R1 #0

R3 < 0 ?

R1 [(R2)]

R2 [R2] + 1

SI

NO

NO

R0 #K

R2 #A

R3 [(R2)] – R1

R0 [R0] - 1

R0 > 0 ?SI

MAX_A [R1]

MOV #K, R0

MOV #0, R1

MOV #A, R2

LOOP_A: SUB (R2), R1, R3

BRANCH < 0 STEP_A

MOV (R2), R1

STEP_A: INC R2

DEC R0

BRANCH > 0 LOOP_A

MOV R1,MAX_A

Page 47: Struttura del Computer

MOV #K, R0

MOV #0, R1

MOV #A, R2

LOOP_A: SUB (R2), R1, R3

BRANCH < 0 STEP_A

MOV (R2), R1

STEP_A: INC R2

DEC R0

BRANCH > 0 LOOP_A

MOV R1,MAX_A

MOV #K, R0

MOV #0, R1

MOV #A, R2

LOOP_A: MOV R1,R3 SUB (R2), R3

BRANCH < 0 STEP_A

MOV (R2), R1

STEP_A: INC R2

DEC R0

BRANCH > 0 LOOP_A

MOV R1,MAX_A

Page 48: Struttura del Computer

MOV #K, R0 MOV #0, R1 MOV #A, R2

LOOP_A: MOV R1,R3 SUB (R2), R3 BRANCH < 0 STEP_A MOV (R2), R1

STEP_A: INC R2 DEC R0 BRANCH > 0 LOOP_A MOV R1,MAX_A

MOV #K, R0 MOV #0, R1 MOV #B, R2

LOOP_B: MOV R1,R3 SUB (R2), R3 BRANCH < 0 STEP_B MOV (R2), R1

STEP_B: INC R2 DEC R0 BRANCH > 0 LOOP_B

ADD MAX_A, R1 MOV R1,SOMMA

Page 49: Struttura del Computer

R1 #0

R3 < 0 ?

R1 [(R2)]

R2 [R2] + 1

SI

NO

NO

R0 #K

R2 #A

R3 [(R2)] – R1

R0 [R0] - 1

R0 > 0 ?SI

MAX_A [R1]

MAX_A #0

R1 < 0 ?

R1 [(R0)]

R0 [R0] + 1

SI

NO

NO

CONT #K

R0 #A

R1 [(R0)] – R1

R1 [CONT]

R1 > 0 ?SI

R1 [MAX_A]

MAX_A [R1]

R1 [R1] - 1

CONT [R1]

Page 50: Struttura del Computer

MAX_A #0

R1 < 0 ?

R1 [(R0)]

R0 [R0] + 1

SI

NO

NO

CONT #K

R0 #A

R1 [(R0)] – R1

R1 [CONT]

R1 > 0 ?SI

R1 [MAX_A]

MAX_A [R1]

R1 [R1] - 1

CONT [R1]

MOV #K,CONT MOV #0,MAX_A MOV #A,R0

LOOP_A: MOV MAX_A,R1 SUB (R0),R1 BRANCH < 0 STEP_A MOV (R0),R1 MOV R1,MAX_A

STEP_A: INC R0 MOV CONT,R1 DEC R1 BRANCH = 0 OUT_A MOV R1,CONT BRANCH LOOP_A

OUT_A:

Page 51: Struttura del Computer

MOV #K,CONT MOV #0,MAX_A MOV #A,R0

LOOP_A: MOV MAX_A,R1 SUB (R0),R1 BRANCH < 0 STEP_A MOV (R0),R1 MOV R1,MAX_A

STEP_A: INC R0 MOV CONT,R1 DEC R1 BRANCH = 0 OUT_A MOV R1,CONT BRANCH LOOP_A

OUT_A: MOV #K,CONT MOV #0,MAX_B MOV #B,R0

LOOP_B: MOV MAX_B,R1 SUB (R0),R1 BRANCH < 0 STEP_B MOV (R0),R1 MOV R1,MAX_B

STEP_B: INC R0 MOV CONT,R1

DEC R1 BRANCH = 0 OUT_B MOV R1,CONT BRANCH LOOP_B

OUT_B: MOV MAX_A,R1 ADD MAX_B,R1 MOV R1,SOMMA

Page 52: Struttura del Computer

Altre Modalità di indirizzamento Altre Modalità di indirizzamento

Indirizzamento con autoincremento

Indirizzo dell’operando è contenuto nel registro specificato.Dopo l’accesso all’operando, il contenuto del registro èIncrementato di 1 per puntare all’elemento successivoDella lista.

ADD (R1)+, R0

Indirizzamento con autodecremento

Il contenuto del registro è decrementato. Il nuovoContenuto è usato come indirizzo dell’operando.

ADD -(R1), R0

Page 53: Struttura del Computer

Altre Modalità di Indirizzamento (cont.)Altre Modalità di Indirizzamento (cont.)

∑K

1=iiNUM = C

Move #K, R1

Clear R0

Move #NUM1, R2

LOOP: Add (R2), R0

Inc R2

Dec R1

Branch > 0 LOOP Move R0,C

Move #N, R1

Clear R0

Move #NUM1, R2

LOOP: Add (R2)+, R0

Dec R1

Branch > 0 LOOP Move R0,C

Page 54: Struttura del Computer

Altre Modalità di indirizzamento Altre Modalità di indirizzamento

Indirizzamento indicizzato

Indirizzo dell’operando è ottenuto sommando un valoreCostante (offset) al contenuto di un registro (registro indice)

ADD 30(R0), R1

ADD 30(R0), R1

••

R0

R1

800

200

830

800

1000

500

Offset = 30

Page 55: Struttura del Computer

Altre Modalità di indirizzamento Altre Modalità di indirizzamento

Indirizzamento indicizzato

Indirizzo dell’operando è ottenuto sommando un valoreCostante (offset) al contenuto di un registro (registro indice)

ADD 10(R1), R0

ADD (R1, R2) , R0

ADD 10(R1, R2) , R0

Page 56: Struttura del Computer

Problema 7 ?Problema 7 ?

Una centralina metereologica ha 3 sensori:Temperatura, Pressione, Umidità. Ogni ora esegue le tre letture e le memorizza in locazioni di memoria consecutive. Il numero di letture effettuate è memorizzatoin una locazione NUM.

Scrivere un Programma che effettui la media Di tutte le misure relative allo stesso sensore.

NUM

LISTA

N

Ora 1

Umidità

••

Temperatura

Pressione

Ora 2

Temperatura

Pressione

Umidità

Ora N

Temperatura

Pressione

Umidità

Page 57: Struttura del Computer

Problema 7 (Soluzione)Problema 7 (Soluzione)

Mov #LISTA, R0

Mov NUM, R1

Clear R2

Clear R3

Clear R4

LOOP: Add 1(R0), R2

Add 2(R0), R3

Add 3(R0), R4

Add #4, R0

Dec R1

Branch > 0 LOOP

Mov NUM, R1

Div R2, R1

Mov R1, MediaTemp

Mov NUM, R1

Div R3, R1

Mov R1, MediaPress

Mov NUM, R1

Div R4, R1

Mov R1, MediaUmid

Page 58: Struttura del Computer

AssemblatoreAssemblatore

Programma del sistema operativo che traducela sequenza di istruzioni simboliche dell’utente(programma sorgente) in una sequenza di istruzioni in linguaggio macchina (programma oggetto).

Esiste una corrispondenza 1 a 1 tra istruzioniSorgenti e quelle oggetto.Ad 1 istruzione sorgente corrisponde 1 in linguaggiomacchina

Page 59: Struttura del Computer

Assemblatore (cont.)Assemblatore (cont.)

100

LOOP

MOVE N, R1

MOVE #NUM1, R2

MOVE R0, C

N

NUM2

DEC R1

NUM1

Branch > 0 LOOP

NUMK

C

300

••

CLEAR R0

ADD (R2) , R0

INC R2

101

102

103

104

105

106

107

108

••

200

201

202

203

501

C EQU 200ORIGIN 201

N DATA 300NUM1 RESERVE 300

ORIGIN 100

START MOVE N,R1MOVE #NUM1,R2CLR R0

LOOP ADD (R2),R0INC R2DEC R1BGTZ LOOPMOVE R0,C

RETURNEND START

Page 60: Struttura del Computer

Organizzazione dei datiOrganizzazione dei dati

NUM

LISTA

N

Ora 1

Umidità

••

Temperatura

Pressione

Ora 2

Temperatura

Pressione

Umidità

Ora N

Temperatura

Pressione

Umidità

LISTA o ARRAY

Page 61: Struttura del Computer

Organizzazione dei dati (cont.)Organizzazione dei dati (cont.)

STACK o PILA

E’ una lista di dati in cui gli elementi possono essereInseriti o letti solo da una estremità (TOP). L’estremitàOpposta è detta BOTTOM.

LIFO (Last-In-First-Out)

Page 62: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

Current TOP(Stack Pointer)

PUSH (Inserimento)

Dec SPMove LOC,(SP)

POP (Estrazione)

Move (SP),LOCInc SP

Page 63: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SP

PUSH (Inserimento)

Dec SPMove LOC,(SP)

LOC = 1000

SP= 102

Page 64: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SPPUSH (Inserimento)

Dec SPMove LOC,(SP)

LOC = 1000

SP= 101

Page 65: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SPPUSH (Inserimento)

Dec SPMove LOC,(SP)

LOC = 1000

SP= 101

1000

Page 66: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SP

POP (Estrazione)

Move (SP),LOCInc SP

LOC = 222

SP= 102

Page 67: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SP

POP (Estrazione)

Move (SP),LOCInc SP

LOC = 100

SP= 102

Page 68: Struttura del Computer

100

BOTTOM

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: STACKOrganizzazione dei dati: STACK

1002

200

SP

POP (Estrazione)

Move (SP),LOCInc SP

LOC = 100

SP= 103

Page 69: Struttura del Computer

Organizzazione dei dati (cont.)Organizzazione dei dati (cont.)

QUEUE o CODA

E’ una lista di dati in cui gli elementi sono inseritida una estremità (START) e letti da quellaopposta è detta (END).

FIFO (First-In-First-Out)

Page 70: Struttura del Computer

100

END

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

PUSH (Inserimento)

Inc ENDMove LOC,(END)

POP (Estrazione)

Move (START),LOCInc START

Page 71: Struttura del Computer

100

END

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

PUSH (Inserimento)

Inc ENDMove LOC,(END)

LOC = 1000

END = 108

Page 72: Struttura del Computer

100

END

738

15

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

PUSH (Inserimento)

Inc ENDMove LOC,(END)

LOC = 1000

END = 109

109

Page 73: Struttura del Computer

100

END

738

15

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

PUSH (Inserimento)

Inc ENDMove LOC,(END)

LOC = 1000

END = 109

109 1000

Page 74: Struttura del Computer

100

END

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

POP (Estrazione)

Move (START),LOCInc START

LOC = 1000

START = 102

Page 75: Struttura del Computer

100

END

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

POP (Estrazione)

Move (START),LOCInc START

LOC = 100

START = 102

Page 76: Struttura del Computer

100

END

738

15

••

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUEOrganizzazione dei dati: QUEUE

1002

200

START

POP (Estrazione)

Move (START),LOCInc START

LOC = 100

START = 103

Page 77: Struttura del Computer

100

END

738

15

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUE con Buffer CircolareOrganizzazione dei dati: QUEUE con Buffer Circolare

1002

200

START

PUSH (Inserimento)

Inc ENDCmp END,BufEndBranch > 0 INITMove LOC,(END)Return

INIT Move #BufStart,ENDMove LOC,(END)Return109

BufStart

BufEnd

Page 78: Struttura del Computer

100

END

738

15

100

-38

25

101

102

103

104

105

106

107

108

••

200

201

202

203

501

Organizzazione dei dati: QUEUE con Buffer CircolareOrganizzazione dei dati: QUEUE con Buffer Circolare

1002

200

START

POP (Estrazione)

Move START,LOCInc STARTCmp START,BufEndBranch > 0 INITReturn

INIT Move #BufStart,STARTReturn

109

BufStart

BufEnd

Page 79: Struttura del Computer

SUBROUTINESUBROUTINE

Ordinamento decrescente di numeri positivi

main(){ int lista[5]={5,10,4,7,3}; int ord[5]={0,0,0,0,0}; int pos=0; int posmax; int i;

for (i=0;i<5;i++) {max(lista,&posmax);ord[pos]=lista[posmax];pos++;lista[posmax]=0;

}}

max(int *a,int *ind){ int max; int i;

max=a[0]; ind=0; for (i=1;i<5;i++) { if (a[i] > max) {

max=a[i];*ind=i;

} } return;}

Page 80: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Istruz. 1

Istruz. 2

PC

Link Register

102

Page 81: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Istruz. 1

Istruz. 2

PC

Link Register

102

103

Page 82: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Istruz. 1

Istruz. 2

PC

Link Register

200

103

Page 83: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Istruz. 1

Istruz. 2

PC

Link Register

103

103

Page 84: Struttura del Computer

SUBROUTINESUBROUTINE

PC

Link Register

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Call SUB1

Istruz. 2

Sub SUB1

1 - Passaggio dei parametri

2 - Procedure Annidate

Page 85: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Call SUB1

Istruz. 2

Sub SUB1 Bottom

STACK

PC

103

200

Page 86: Struttura del Computer

SUBROUTINESUBROUTINE

100

Call MAX

101

102

103

104

105

106

107

108

Return

200

201

202

203

501

109

END

Istruz. Success.

Sub MAX

Call SUB1

Istruz. 2

Sub SUB1 Bottom

STACK

PC

103

501

202

Page 87: Struttura del Computer

SUBROUTINESUBROUTINE

Passaggio dei parametri

main(){ int lista[5]={5,10,4,7,3}; int ord[5]={0,0,0,0,0}; int pos=0; int posmax; int i;

for (i=0;i<5;i++) {max(lista,&posmax);ord[pos]=lista[posmax];pos++;lista[posmax]=0;

}} Bottom

STACK

Indirizzo di ritorno

lista

posmax

Page 88: Struttura del Computer

SUBROUTINESUBROUTINE

Passaggio dei parametri

100 Move #LISTA, -(SP)101 Move N, -(SP)102 Call LISTADD103 Move 1(SP),SOMMA

Bottom

STACK

LISTA

K

103 Top

Page 89: Struttura del Computer

SUBROUTINESUBROUTINE

Passaggio dei parametri

100 Move #LISTA, -(SP)101 Move N, -(SP)102 Call LISTADD103 Move 1(SP),SOMMA

LISTADD Move_Mult R0-R2, -(SP) Move 4(SP),R1 Move 5(SP),R2 Clear R0

LOOP Add (R2)+,R0 Dec R1 Branch > 0 LOOP Move R0, 5(SP) Move_Mult (SP)

+,R0-R2 Return

BottomLISTA

K

103

Top

[R0]

[R1]

[R2]

Page 90: Struttura del Computer

RiepilogoRiepilogo

Organizzazione della Memoria e dei Registri (lunghezza della word, big-endian,ecc.)

Metodo di Indirizzamento

Struttura delle istruzioni

Organizzazione dei dati

Controllo di flusso del programma

Page 91: Struttura del Computer

Motorola Motorola 6800068000

Page 92: Struttura del Computer

Motorola 68000:MemoriaMotorola 68000:Memoria

Lunghezza word = 16 bit

Gestisce anche i byte e le Longword (32 bit)

0 1

0

0

2

224-2

Big-Endian

2815

Byte

word

Il 68000 ha 64 bit

16 bit per i dati24 bit per indirizzi24 bit per controllo e aliment.

Spazio indirizzamento 16 MB

Page 93: Struttura del Computer

Motorola 68000: RegistriMotorola 68000: Registri

A0

A1

A2

A3

A4

A5

A6

A7SP allo stack Utente

SP allo Stack Supervisore

D0

D1

D2

D3

D4

D5

D6

D7

Byteword

Longword

031

SR

Page 94: Struttura del Computer

Motorola 68000: IndirizzamentoMotorola 68000: Indirizzamento

Immediate #valShort Absolute val (word)Long Absolute val (Longword)Register RnIndirect Register (An)Autoincrement (An)+Autodecrement -(An)Indexed Wval(An)Extended Indexed Bval(An,Rm.S)Relative Wval(PC)Extended Relative Bval(PC,Rm.S)

100

Istruzione

101

102

103

104

105

106

107

108

Array 2

200

201

202

203

501

109

END

Istruz. Success.

N

Data

Array 1

Array N

Bval

Rm

PC

Page 95: Struttura del Computer

Motorola 68000: Formato IstruzioniMotorola 68000: Formato Istruzioni

ADD #9,D3

15 12 9

0

7 6 5 0

dst srcopcode size

1101 011 0 01 111100Opcode ADD

Registro 3 wordImmediate

1101 0110 0111 1100 D 6 7 C

D67C

9

Page 96: Struttura del Computer

Motorola 68000: Formato IstruzioniMotorola 68000: Formato Istruzioni

A= 201150

B=201152

201200

201202

201204

201206

201208

20120C

20120E

201210

201212

20120A

63910

-21510

Opcode word

20

2200

Opcode word

Opcode word

C= 202200

20

20

1150

1152

MOVE A, D0

ADD B, D0

MOVE D0, C

Page 97: Struttura del Computer

Motorola 68000: AssemblatoreMotorola 68000: Assemblatore

EQU C EQU $1222186

ORG ORG %11100101

DC.S A DC.W 639A DC.L 65536STR DC.L “ALBA”

DS.S ARRAY DS.W 200 ARRAY DS.L 200

Page 98: Struttura del Computer

Motorola 68000: Controllo di FlussoMotorola 68000: Controllo di Flusso

Status Register

0 = C (carry)1 = V (overflow)2 = Z (zero)3 = N (negative)4 = X (Extension)8-10 = Interrupt mask13 = S (Supervisor Mode)15 = T (Trace Mode – Debugger)

Page 99: Struttura del Computer

Motorola 68000: SaltiMotorola 68000: Salti

LOOP ADD.W (A2)+,D0 SUBQ.W #1, D1 BGT LOOP

LOOP 1000

1002

1004

1006

Opcode word

Opcode word

Opcode -6

∑n

1=iiNUM = C

DBGT D1,LOOP

LOOP ADD.W (A2)+,D0DBGT D1,LOOP

Page 100: Struttura del Computer

Motorola 68000: SubroutineMotorola 68000: Subroutine

2000 MOVE.L PARAM1,-(A7)2006 MOVE.L PARAM2,-(A7)2010 BSR SUB12012 MOVE.L 4(A7),RISULTATO2018 ADDI.L #8,A7

Prima Procedura

2100 SUB1 MOVEM.L D0-D2/A0-A1,-(A7)2104 MOVEA.L 28(A7),A0

………..MOVE.L PARAM3,-(A7)

2150 BSR SUB22154 ADDI.L #4,A7

……….MOVE.L D2,28(A7)MOVEM.L (A7)+,D0-D2/A0-A1RTS

Seconda Procedura3000 SUB2 MOVE.L D0,-(A7)

MOVE.L (A7)+,D0RTS

1046

STACK

PARAM1

[D0] da SUB

PARAM3

PARAM2

2012

2154

[D0] da SUB1

[D1] da SUB

[D2] da SUB

[A0] da SUB

[A1] da SUB

1042

1038

1034

1030

1026

1022

1018

1014

1010

1006

Page 101: Struttura del Computer

PowerPC PowerPC 601601

Page 102: Struttura del Computer

PowerPC: ArchitetturaPowerPC: Architettura

Instruction ManagementUnit

Integer ALU

Floating Point ALU

CACHE

RAM

Page 103: Struttura del Computer

PowerPC:MemoriaPowerPC:Memoria

Lunghezza word = 32 bit

Gestisce anche i byte, Halfword (16 bit), Doubleword (64 bit)

0 1

31

0

8

232-8

Big-Endian

150

Halfwordword

Spazio indirizzamento 4 GB

2 3 4 5 6 7

6332

Double word

Page 104: Struttura del Computer

PowerPC: RegistriPowerPC: Registri

FR0

FR1

FR31

R0

R1

R2

R31

310

CR•••••

•••

630

0 31

XER

0 31

CTR

0 31

0 31

LINK

Page 105: Struttura del Computer

PowerPC: IstruzioniPowerPC: Istruzioni

Istruzioni che trasferiscono dati tra memoria e registri (Load e Store)

Istruzioni Aritmetico-Logiche tra i registri R0-R31 (2 o 3 operandi)

Istruzioni per il controllo di flusso

Istruzioni in floating-point

Istruzioni per il controllo del processore (Cache,I/O,ecc.)

Page 106: Struttura del Computer

PowerPC: IndirizzamentoPowerPC: Indirizzamento

Indexed HWval(Rn)

Register Indexed (Rn,Rm)

Page 107: Struttura del Computer

PowerPC: Controllo di FlussoPowerPC: Controllo di Flusso

CR

0 = LT (1 se risultato < 0)1 = GT (1 se risultato > 0)2 = EQ (1 se risultato = 0)3 = SO (Summary Overflow)

0 31

XER

0 31

CR

0 = SO (Summary Overflow)1 = OV (Overflow)2 = CA (Carry)

XER

Page 108: Struttura del Computer

PowerPC: SaltiPowerPC: Salti

B Salto Incondizionato all’indirizzo specificato

BC Salto all’indirizzo specificato se verificata la condizione(BGT, BLT, BEQ, BNE, ecc.)

Indirizzamento nel salto

Relativo offset con l’istruzione destinaz. Specificata

Assoluto destinazione Specificata

Indiretto a Registro destinaz. Specificata in registro

Page 109: Struttura del Computer

PowerPC: SubroutinePowerPC: Subroutine

Non esiste una istruzione specifica di chiamata a subroutine.

Esiste una istruzione di ritorno (BLR)

Ogni istruzione di salto memorizza l’indirizzo di ritorno nel LINK Register

Per le procedure annidate e passaggio dei parametri, ad ogni proceduraÈ riservata un area sullo stack (stack frame) a gestione della procedura

Page 110: Struttura del Computer

IA-32 IA-32

Page 111: Struttura del Computer

IA-32: EvoluzioneIA-32: Evoluzione

8086-8088 (1978): 16 bit , segmentation

80286 (1982): 24 bit, protected mode, virtual memory

80386 (1985): 32 bit, parallelism

80486 (1989): more parallelism, cache, FPU

Pentium (1993): two pipeline, more cache, data paths da 128 e 256 bits, dual processor

Pentium P6 (1995-99): (Pro, II, Celeron, III, III Xeon): two cache, more pipeline, MMX, SIMD

Pentium 4 (2000-2003) (4, Xeon, M): Netburst, Hyperthreading

Page 112: Struttura del Computer

IA-32:MemoriaIA-32:Memoria

Lunghezza word = 32 bit

Gestisce anche i byte, Halfword (16 bit), doubleword (64 bit), Fpword (80-bit)

3 2

0

0

4

232-4

Little-Endian

1531

Halfwordword

Spazio indirizzamento 4 GB1 0

Memoria Segmentata

Page 113: Struttura del Computer

IA-32: Operating ModeIA-32: Operating Mode

Protected Mode: 64GB di Memoria

Real Address Mode: 4GB Memoria

System Management Mode

Page 114: Struttura del Computer

IA-32: RegistriIA-32: Registri

CS

DS

ES

FS

GS

SS

016031

EAX

EBX

ECX

EDX

EBP

ESP

ESI

EDI

ALAH

AX

EFLAGS

EIP

General Purpose Register

Segment Register

Page 115: Struttura del Computer

IA-32: IndirizzamentoIA-32: Indirizzamento

Immediate Es: ADD EAX ,14

Register All Register (EAX,AX,AL,AH)

Memory:

Segment Selector (CS,SS,DS,ES)

Indirizzo nel segmento (Offset)

MOV ES:[EBX], EAX

Page 116: Struttura del Computer

IA-32: Indirizzamento (cont.)IA-32: Indirizzamento (cont.)

Offset (Indirizzo effettivo):

Base + (index*scale)+displacement

Base = registro (EAX,EBX,ECX,EDX,ESP,EBP,ESI,EDI)

Index = registro (EAX,EBX,ECX,EDX,EBP,ESI,EDI)

Scale = numero (1,2,4,8)

Displacement = numero (none, 8-bit, 16-bit, 32-bit)

Page 117: Struttura del Computer

IA-32: IstruzioniIA-32: Istruzioni

General Purpose Instructions

FPU e SIMD Instructions

MMX Instructions

SSE,SSE2,SSE3 Instructions

System Instructions

Page 118: Struttura del Computer

IA-32: controllo di flussoIA-32: controllo di flusso

Condizioni verificate con EFLAGS register bits

Page 119: Struttura del Computer

IA-32: SubroutineIA-32: Subroutine

CALL e RET (Near CALL, far CALL)

ENTER e LEAVE

Page 120: Struttura del Computer

IA-32: Program TemplateIA-32: Program Template

TITLE <nome programma> (templat.asm)

; Autore; Data; Revisione

INCLUDE <nome file>

.data

< dichiarazioni dei dati>

.code

main PROC

< istruzioni>

Main ENDP

< procedure>

END main

Page 121: Struttura del Computer

Esempio: somma e differenza (versione 2)Esempio: somma e differenza (versione 2)

TITLE Somma e Sottrai (addsub.asm)

; Questo programma somma e sottrae tre interi

.codemain PROC

mov eax, 10000h ; Eax= 10000hadd eax, 40000h ; Eax = Eax+40000hsub eax, 20000h ; Eax = Eax-20000h

main ENDPEND main

Page 122: Struttura del Computer

Esempio: somma e differenzaEsempio: somma e differenza

TITLE Somma e Sottrai (versione2) (addsub2.asm)

; Questo programma somma e sottrae tre interi.dataval1 DWORD 10000hval2 DWORD 40000hval3 DWORD 20000hrisul DWORD ?

.codemain PROC

mov eax, val1 ; Eax= 10000hadd eax, val2 ; Eax = Eax+40000hsub eax, val3 ; Eax = Eax-20000hmov risul,eax

main ENDPEND main

Page 123: Struttura del Computer

IA-32: Altri comandiIA-32: Altri comandi

Costanti

const EQU 100

const = 100

Array

list BYTE 10,20,30,40

var2 DWORD 10 DUP(0)

var2 DWORD 10 DUP(?)

Page 124: Struttura del Computer

Esempio: somma e differenza in Real-address modeEsempio: somma e differenza in Real-address mode

TITLE Somma e Sottrai (versione2) (addsub2.asm)

; Questo programma somma e sottrae tre interi.dataval1 DWORD 10000hval2 DWORD 40000hval3 DWORD 20000hrisul DWORD ?

.codemain PROC

mov ax,@datamov ds,ax

mov eax, val1 ; Eax= 10000hadd eax, val2 ; Eax = Eax+40000hsub eax, val3 ; Eax = Eax-20000hmov risul,eax

main ENDPEND main