Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Linguaggio Assembler
Andrea Coccoli
A.Coccoli@guest.cnuce.cnr.it
Indice
• Linguaggio ad alto livello, linguaggio assembler e
linguaggio macchina
• Il linguaggio assembler MIPS: istruzioni aritmetiche, di
trasferimento dati e di salto
• Conversione linguaggio assembler in linguaggio macchina
• Gestione delle procedure. Uso della pila
• Modi di indirizzamento
• Sintassi di un programma in assembler MIPS
• SPIM
• Confronto con altre architetture: PowerPC e Intel 80x86
1
3/8/03
Intr oduzione
Linguaggio m acchina
2
3/8/03
Linguaggio assembler
• Più primitivo dei linguaggi di alto livello
e.g., manca un controllo di flusso sofisticato
• Molto restrittivo
e.g., Istruzioni Aritmetiche del MIPS
• Noi lavoreremo con l’architettura del set di istruzioni MIPS
– simile ad altre architetture sviluppate a partire dagli anni ‘80
– usata da NEC, Nintendo, Silicon Graphics, Sony
• Obiettivi di Progetto: massimizzare la performance e
minimizzare il costo, ridurre il tempo di progettazione
.text
.align 2
.globl main
main: 00100111101111011111111111100000
subu $sp, $sp, 32 10101111101111110000000000010100
sw $ra , 20($sp ) 10101111101001000000000000100000
Programma che ca lcola e sta mpa sd $a0, 32($ sp) 10101111101001010000000000100100
sw $0, 24($ sp) 10101111101000000000000000011000
la so mma dei q uad rati dei pri mi 100 i nteri sw $0, 28($ sp) 10101111101000000000000000011100
loop: 10001111101011100000000000011100
lw $t6, 28($ sp) 10001111101110000000000000011000
mul $t7, $t6, $t 6 00000001110011100000000000011001
#inclu de <stdi o.h> lw $t8, 24($ sp) 00100101110010000000000000000001
addu $t9, $t8, $t 7 00101001000000010000000001100101
int main (int argc, char *a rgv[]) w $t9, 24($ sp) 10101111101010000000000000011100
{ addu $t0, $t6, 1 00000000000000000111100000010010
int i; sw $t 0, 28($sp) 00000011000011111100100000100001
int sum = 0 ; ble$t 0, 100, loop 00010100001000001111111111110111
for (i = 0; i <= 1 00; i = i + 1) sum = sum + i * i ; la $a0, str 10101111101110010000000000011000
printf ("The sum from 0 .. 100 is %d\n", sum); lw $a1, 24($sp) 00111100000001000001000000000000
jal printf 10001111101001010000000000011000
} move$v0, $0 00001100000100000000000011101100
lw $ra, 20($sp) 00100100100001000000010000110000
ddu $sp , $sp, 32 10001111101111110000000000010100
jr $ra 00100111101111010000000000100000
.data 00000011111000000000000000001000
.align 0 00000000000000000001000000100001
str:
.asciiz "The sum from 0 .. 100 is %d\ n"
6
3
3/8/03
Gerarchia di trasformazione
Programma in C
Compilatore
Assemblatore
Linker
Eseguibile
Loader
Memoria
Gerarchia di trasformazione
4
3/8/03
• Vantaggi
– velocità d’esecuzione (es.: calcolatori embedded)
– occupazione di memoria
– collegabile a programmi ad alto livello
– possibilità di usare istruzioni specializzate
• Svantaggi
– minore portabilità
– fattore di espansione elevato => minore produttività
– minore leggibilità
– no programmazione strutturata
• Nota: la performance deve essere valutata sulle
istruzioni reali
9
Gli assemblatori
• File oggetto
– etichetta esterna (o globale)
– etichetta locale (funzione static in C)
• Riferimenti in avanti => tabella dei simboli.
10
5
3/8/03
• E’ composto da 6 parti
– Intestazione del file oggetto,
– Segmento di testo,
– Segmento di dato,
– Informazioni di rilocazione,
– Tabella dei simboli,
– Informazioni per il debugger
• Funzionalità aggiuntive
– direttive per la codifica dei dati
– macro
11
6
3/8/03
13
7
3/8/03
15
Moltiplicazione e divisione
• Coppia di registri 32 bit (Hi,Lo) che contengono il prodotto
di una moltiplicazione (mult $s2,$s3) ed il resto ed il
quoziente, rispettivamente, di una divisione (div $s2,$s3).
• Istruzioni mflo,mfhi per acce dere ai registri Hi e Lo
• Esempio:
– mult $s2,$s3
– mflo $s1 #$s1 = Lo (copia di Lo in $s1)
• Compito del programmatore evitare l’overflow e controllare che
non ci siano divisioni improprie (per zer o).
Istruzione Esempio Significato Commenti
move from Hi mfhi $s1 $s1 = Hi Copia Hi in $s1
move from Lo mflo $s1 $s1 = Lo Copia Lo in $s1
move to Hi mthi $s1 Hi = $s1 Copia $s1 in Hi
move to Lo mtlo $s1 Lo = $s1 Copia $s1 in Lo
16
8
3/8/03
Overflow
• Il risultato dell’operazione non e’ rappresentabile
dall’hw disponibile
• I numeri rappresentabili sono
– da 0 a (232 -1) (unsigned)
– da -231 a (231-1) (signed)
• Condizioni di overflow (numeri signed):
– A>0; B>0 e si ottiene A+B<0
– A<0; B<0 e si ottiene A+B≥0
– A ≥0; B<0 e si ottiene A-B<0
– A<0;B ≥0 e si ottiene A-B ≥0
• Le operazioni addu,subu,mult, multu, div e divu
non fanno alcun controllo sull’overflow
17
Istruzioni Aritmetiche
Istruzione Esempio Significato Commenti
9
3/8/03
• Esempio2
#f = (g + h) - (i + j)
add $t0, g, h #$t0 = g + h
add $t1, i, j #$t1 = i + j
sub f, $t0, $t1 #f = $t0 - $t1
19
Registri e Memoria
• Le istruzioni aritmetiche Control Input
Memory
operano su registri Datapath Output
– solo 32 registri
Processor I/O
– ogni registro 1W (4B )
• il compilatore associa
0 8 bits of data
variabili con registri 1 8 bits of data
• Cosa succede con 2 8 bits of data
20
10
3/8/03
21
22
11
3/8/03
23
12
3/8/03
Un esempio
swap(int v[], int k);
{ int temp;
temp = v[k]
v[k] = v[k+1];
v[k+1] = temp; swap:
} add $t1, $a1, $a1
add $t1, $t1, $t1
add $t1, $a0, $t1
lw $t0, 0($t1)
lw $t2, 4($t1)
sw $t2, 0($t1)
sw $t0, 4($t1)
jr $ra
25
• g = h + A[i]
g $s1
h $s2
A $s3 (indirizzo base)
i $s4 (offset)
26
13
3/8/03
Spilling
27
28
14
3/8/03
Riassum endo
• MIPS
– load/store word, con indirizzamento al byte
– aritmetica solo su registri
• Istruzioni Significato
add $t1, $t2, $t3 $t1 = $t2 + $t3
sub $t1, $t2, $t3 $t1 = $t2 - $t3
lw $t0, 100($t1) $t0 = Memory[$t1+100]
sw $t0, 100($t1) Memory[$t1+100] = $t0
29
Linguaggio m acchina
op rs rt rd shamt funct
6 bit 5 bit 5 bit 5 bit 5 bit 6 bit
30
15
3/8/03
Linguaggio m acchina
31
op rs rt address
opcode base Dest(lw)
• Principio di progetto 3:
– un buon progetto richiede buoni compromessi
32
16
3/8/03
• esempio
lw $t0, 32($s3)
lw $8, 32($19)
35 19 8 32
op rs rt address
opcode base Dest
• Compromesso di progetto
– anche lw/sw sono lunghe 1w, con displacement costante (operando
immediato di 16 bit) inserito direttamente nell’istruzione
33
17
3/8/03
Ciclo fetch&execute
Instruction Fetch
Instruction Decod e
Operand Fetch
Execute
Result Sto re
Next Instruction
35
memory
OS
code
Program 1
data
CPU
unused
Program 2
unused
36
18
3/8/03
Dati dinamici
Segmento di dato
Dati statici
0x10000000
0x400000
Riservata
37
Istruzioni di controllo
• Istruzioni per prendere le decisioni
– alterano il controllo del flusso (sequenziale)
– cambiano quindi la prossima istruzione da eseguire
• Istruzioni MIPS di salto condizionato
bne $t0, $t1, Label # branch if not equal
beq $t0, $t1, Label # branch if equal
• Esempio: if (i==j) h = i + j;
bne $s0, $s1, Label
add $s3, $s0, $s1
Label: ....
38
19
3/8/03
Istruzioni di controllo
• Salto non condizionato
j label
39
Istruzioni di controllo
• Esempio costrutto while:
while (vett[i] == k) i=i+j;
– i, j e k sono contenuti nei registri $s3, $s4 e $s5 e l’indirizzo base
di vet t in $s6
ciclo:add $t1, $s3, $s3 #t1 =2*i
add $t1, $t1, $t1 #t1 =4*i
add $t1, $t1, $s6 #t1 = indirizzo di vett[i]
lw $t0, 0($t1) #t0 = vett[i]
bne $t0, $s5, esci #vai a esci se
#vett[i] = k
add $s3, $s3, $s4 #i=i+j
j ciclo #vai a ciclo
Esci: ...
40
20
3/8/03
Istruzioni di controllo
• Istruzione set on less than:
#if $s1 < $s2 then
slt $t0, $s1, $s2 # $t0 = 1
# else $t0 = 0
• Questa istruzione puo’ essere utilizzata per creare “blt” che non
e’ predefinita nel linguaggio MIPS. Per fare questo e’ necessario
l’uso del registro $zero.
• Formato J-Type
J op 26 bit address
6 bit 26 bit
41
Comando FOR
• C code:
for (i=0; i != n; i = i+k)
a = a+b;
• MIPS:
add $t0, $zero, $zero # i=$t0=0
Loop:
beq $t0, $s0, Exit # if (i== n) goto Exi t
add $s1, $s1, $s2 # a = a+b ;
add $t0, $t0, $t1 # i = i+k ;
j Loop # goto Loop
Exit:
42
21
3/8/03
Comando Case/Switch
• Comando Case/switch
– puo’ essere tradotto in una catena di if-then-else
– caso peggiore: tempo di esecuzione proporzionale al numero di casi
– puo’ essere reso più veloce con una jump address table
ind. in cui si trovano le sequenze di istruzioni = vettore con indirizzi delle
etichette
• Nuova istruzione:
salto tramite registro jr $t0 (jump register )
• switch (k){
case 0: f = i+j; break;
case 1: f = g+h; break;
case 2: f = g-h; break;
case 3: f = i-j; break;
}
43
Comando Case/Switch
slt $t3, $s5, $zero
Assembler code: bne $t3, $zero, Exi t
slt $t3, $s5, $t2
1. test se 0 <= k <= 3 beq $t3, $zero, Exi t
2. calcola indirizzo add $t1, $s5, $s5
3. trova indirizzo di salto e salta add $t1, $t1, $t1
4. esegue il codice add $t1, $t1, $t4
lw $t0, 0($t1)
Data: jump table
jr $t0
$t4 address L0 L0: add $s0, $s3, $s4
$t4+4 address L1 j Exit
L1: add $s0, $s1, $s2
$t4+8 address L2
j Exit
$t4+12 address L3
L2: sub $s0, $s1, $s2
j Exit
$t4 <-> base
$t2 <-> 4 L3: sub $s0, $s3, $s4
$s5 <-> k Exit:
44
22
3/8/03
Istruzioni di confronto
Istruzione Esempio Significato Commenti
set less than slt $s1,$s2,$s3 if ($s2<$s3) $s1=1; Confronto tra registri
else $s1=0 (con segno)
set less than imm. slti $s1,$s2,100 if ($s2<100) $s1=1; Confronto registro-costante
else $s1=0 (con segno)
set less than uns. sltu $s1,$s2,$s3 if ($s2<$s3) $s1=1; Confronto tra registri (senz a
else $s1=0 segno)
set l. t. imm. uns. sltiu $s1,$s2,100 if ($s2<100) $s1=1; Confronto registro-costante
else $s1=0 (senz a segno)
45
Branch e Branch&Link
Istruzione Esempio Significato Commenti
branch gr. eq. zero bgezal $s1, P if ($s1>= $0) { Test di non negativo; PC-rel.
and link $ra = PC + 4; Per le chiamate a procedura
go to P}
branch less th. zero bltzal $s1, P if ($s1< $0) { Test di negativo; PC-rel.
and link $ra = PC + 4; Per le chiamate a procedura
go to P} 46
23
3/8/03
Jump
47
Procedure/Funzioni
• Molto utili in linguaggi di alto livello
– astrazione - riusabilita’ - strutturazione/modularità
• Chi si “occupa” della chiamata
– il compilatore nei linguaggi ad alto livello
– il programmatore in assembler
• Passi necessari
– passare i parametri al chiamato
– salvare l’indirizzo di ritorno e passare il controllo alla procedura
– allocare spaz io per variabili locali
– eseguire il corpo della procedura
– passare i risultati al chiamante
– riportare il controllo al punto di partenza
48
24
3/8/03
49
chiamante
chiamato
... label: ...
... ...
jal label ...
$ra ... ...
... ...
... ...
... jr $ra
... ...
25
3/8/03
Chiamate annidate
chiamante
chiamato/
chiamante
... chiamato
loc: ...
...
...
jal loc loc2: ...
$ra ...
... ...
jal loc2
... ...
$ra ...
...
...
...
...
...
...
jr $ra
jr $ra
...
51
52
26
3/8/03
53
27
3/8/03
Stack frame
Scopo
• memorizzare i dati di una $fp Argomento 5
Argomento 6
procedura in una struttura singola
...
• si puo’ accedere ai dati con $fp ...
• usare $sp puo’ essere difficile Registri
perche’ la pila puo’ variare salvati Stack
frame
• E’ necessario?
– si, per le chiamate ricorsive Variabili
– si, per chiamate di procedure locali
complesse
– no, per programmi semplici
$sp
– I compilatori lo usano!
• Contenuti
– argomenti non passati con a0-a3
– valori di registri salvati Lo stack cresce
verso il basso
– variabili locali della procedura
55
Stack Frame
argomento 5 $fp
SF argomento 6
procedura A
...
SF
procedura B Registri
SF salvati
procedura C
variabili
locali
$sp
stack
cresce verso
il basso
56
28
3/8/03
58
29
3/8/03
Preservato in u na
chiamata a
No me Numero Uso p ro ced ura?
$zero 0 il valore costante 0 n.a.
$v0-$v1 2-3 valori per risultati di espressioni no
$a0-$a3 4-7 argomenti si
$t0-$t7 8-15 temporanei no
$s0-$s7 16-23 salvati si
$t8-$t9 24-25 altri temporanei no
$gp 28 global pointer si
$sp 29 stack pointer si
$fp 30 frame pointer si
$ra 31 return address si
59
• procedure ricorsive
– foglia
• procedure che non effe ttuano chiamate
60
30
3/8/03
Caller:
add $a0,$s1,$zero
add $a1,$s2,$zero
jal swap
61
} jr $ra
62
31
3/8/03
Funzione ricorsiva
Fattoriale : n! = n* (n-1)!
0! = 1
63
Funzione ricorsiva
instruction
address
1000 fact: addi $sp,$sp,-8
1004 sw $ra,4($sp)
1008 sw $a0,0($sp)
1012 slti $t0,$a0,1 # test if n<1
1016 beq $t0,$zero,L1 # if n>=1 goto L1
1020 addi $v0,$zero,1 # return 1
1024 addi $sp,$sp,8 #
1032 jr $ra
1036 L1: addi $a0,$a0,-1
1040 jal fact # call fact with (n-1)
1044 lw $a0,0($sp)
1048 lw $ra,4($sp)
1052 addi $sp,$sp,8
1056 mul $v0,$a0,$v0 # return n*fact(n-1)
1060 jr $ra
64
32
3/8/03
Funzione ricorsiva
low address
$a0 = 0 $v0 = 1
$ra = 1044
$a0 = 1 100 addi $a0,$zero,3
$v0 = 1
$ra = 1044
$a0 = 2 104 jal fact
$v0 = 2
$ra = 1044 108 ....
$a0 = 3
$sp $v0 = 6
$ra =108
filled
high address
65
Istruzioni Logiche
Istruzione Esempio Significato Commento
and and $s1,$s2,$s3 $s1 = $s2 & $s3 3 reg. operands; Logical AND
or or $s1,$s2,$s3 $s1 = $s2 | $s3 3 reg. operands; Logical OR
xor xor $s1,$s2,$s3 $s1 = $s2 ^ $s 3 3 reg. operands; Logical XOR
nor nor $s1,$s2,$s3 $s1 = ~($s2 |$s 3) 3 reg. operands; Logical NOR
and immediato andi $s1,$s2,10 $s1 = $s2 & 10 Logical AND reg, constant
or immediato ori $s1,$s2,10 $s1 = $s2 | 10 Logical OR reg, constant
xor immediato xori $s1, $s2,10 $s1 = ~$s2 &~10 Logical XOR reg, constant
shift left logical sll $s1,$s2,10 $s1 = $s2 << 10 Shift left by constant
shift right logical srl $s1,$s2,10 $s1 = $s2 >> 10 Shift right by constant
shift right arithm. sra $s1,$s2,10 $s1 = $s2 >> 10 Shift right (si gn extend)
shift left logical sllv $s1,$s2,$s3 $s1 = $s2 << $s3 Shift left by variable
shift right logical srlv $s1,$s2, $s3 $s1 = $s2 >> $s3 Shift right by variable
shift right arithm. srav $s1,$s2, $s3 $s1 = $s2 >> $s3 Shift right arith. by variable
66
33
3/8/03
Costanti
• Le costanti sono utilizzati frequentemente (50% degli
operandi). Esempio: A = B + 5;
• Possibile soluzione:
– mettere le costanti in memoria e caricarle nei registri
67
0xaabb 0xccdd
34
3/8/03
• Istruzioni:
bne $t4,$t5,Label #Salta a Label se $t4!=$t5
beq $t4,$t5,Label #Salta a Label se $t4==$t5
j Label #Salta Label
J op 26 bit address
70
35
3/8/03
72
36
3/8/03
Intervallo ± 32 Kb
dalla base
73
74
37
3/8/03
75
Pseudoistruzioni
76
38
3/8/03
Pseudo Istruzioni
• aritmetiche
– abs, neg, negu, mul, mlo, mlou, div, divu, rem, remu
• logiche
– not
– rol, ror
• di trasferimento dati
– la, ld, ulh, ulhu, ulw, li
– sd, ush, usw
– move
• di confronto
– seq, sge, sgeu, sgt, sgtu, sle, sleu, sne
77
Pseudo Istruzioni
• di controllo
– b, beqz, bge, bgeu, bgt, bgtu, ble, bleu, blt, bltu, bnez
• dei coprocessori
– mfc1.d
• floating-point
– li.s(.d), l.s(.d),s.s(.d)
78
39
3/8/03
Pseudoistruzioni: esempi
• la $4, label
lui $1, UpperPartLabelAddress
ori $4, $1, LowerPartLabelAddress
• bge $8,$9,address
slt at,$8,$9
beq at,$zero, address
• move $8,$10
addu $8, $0, $10
79
Pseudoistruzioni: esempi
80
40
3/8/03
Pseudo-Indirizzam ento
81
Riassum endo
MIPS operands
Name Example Comments
$s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform
32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is
$fp, $sp, $ra, $at reserved for the assembler to handle large constants.
Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so
230 memory Memory[4], ..., sequential words differ by 4. Memory holds data structures, such as arrays,
words Memory[4294967292] and spilled registers, such as those saved on procedure calls.
82
41
3/8/03
Riassum endo
MIPS assembly language
Category Instruction Example Meaning Comments
add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers
add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load word lw $s1, 100($s2) $s1 = Memory[ $s2 + 100] Word from memory to register
store word sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1, 100($s2) $s1 = Memory[ $s2 + 100] Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper lui $s1, 100 $s1 = 100 * 2
16 Loads constant in upper 16 bits
immediate
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100
branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0
set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0
83
Riassum endo
• Tutte le istruzioni modificano tutti i 32 bits del registro destinazione (incluso
lui, lb, lh) e tutte leggono i 32 bits della sorgente (add, sub, and, or, …)
• Le istruzioni Immediate aritmetiche e logiche sono estese nel seguente modo:
– gli operandi logici immediati sono “zero extended” a 32 bits
– gli operandi aritmetici immediati sono “sign extended” a 32 bits (incluso addu)
• I dati caricati da lb e lh sono estesi nel seguente modo:
– lbu, lhu “zero extended”
– lb, lh “sign extended”
• Overflow puo’ verificarsi con:
– add, sub, addi
• non puo’ verificarsi con:
– addu, subu, addiu, and, or, xor, nor, shifts, mult, multu, div, divu
84
42
3/8/03
Summar y
85
43
3/8/03
44
3/8/03
89
Direttive
90
45
3/8/03
.data, .kdata
91
Global Pointer
• Disponendo di un offset a 16 bit le istruzioni di
load/store non possono accedere direttamente ai dati
statici:
lui $16, 0x1000
lw $2, 0x8000($16)
• Conviene dedicare un registro (per convenzione $28)
come puntatore globale, caricandovi l’indirizzo
0x10008000 (è automatico in SPIM), in modo da
rendere direttamente accessibili i primi 64KB dei dati
statici:
lw $2, 0($28)
92
46
3/8/03
.text, .ktext
93
.space
94
47
3/8/03
.ascii, .asciiz
95
96
48
3/8/03
__start
• E’ l’etichetta che, per default, specifica la prima
istruzione eseguibile del programma
• E’ di solito definita nel trap file, dove è associata al
codice
__start:
. . .
jal main
li $v0 10 # syscall 10 (exit)
syscall
• Nota:
– Codice di Startup e’ pre-caricato.
– Pseudo-istruzioni sono espanse in istruzioni reali.
98
49
3/8/03
99
System call
100
50
3/8/03
Architecture alternative
• Design alternative:
– provide more powerful opera tions
– goal is to reduce number of instructions executed
– danger is a slower cycle time and/or a higher CPI
101
Power PC
• Indexed addressing
– example: lw $t1,$a0+$s3
#$t1=Memory[$a0+$s3]
– What do we have to do in MIPS?
• Update addressing
– update a register as part of load (for marching through arrays)
– example: lwu $t0,4($s3)
#$t0=Memory[$s3+4];$s3=$s3+4
– What do we have to do in MIPS?
• Others:
– load multiple/store multiple
– a special counter register “bc Loop”
– decrement counter, if not 0 goto loop
102
51
3/8/03
80x86
• 1978: The Intel 8086 is announced (16 bit architecture )
• 1980: The 8087 floating point coprocessor is added
• 1982: The 80286 increases address space to 24 bits, +instructions
• 1985: The 80386 extends to 32 bits, new addressing modes
• 1989-1995: The 80486, Pentium, Pentium Pro add a few
instructions (mostly designed for higher performance)
• 1997: MMX is added
– “This history illustrates the impact of the “golden handcuffs” of
compatibility
103
52
3/8/03
Registri 80386
105
Istruzioni 80x86
106
53
3/8/03
107
esempio
108
54
3/8/03
esempio
109
esempio
# EXAMP LE II: L oad an i nte ger from the dat a seg men t
# and prin t it.
lw $a0, item0 # Load the valu e of ite m0
# into $a0
li $v0, 1 # Set $ v0 to 1, this tells
# sysca ll to print the
# integ er speci fie d by $a0
syscall # Now p rin t the in teger
110
55
3/8/03
esempio
111
esempio
# EXAMP LE IV: Prin t an ele ment fro m an int eger arr ay
# in the dat a seg men t.
li $t0, 3 # Set t 0 t o 3, the inde x of
# the elem ent w e are fe tch ing
li $t1, 4 # Set t 1 t o 4, thi s is the
# size in bytes of an e lem ent
mul $t2 , $ t1, $ t0 # t2 = t1 * t0, so t2 i s t he
# BYTE off set o f t he el eme nt
lw $a0, array0( $t2 ) # L oad the ele ment of the
# array ; n ote that the fir st
# eleme nt in the a rray has
# an of fse t of zer o
li $v0, 1 # Set $ v0 to 1, this tells
# sysca ll to print the
# integ er speci fie d by $a0
syscall # Now p rin t the in teger
112
56
3/8/03
esempio
113
esempio
C MIPS
=== ======
global var .data
x = y; lw $t0, y
sw $t0, x
x = y + z; lw $t0, y
lw $t1, z
add $t0, $t0, $t1
sw $t0, x
char s[] = "hello\n"; s: .asciiz "hello\n"
x = s[2]; li $t0, 2
lb $t1, s($t0)
x = s[y]; lw $s0, y
lb $s1, s($s0)
114
57