aii e05 istruzionimars 1arch2.000webhostapp.com/appunti/aii_e05_istruzionimars_1.pdfistruzioni mars...
TRANSCRIPT
![Page 1: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/1.jpg)
Architettura degli ElaboratoriElaboratori
Istruzioni MARS
Dott. Franco Liberati
![Page 2: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/2.jpg)
Argomenti della lezione Istruzioni
Spostamento
o movimento
Logico Aritmetiche
Salto Salto
Condizionato
incondizionato
a sub-routine
(interruzioni software:trap)
Istruzioni di sistema
![Page 3: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/3.jpg)
Architettura degli ElaboratoriElaboratoriIstruzioni generalità
Dott. Franco Liberati
![Page 4: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/4.jpg)
Istruzioni MARSGeneralità
Le istruzioni del linguaggio assembly possono essere divise nelle seguenti categorie:Istruzioni “Load and Store”
Spostano dati tra la memoria e i registri generali del processore (i valori non sono modificati, ma solo spostati)
Istruzioni “Load Immediate”Caricano, nei registri, valori costanti
Istruzioni “Data Movement”Spostano dati tra i registri del processoreSpostano dati tra i registri del processore
Istruzioni aritmetico/logicheEffettuano operazioni aritmetico, logiche o di scorrimento sui registri del processore(il valore risultante modifica i condition code della ALU)
Istruzioni di confrontoEffettuano il confronto tra i valori contenuti nei registri
Istruzioni di salto condizionatoSpostano l’esecuzione da un punto ad un altro di un programma in presenza di certe condizioni
Istruzioni di salto non condizionatoSpostano l’esecuzione da un punto ad un altro di un programma
Istruzioni di sistema o comandoInfluenzano lo svolgimento del programma (HALT, NOP,BREAK , TRAP …)
![Page 5: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/5.jpg)
Architettura degli ElaboratoriElaboratori
Istruzioni di spostamento
![Page 6: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/6.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD/STORE
L'architettura di MIPS è di tipo Load-and-Store In pratica, la maggioranza delle istruzioni di MIPS operano utilizzando
i registri interni al processore e non direttamente con i valori presenti nelle celle di memoria
Esempio
add $t0, $t1, $t2 #Somma $t1+$t2 e mette il risultato in $t0
Per questo motivo
LOAD: il dato è prelevato da una cella di memoria e immesso in un registro
STORE: il valore di un registro è salvato in una cella di memoria
![Page 7: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/7.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD/STORE
lb lb rdestrdest, , addressaddress Carica un byte sito all’indirizzo address nel registro rdest
lbulbu rdestrdest, , addressaddress Carica un unsigned-byte sito all’indirizzo address nel registro rdest
lhlh rdestrdest, , addressaddress Carica un halfword sito all’indirizzo address nel registro rdest
lhulhu rdestrdest, , addressaddress Carica un unsigned-halfword sito all’indirizzo address nel registro rdestlhulhu rdestrdest, , addressaddress Carica un unsigned-halfword sito all’indirizzo address nel registro rdest
lwlw rdestrdest, , addressaddress Carica una word sita all’indirizzo address nel registro rdest
la la rdestrdest, , addressaddress Carica un indirizzo address nel registro rdest
sbsb rsourcersource, , addressaddress Memorizza un byte all’indirizzo address prelevandolo dal registro rsource
shsh rsourcersource, , addressaddress Memorizza un halfword all’indirizzo address prelevandolo dal registro
rsource
swsw rsourcersource, , addressaddress Memorizza una word all’indirizzo address prelevandola dal registro rsource
![Page 8: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/8.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD
Indirizzo in memoria della variabile valore(262160)
Contenuto (1.437.364.264)
00000000 00000100 00000000 00010000 01010101 10101100 01110000 00101000
lb lb rdestrdest, , addressaddress lb $t0,valorelb $t0,valore 00000000 00000000 00000000 00101000
lhlh rdestrdest, , addressaddress lhlh $t0,valore$t0,valore 00000000 00000000 01110000 00101000
lwlw rdestrdest, , addressaddress lwlw $t0,valore$t0,valore 01010101 10101100 01110000 10101000
la la rdestrdest, , addressaddress la $t0,valorela $t0,valore 00000000 00000100 00000000 00010000
![Page 9: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/9.jpg)
Istruzioni MARSSPOSTAMENTO: STORE
Registro Contenuto
$t0 11010001 10000001 11010101 10101010
sbsb rsourcersource, , addressaddress sbsb $t0,variabile1$t0,variabile1
shsh rsourcersource, , addressaddress shsh $t0,variabile2$t0,variabile2
swsw rsourcersource, , addressaddress swsw $t0,variabile3$t0,variabile3
Indirizzo Contenuto
variabile1 00000000 00000000 00000000 10101010
variabile2 00000000 00000000 11010101 10101010
variabile3 11010001 10000001 11010101 10101010
![Page 10: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/10.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD/STORE
.text
.globl mainmain:
lb $t1, operandA #carica operando dalla memoria al registrolb $t2, operandB #carica operando dalla memoria al registroadd $t0, $t1, $t2 #somma operandi
…
00000000
00000101
00000111
00000000add $t0, $t1, $t2 #somma operandisb $t0, risultato #sposta l‘operando in memoria
.end
.dataoperandA: .byte 5operandB: .byte 7risultato: .byte 0
00000000
00001100
…
$t1 $t2$t0
ALU
![Page 11: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/11.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD SINGNED/UNSIGNED
lb $t0, addressCarica il byte indirizzato da address nel byte meno significativo del registroIl bit di segno viene esteso
…
…
BBBBBBBB BBBBBBBB BBBBBBBB BVVVVVVVIl bit di segno viene esteso
lbu $t0, addressCarica il byte indirizzato da address nel byte meno significativo del registroGli altri tre byte vengono posti a zero
…
…
00000000 00000000 00000000 BVVVVVVV
![Page 12: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/12.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD SINGNED/UNSIGNED
lh $t0, addressCarica 2 byte indirizzati da address nei 2 byte meno significativi del registroIl bit del segno viene esteso
…
…
BBBBBBBB BBBBBBBB BVVVVVVV VVVVVVVVIl bit del segno viene esteso
lhu $t0, addressCarica i 2 byte indirizzati da address nei 2 byte meno significativi del registroGli altri due byte vengono posti a zero
…
…
00000000 00000000 BVVVVVVV VVVVVVVV
![Page 13: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/13.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD SINGNED/UNSIGNED
lw $t0, addressCarica 4 byte indirizzati da addressnel registro
…
…
BVVVVVVV VVVVVVVV VVVVVVVV VVVVVVVVnel registro• Non c’è alcuna
estensione del bit del segno
![Page 14: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/14.jpg)
Istruzioni MARSSPOSTAMENTO: LOAD SIGNED/UNSIGNED
Valore
01010101 10101100 11110000 10101000
lb $t0,valorelb $t0,valore 11111111 11111111 11111111 10101000
lbulbu $t0,valore$t0,valore 00000000 00000000 00000000 10101000lbulbu $t0,valore$t0,valore 00000000 00000000 00000000 10101000
lhlh $t0,valore$t0,valore 11111111 11111111 11110000 10101000
lhulhu $t0,valore$t0,valore 00000000 00000000 11110000 10101000
lwlw $t0,valore$t0,valore 01010101 10101100 11110000 10101000
![Page 15: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/15.jpg)
Istruzioni MARSRAPPRESENTAZIONI DI PAROLE: ENDIAN
Definizione di endianness: disposizione di una parola (word, 32 bit) nei byte di memoria
Little Endian: memorizza prima la “little end” (ovvero i bit meno significativi) della word
Big Endian: memorizza prima la “big end” (ovvero i bit più significativi) della word
Big Endian: memorizza prima la “big end” (ovvero i bit più significativi) della wordTerminologia ripresa da “I viaggi di Gulliver” di Jonathan Swift, in cui due fazioni sono in lotta per decidere se le uova sode devono essere aperte a partire dalla "little end" o dal "big end" dell'uovo
NB: I processori x86 sono little-endian
MIPS può essere little endian o big endian (L’endianness di SPIM dipende dal sistema in cui viene eseguito, quindi in generale è little endian)
Quando bisogna affrontare i problemi di endianness? Quando si "mischiano" operazioni su 8, 16 e 32 bit
Se si utilizzano operazioni uniformi, non vi sono problemi di sorta
![Page 16: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/16.jpg)
Istruzioni MARSRAPPRESENTAZIONI DI PAROLE: ENDIAN
Little Endian:
lh $t0,value
…
00001100
Big Endian:
lh $t0,value
…
0000000000001100
00100010
00000000
00000000
…
00000000 00000000 00100010 00001100 00000000 00000000 00001100 00100010
00000000
00000000
00001100
00100010
…
![Page 17: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/17.jpg)
Istruzioni MARSSPOSTAMENTO: ACQUISIZIONE DELL’INDIRIZZO DI UNA VARIABILE
L’istruzione lw $t0, address carica il contenuto della word indirizzata
dall’etichetta address in memoria
L’istruzione la $t0, address carica l'indirizzo della word indirizzata dall’etichetta
address in memoria (utile quando si vuole caricare in un registro l’indirizzo di
una particolare zona di memoria (es.: una variabile))una particolare zona di memoria (es.: una variabile))
.textlw $t0,valla $t1,val
.dataval: .byte 250 #11111010(bin)FA(hex)
In $t0 si trova il dato numerico 250
In $t1 si trova l’indirizzo di memoria
dove risiede il valore
![Page 18: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/18.jpg)
Istruzioni MARSSPOSTAMENTO: ACQUISIZIONE DELL’INDIRIZZO DI UNA VARIABILE
L’istruzione lw $t0, address carica il contenuto della word indirizzata dall’etichetta address in memoria
L’istruzione la $t0, address carica l'indirizzo della word indirizzata dall’etichetta address in memoria (utile quando si vuole caricare in un registro l’indirizzo di una particolare zona di memoria (es.: una variabile))
…
0x00400030 12
0x00400031 34
0x00400032 56
0x00400033 78
…
00101110 00111000 00100010 00001100 00000000 00101000 00000000 00011110
…
0x00400030 12
0x00400031 34
0x00400032 56
0x00400033 78
…
$t0=0x78563412 $t0=0x00400030
![Page 19: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/19.jpg)
Istruzioni MARSSPOSTAMENTO: CARICAMENTO DIRETTO DI UN VALORE
Per caricare dei dati numerici nei registri si utilizzano le operazioni di loadimmediate
Si evita di definire una valore in memoria il valore numerico è nella stessa istruzione
Il valore numerico è anche detto IMMEDIATE VALUE
Le istruzioni sono più lunghe Le istruzioni sono più lunghe
Esempio
li $t0, 234 #Mette nel registro $t0 il valore 234
li rdest, imm Muove il valore imm nel registro rdest
lui rdest, imm Muove la halfword imm nella parte più alta dell’halfword del registro
rdest
![Page 20: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/20.jpg)
Istruzioni MARSSPOSTAMENTO TRA REGISTRI
La maggioranza delle istruzioni di MIPS operano tramite i registri interni al processore
Per spostare i valori tra registri si utilizzano le operazioni di data movementEsempio
move $t0, $t1 #Copia il contenuto di $t1 in$t0move $t0, $t1 #Copia il contenuto di $t1 in$t0
move rdest, rsource Muove il registro rsource nel registro rdest
mfhi rdest Muove il registro hi nel registro rdest
mflo rdest Muove il registro lo nel registro rdest
mthi rsource Muove il registro rsource nel registro hi
mtlo rsource Muove il registro rsource nel registro lo
![Page 21: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/21.jpg)
Istruzioni MARSESEMPIO: SPOSTAMENTO
Scambio dei valori contenuti in due registri
Prima:
.text
.globl main
main:li $t0,5 #carico xli $t1,3 #carico ymove $t2,$t1 #c=y
SPOSTAMENTO.ASM
Prima:
$t0=5 e $t1=3
Dopo
$t0=3 e $t1=5
move $t2,$t1 #c=ymove $t1,$t0 #y=xmove $t0,$t2 #x=cxor $t2,$t2 #pulisco $t2
li $v0,10syscall
![Page 22: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/22.jpg)
Istruzioni MARSESERCIO PROPOSTO
Definiti due valori in
memoria x,y
scambiare la loro
.text
.globl main
main:lw $t0,x #carico xlw $t1,y #carico ymove $t2,$t1 #c=y
SPOSTAMENTO2.ASM
scambiare la loro
posizione
move $t2,$t1 #c=ymove $t1,$t0 #y=xmove $t0,$t2 #x=cxor $t2,$t2 ,$t2 #pulisco $t2sw $t0,ysw $t1,xli $v0,10syscall
.datax :.word 5 y :.word 10
![Page 23: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/23.jpg)
Istruzioni MARSESERCIO PROPOSTO
Definiti due valori in
memoria x,y
scambiare la loro
.text
.globl main
main:lw $t0,x #carico xlw $t1,y #carico ysw $t1,x #carico y in x
SPOSTAMENTO2variante.ASM
scambiare la loro
posizionesw $t1,x #carico y in xsw $t0,y #carico x in yli $v0,10syscall
.datax :.word 5 y :.word 10
![Page 24: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/24.jpg)
Istruzioni MARSRELAZIONE CON I CONDITION CODE
Codice C N Z W P
LOAD X X X X X
MOVE X X X X X
STORE X X X X X
![Page 25: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/25.jpg)
Architettura degli Elaboratori IIElaboratori II
Istruzioni logiche ed aritmetiche
![Page 26: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/26.jpg)
Istruzioni MARSISTRUZIONI LOGICHE E ARITMETICHE: GENERALITÀ
Gli elaboratori elettronici hanno come principale motivo di esistenza la possibilità di svolgere un gran numero di operazioni in tempi rapidissimi
Esempioadd $t2, $t0, $t1 #Somma il contenuto di $t1e $t0 e lo mette in $t2xor $t2,$t2,$t2 #Inizializza il registro $t2 a zeroxor $t2,$t2,$t2 #Inizializza il registro $t2 a zero
NB: tipicamente le istruzioni aritmetiche operano sfruttando i registri. I registri vengono usati per tre motivi principali: Costruiti con ottima tecnologia Sono vicini alla CPU Consentono di trovare i dati su cui operare utilizzando pochi bit
![Page 27: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/27.jpg)
Istruzioni MARSISTRUZIONI LOGICHE E ARITMETICHE: GENERALITÀ
Gli elaboratori elettronici hanno come principale motivo di esistenza la possibilità di svolgere un gran numero di operazioni in tempi rapidissimi
Esempioadd $t2, $t0, $t1 #Somma il contenuto di $t1e $t0 e lo mette in $t2xor $t2,$t2,$t2 #Inizializza il registro $t2 a zeroxor $t2,$t2,$t2 #Inizializza il registro $t2 a zero
NB: tipicamente le istruzioni aritmetiche operano sfruttando i registri. I registri vengono usati per tre motivi principali: Costruiti con ottima tecnologia Sono vicini alla CPU Consentono di trovare i dati su cui operare utilizzando pochi bit
![Page 28: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/28.jpg)
Istruzioni MARSISTRUZIONI ARITMETICHE
add rd, rs, rt rd = rs + rt (con overflow)
addu rd, rs, rt rd = rs + rt (senza overflow)
addi rd, rs, imm rd = rs + imm (con overflow)
addiu rd, rs, imm rd = rs + imm (senza overflow)addiu rd, rs, imm rd = rs + imm (senza overflow)
sub rd, rs, rt rd = rs - rt (con overflow)
subu rd, rs, rt rd = rs - rt (senza overflow)
neg rd, rs rd = - rs (con overflow)
negu rd, rs rd = - rs (senza overflow)
abs rd, rs rd = |rs|
![Page 29: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/29.jpg)
Istruzioni MARSISTRUZIONI ARITMETICHE
mult rs, rt hi,lo = rs * rt (signed, overflow impossibile)
multu rs, rt hi,lo = rs * rt (unsigned, overflow impossibile)
mul rd, rs, rt rd = rs * rt (senza overflow)
mulo rd, rs, rt rd = rs * rt (con overflow)
mulou rd, rs, rt rd = rs * rt (con overflow, unsigned))
div rs, rt hi,lo = resto e quoz. di rs / rt (signed con overflow)
divu rs, rt hi,lo = resto e quoz. di rs / rt (unsigned, no overflow)
div rd, rs, rt rd = rs / rt (signed con overflow)
divu rd, rs, rt rd = rs / rt (unsigned)
rem rd, rs, rt rd = resto di rs / rt (signed)
remu rd, rs, rt rd = resto di rs / rt (unsigned)
![Page 30: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/30.jpg)
Istruzioni MARSISTRUZIONI ARITMETICHE
Overflow nella moltiplicazione
1 1 1 1 x (-1)
1 1 1 0 (-2)
0 0 0 00 0 0 0
1 1 1 1
1 1 1 1
1 1 1 1
1 1 0 1 0 0 1 0 (+2)
![Page 31: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/31.jpg)
Istruzioni MARSISTRUZIONI ARITMETICHE
Overflow nella moltiplicazione
0 1 1 1 x (+7)
0 1 1 1 (+7)
0 1 1 10 1 1 1
0 1 1 1
0 1 1 1
0 0 0 0
0 1 1 0 0 0 1 (+49)
![Page 32: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/32.jpg)
Istruzioni MARSISTRUZIONI LOGICHE
and rd, rs, rt rd = rs AND rt
andi rd, rs, imm rd = rs AND imm
or rd, rs, rt rd = rs OR rt
ori rd, rs, imm rd = rs OR immori rd, rs, imm rd = rs OR imm
xor rd, rs, rt rd = rs XOR rt
xori rd, rs, imm rd = rs XOR imm
nor rd, rs, rt rd = NOT (rs OR rt)
not rd, rs rd = NOT rs
![Page 33: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/33.jpg)
Istruzioni MARSISTRUZIONI LOGICHE- ARITMETICHE: CASI IMMEDIATE E UNSIGNED
Versioni immediate (i)
Le versioni immediate (i) delle istruzioni precedenti utilizzano un valore
costante (risultato di un'espressione) al posto di uno degli operandi
Non sempre è necessario specificare la i; l'assemblatore è in grado di Non sempre è necessario specificare la i; l'assemblatore è in grado di
riconoscere anche istruzioni.
Esempio: add $t0, $t0, 1
Versioni unsigned (u)
Le versioni unsigned delle istruzioni non gestiscono le problematiche
relative all’overflow
Dettagli in seguito quando parleremo di eccezioni
![Page 34: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/34.jpg)
Istruzioni MARSISTRUZIONI LOGICHE- ARITMETICHE
Lo Shift (spostamento) perde i bit dalla parte della movimentazione; mentre è colmato con 0 nella direzione opposta
Il Rotate (rotazione) mantiene i bit dalla parte della movimentazione riproponendo i bit spostati nella direzione opposta
sll rd, rs, rt rd = rs è shiftato verso sinistra di rt mod 32 bitssll rd, rs, rt rd = rs è shiftato verso sinistra di rt mod 32 bits
srl rd, rs, rt rd = rs s è shiftato verso destra di rt mod 32 bits
rol rd, rs, rt rd = rs è ruotato verso sinistra di rt mod 32 bits
ror rd, rs, rt rd = rs è ruotato verso destra di rt mod 32 bits
Rotate inserisce da una parte i bit che escono dall’altra
![Page 35: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/35.jpg)
Istruzioni MARSISTRUZIONI LOGICHE- ARITMETICHE
sll $t2, $t1,3
$t1 00000000000000000000000010011101$t1 00000000000000000000000010011101
$t2 00000000000000000000010011101000$t2 00000000000000000000010011101000 1256
157
$t1 00000000000000000000000000000100$t1 00000000000000000000000000000100
ror $t2, $t1,3
$t1 00000000000000000000000010011101$t1 00000000000000000000000010011101
$t2 10100000000000000000000000010011$t2 10100000000000000000000000010011
$t1 00000000000000000000000000000100$t1 00000000000000000000000000000100$t2 00000000000000000000000000001000$t2 00000000000000000000000000001000 8
4
$t1 00000000000000000000000010011$t1 00000000000000000000000010011101101
2684354579
157
![Page 36: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/36.jpg)
Istruzioni MARSISTRUZIONI LOGICHE- ARITMETICHE: Esempio
Realizzare un programma che in linguaggio SPIM prenda un valore residente in memoria (di tipo byte) e metta 0 o 1 se il numero è rispettivamente pari o dispari
.text.text
.globl mainmain:
lb $t0,pippoli $t1,2rem $t2,$t0,$t1sb $t2,risb
li $v0,10syscall
.datapippo: .byte 100risb: .byte 0
![Page 37: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/37.jpg)
Istruzioni MARSISTRUZIONI DI CONFRONTO
Le istruzioni di confronto sono di solito usate dall’assembler ( i
programmatori usano direttamente le istruzioni di salto
condizionato)
slt rd, rs, rt Setta il registro rd a 1 se rs < rt, 0 altrimenti (con overflow)
sltu rd, rs, rt Setta il registro rd a 1 se rs < rt, 0 altrimenti (no overflow)
slti rd, rs, imm Setta registro rd a 1 se rs < imm, 0 altrimenti (con overflow)
sltiu rd, rs, imm Setta registro rd a 1 se rs < imm, 0 altrimenti (no overflow)
sle, sgt, sge, seq, sne Analoghe per testare <=, >, >=, = e <>
![Page 38: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/38.jpg)
Istruzioni MARSRELAZIONE CON I CONDITION CODE
Codice C N Z W PADD 1/0 1/0 1/0 1/0 1/0
CMP 1/0 1/0 1/0 1/0 1/0
NEG 1/0 1/0 1/0 1/0 1/0
SUB 1/0 1/0 1/0 1/0 1/0SUB 1/0 1/0 1/0 1/0 1/0
AND 0 1/0 1/0 0 1/0
OR 0 1/0 1/0 0 1/0
XOR 0 1/0 1/0 0 1/0
NOT 0 1/0 1/0 0 1/0
SL 1/0 1/0 1/0 1/0 1/0
SR 1/0 1/0 1/0 1/0 1/0
ROL 1/0 0 0 0 1/0
ROR 1/0 0 0 0 1/0
![Page 39: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/39.jpg)
Istruzioni MARSESEMPIO
Si scriva un programma in linguaggio assemblyche definito un valore intero in memoria
.text
.globl main
main:
PRESUC.ASM
che definito un valore intero in memoria riporti in $t0 l’intero precedente, in $t1 il numero corrente e in $t2 il successivo
main:lw $t1,x #carico xsub $t0,$t1,1 #pre=x-1add $t2,$t1,1 #suc=x+1li $v0,10syscall
.datax :.word 5
![Page 40: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/40.jpg)
Istruzioni MARSESEMPIO
Si scriva un programma in linguaggio assemblyche definiti due numeri in memoria x
.text
.globl main
main:
MEDIA.ASM
che definiti due numeri in memoria xe y riporta la media (fra interi) in $t2
Es: x=5 y=8
media=(5+8)/2=6
main:lw $t0,x #carico xlw $t1,y #carico yadd $t2,$t0,$t1 #m=x+ydiv $t2,$t2,2 #mediali $v0,10syscall
.datax :.word 5 y:.word 8
![Page 41: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/41.jpg)
Istruzioni MARSESERCIO PROPOSTO
Scrivere un programma che definito in memoria il prezzo e lo sconto
.text
.globl main
main:
SALDI.ASM
prezzo e lo sconto (interi) riporta in $t0 il prezzo reale in $t1 lo sconto (arrotondato all’intero) e in $t2 il prezzo scontato
main:lw $t0,prezzo #carico prezzolw $t1,sconto #carico scontomul $t2,$t0,$t1 #prezzo=prezzo*25div $t2,$t2,100 #sconto=prezzo/100li $v0,10syscall
.dataprezzo : .word 133sconto: .word 25
![Page 42: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/42.jpg)
Istruzioni MARSESERCIZIO PROPOSTO PER CASA 1
Scrivere un programma che, definiti due
numeri (word) in memoria spazio e tempo, che
fanno riferimento ad un punto mobile nello fanno riferimento ad un punto mobile nello
spazio, determina :
1. La velocità del punto che si muove in modo
rettilineo uniforme (risultato in $t0)
![Page 43: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/43.jpg)
Istruzioni MARSESERCIZIO PROPOSTO PER CASA 2
Scrivere un programma che converta un valore da scala Celsius a scala Fahrenheit– Definire in memoria una variabile intera (word)
riportante il valore in scala Celsiusriportante il valore in scala Celsius
– Riportare il risultato in scala Fahrenheit in $t0
ES: gradoC=38 $t0=100
OSS: Utilizzare le operazione tra interi (il valore risultante può essere approssimato)
![Page 44: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/44.jpg)
Istruzioni MARSESERCIZIO PROPOSTO PER CASA 3
Scrivere un programma che, definiti due numeri (word) in memoria X e Y, determina le seguenti informazioni:1. $t0=0 se X è un numero positivo o $t0=1 se X è un 1. $t0=0 se X è un numero positivo o $t0=1 se X è un
numero negativo
2. $t1=0 se Y è pari $t1=1 se Y è dispari
3. $t2 riporta X+Y
4. $t3 riporta il valore massimo in valore assoluto tra
(±X) + (±Y)
![Page 45: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/45.jpg)
Architettura degli ElaboratoriElaboratoriIstruzioni di salto
![Page 46: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/46.jpg)
Istruzioni MARSISTRUZIONE DI SALTO CONDIZIONATO: GENERALITÀ
Le istruzioni condizionate fanno riferimento a i flag o codici di condizione presenti nello STATUS REGISTER derivanti da operazione logico-aritmetiche
Operando1 Operando2
STA
TUS
STA
TUS
ALU
Flag - Codici di condizione
Risultato
funzione
ZCWN
![Page 47: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/47.jpg)
Istruzioni MARSISTRUZIONE DI SALTO CONDIZIONATO
beq rs, rt, target Salta all’istruzione puntata da target se rs = rt
bne rs, rt, target Salta all’istruzione puntata da target se rs != rt
bge rs, rt, targetbgeu rs, rt, target
Salta all’istruzione puntata da target se rs >= rt
Comparazione senza considerare il segnobgeu rs, rt, target Comparazione senza considerare il segno
bgt rs, rt, targetbgtu rs, rt, target
Salta all’istruzione puntata da target se rs > rt
Comparazione senza considerare il segno
ble rs, rt, targetbleu rs, rt, target
Salta all’istruzione puntata da target se rs <= rt
Comparazione senza considerare il segno
blt rs, rt, targetbltu rs, rt, target
Salta all’istruzione puntata da target se rs < rt
Comparazione senza considerare il segno
![Page 48: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/48.jpg)
Istruzioni MARSISTRUZIONE DI SALTO CONDIZIONATO (riferimento registro ZERO, $0)
bgez rs, target Salta all’istruzione puntata da target se rs >= 0
bgtz rs, target Salta all’istruzione puntata da target se rs > 0
blez rs, target Salta all’istruzione puntata da target se rs <= 0blez rs, target Salta all’istruzione puntata da target se rs <= 0
bltz rs, target Salta all’istruzione puntata da target se rs < 0
beqz rs, target Salta all’istruzione puntata da target se rs = 0
bnez rs, target Salta all’istruzione puntata da target se rs!= 0
![Page 49: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/49.jpg)
Istruzioni MARSISTRUZIONI DI SALTO CONDIZIONATO: esempio
Realizzare un programma che valuta il massimo fra due numeri x,y (di tipo byte) definiti in memoria. Riportare il risultato in memoria
Definisci x,y
Acquisisci y
max=y
x<y
FINE
max=x
SINO
![Page 50: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/50.jpg)
Istruzioni MARSISTRUZIONI DI SALTO CONDIZIONATO: esempio
Realizzare un programma che valuta il massimo fra due numeri x,y definiti in memoria come byte.text
lb $t0,x #prelievo di xlb $t0,x #prelievo di xlb $t1,y #prelievo di ymove $t2,$t1 #In t2 conservo y come se fosse il massimoblt $t0,$t1,max_y #salto perché y è il massimomove $t2,$t0 #aggiorno il valore massimo mettendo x in $t2
max_y:sb $t2, max #conservo il massimo in
#memoria
.datax: .byte 34y: .byte 76max: .byte 0
![Page 51: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/51.jpg)
Istruzioni MARSISTRUZIONE DI SALTO INCONDIZIONATO
Le istruzioni incondizionate spostano l’esecuzione da un punto
ad un altro di un programma
j target Salto incondizionato all’istruzione puntata da target
![Page 52: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/52.jpg)
Istruzioni MARSISTRUZIONI DI SALTO CONDIZIONATO: esempio
Realizzare un contatore da 0 a 100
contatore=0
contatore=contatore+1
contatore=100SI
NO
FINE
![Page 53: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/53.jpg)
Istruzioni MARSISTRUZIONI DI SALTO CONDIZIONATO: esempio
Realizzare un contatore da 0 a 100
.text
.globl mainmain:
li $t0,0li $t0,0li $t1,100
ciclo:beq $t0,$t1,fineadd $t0,$t0,1j ciclo
fine:li $v0,10syscall
![Page 54: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/54.jpg)
Istruzioni MARSESERCIZIO SUI SALTI
Realizzare un programma che in linguaggio SPIM definisca un valore di tipo byte residente in memoria e metta in $t2 il valore 1 o 2 se il numero è rispettivamente multiplo di 7 o no
.text
.globl mainmain:
lb $t0,valorelb $t0,valoreli $t1,7rem $t3,$t0,$t1li $t2,1beqz $t3,saltali $t2,2
salta:li $v0,10syscall
.datavalore: .byte 100risb: .byte 0
![Page 55: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/55.jpg)
Architettura degli ElaboratoriElaboratori
Progettazione di un quesito matematico
Dott. Franco Liberati
![Page 56: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/56.jpg)
ProgettazioneDiagramma di flusso
Effettuare il fattoriale di un numero n (n0 e n)
Il fattoriale di un numero N
Definisci temp,fattoriale
Definisci n
n!=0
temp=1
NOSI
Leggi n
Il fattoriale di un numero Nè definito come:
N!=NN-1N-2…321NB: 0!=1
Temp=n*temp
n=n-1
fattoriale=temp
STOP
NOn>1
SI
![Page 57: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/57.jpg)
ProgettazioneImplementazione linguaggio ad alto livello
main(){int n,temp, fattoriale;temp=1;
Definisci temp,fattoriale
Definisci n
temp=1
NOSI
Leggi n
temp=1;if (n!=0){
while (n>1){temp=temp*n;n=n-1;}
}fattoriale=temp;
n!=0
temp=n*temp
n=n-1
fattoriale=temp
STOP
NO
NO
n>1
SI
SI
![Page 58: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/58.jpg)
ProgettazioneImplementazione linguaggio assemblativo
.textlw $t0,nlw $t1,templi $t5,1beqz $t0, fine
ciclo:ble $t0,$t5, fine
Definisci temp,fattoriale
Definisci n
temp=1
NOSI
Leggi n
ble $t0,$t5, finemul $t1,$t1,$t0sub $t0,$t0,1j ciclo
fine:sw $t1, fattoriale
.datafattoriale: .word 0temp: .word 1n: .word 35
n!=0
temp=n*temp
n=n-1
fattoriale=temp
STOP
NO
NO
n>1
SI
SI
![Page 59: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/59.jpg)
ProgettazioneAssemblatore
Indirizzo Etichetta Istruzione Commento
400 lw $t0,n
404 lw $t1,temp
408 li $t5,1
412 beqz $t0,fine
416 ciclo ble $t0,$t5,fine416 ciclo ble $t0,$t5,fine
420 mul $t1,$t1,$t0
424 sub $t0, $t0,1
428 j ciclo
432 fine: sw $t1,fattoriale
![Page 60: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/60.jpg)
ProgettazioneAssemblatore
Tabella Simboli
400: lw $t0,n404: lw $t1,temp408: li $t5,1412: beqz $t0, fine416:ciclo: ble $t0,$t5, fine420: mul $t1,$t1,$t0
Tabella Simboli
Etichetta/variabile Locazione
ciclo 416
fine 432
fattoriale 100
temp 104
n 108
.datafattoriale: .word 0temp: .word 1n: .word
420: mul $t1,$t1,$t0424: sub $t0,$t0,1428: j ciclo432: fine: sw $t1, fattoriale
![Page 61: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/61.jpg)
ProgettazioneAssemblatore (sostituzione etichette con indirizzi)
Tabella Simboli
400: lw $t0,(108)404: lw $t1,(104)408: li $t5,1412: beqz $t0, (432)416:ciclo: ble $t0,$t5, (432)420: mul $t1,$t1,$t0
Tabella Simboli
Etichetta/variabile Locazione
ciclo 416
fine 432
fattoriale 100
temp 104
n 108
.datafattoriale: .word 0temp: .word 1n: .word
420: mul $t1,$t1,$t0424: sub $t0,$t0,1428: j (416)432: fine: sw $t1, (100)
![Page 62: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/62.jpg)
Architettura degli ElaboratoriElaboratori
Interazione con tastiera e console
![Page 63: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/63.jpg)
Lettura di valori interi da tastiera.text
.globl main
main:
li $v0,5 #servizio di lettura di un intero da tastiera
syscall #chiamata di sistema
move $t0,$v0 #spostamento del valore letto da tastiera
add $t0,$t0,1 #incremento del valore immesso di una unità
move $a0,$t0 #spostamento del valore intero da stampare
li $v0,1 #servizio di stampa di un intero
syscall #chiamata di sistema
li $v0,10 #chiusura programma
syscall
![Page 64: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/64.jpg)
Architettura degli ElaboratoriElaboratori
Esempi di programmi
Dott. Franco Liberati
![Page 65: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/65.jpg)
MARSUn primo programma
Impostare $t0 ad 1 se il valore della variabile definita in memoria Batman è maggiore del valore della variabile definita in memoria Robin
.text
.globl main
main:main:lw $t1,batman #sposta il valore della variabile dalla memoria in $t1lw $t2,robin #sposta il valore della variabile dalla memoria in $t2li $t0,0 #inizializzazione t0 e caso Batman<Robinblt $t1,$t2, fine #confronta e se $t1<$t2 salta a fineli $t0,1 #settaggio $t0 caso Batman>Robin
fine:li $v0,10 #fine programmasyscall
.data #definizione variabilibatman: .word 5robin: .word 7
![Page 66: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/66.jpg)
MARSUn secondo programma
Impostare $t0 ad 1 se il valore della variabile definita in memoria Batman ha alla terza posizione meno significativa un 1
.text
.globl mainmain:
li $t0,0 #inizializzazione $t0 e caso Batman ha terzo bit LSB uguale a 0li $t0,0 #inizializzazione $t0 e caso Batman ha terzo bit LSB uguale a 0lw $t1,batman #sposta il valore della variabile dalla memoria in $t1li $t2,4 #sposta il valore4 in $t2and $t3, $t1,$t2 #and logicobne $t3,$t2, fine #confronta e se la terza cifra è diversa da 1 salta a fineli $t0,1 #settaggio $t0 caso Batman ha terzo LSB uguale a 1
fine:li $v0,10 #fine programmasyscall
.data #definizione variabilebatman: .word 5
![Page 67: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/67.jpg)
MARSEsercizio proposto
Implementare un programma che dato un valore definito in memoria di tipo word stabilisca se questo è primo (valore 1 in $t2) o no (valore 2 in $t2)
NB: un numero è primo se, oltre ad essere divisibile per 1, è divisibile solamente per se stesso ovvero: (n,x)=1 x -{0} con x=1 x=nper se stesso ovvero: (n,x)=1 x -{0} con x=1 x=n
cioè (n,1)=1(n,n)=1
![Page 68: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/68.jpg)
Architettura degli ElaboratoriElaboratori
Fine
Dott. Franco Liberati
![Page 69: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/69.jpg)
MARSNumeri primo – Diagramma a blocchi
p=2Leggi n
n=pSINO
PRIMO
STOP
n%p=0
p=p+1
NON PRIMO
SI
NO
![Page 70: AII E05 IstruzioniMARS 1arch2.000webhostapp.com/appunti/AII_E05_IstruzioniMARS_1.pdfIstruzioni MARS SPOSTAMENTO: LOAD/STORE llbb rdestrdest,, addressaddress Carica un byte sito all’indirizzo](https://reader030.vdocuments.pub/reader030/viewer/2022041004/5ea8d1229f2e105fa25c571c/html5/thumbnails/70.jpg)
MARSNumeri primo – Diagramma a blocchi
p=2p=2LeggiLeggi nn
n=pn=pSISINONO
.text.text
..globlglobl mainmain
main:main:lwlw $t0,n$t0,nlwlw $t1,p$t1,plili $t2,1$t2,1
inizioinizio::
PRIMOPRIMO
STOPSTOP
n%pn%p=0=0
p=p+1p=p+1
NON PRIMONON PRIMO
SISI
NONO
inizioinizio::beqbeq $t0,$t1, primo$t0,$t1, primo
remrem $t3,$t0,$t1$t3,$t0,$t1beqzbeqz $t3,non_primo$t3,non_primoadd $t1,$t1,1add $t1,$t1,1j j inizioinizio
non_primonon_primo::lili $t2,2$t2,2
primo:primo:lili $v0,10$v0,10syscallsyscall
.data .data n: .word 13n: .word 13p: .byte 2p: .byte 2