Il linguaggio assembly Strutture di controllo P.H. cap. 2.6 1 Argomenti Organizzazione della memoria Istruzioni di trasferimento dei dati Array Le strutture di controllo Istruzioni di salto if then do... while while for switch Pseudoistruzioni 2 1
Le strutture di controllo Queste istruzioni alterano l'ordine di esecuzione delle istruzioni: La prossima istruzione da eseguire non è l'istruzione successiva all'istruzione corrente Permettono di eseguire cicli e condizioni In assembly le strutture di controllo sono molto semplici e primitive 3 Istruzioni di salto condizionato Istruzioni di salto: viene caricato un nuovo indirizzo nel contatore di programma (PC) ( invece dell'indirizzo dell istruzione seguente) Istruzioni di salto condizionato (conditional branch): il salto viene eseguito solo se una certa condizione risulta soddisfatta Esempi: beq (branch on equal) e bne (branch on not equal) beq r1, r2, L1 # go to L1 if (r1 == r2) bne r1, r2, L1 # go to L1 if (r1!= r2) 4 2
Istruzioni di salto incondizionato Istruzioni di salto incondizionato ( unconditional jump): il salto viene sempre eseguito. Esempi: j (jump) e jr (jump register) e jal (jump and link) j L1 # go to L1 jr r31 # go to add. contained in r31 jal L1 # go to L1. Save add. of next # instruction in reg. Ra 5 Esempio if... then Codice C: if (i == j) f=g+h; if if (condition) clausola-1 f in in $s0 $s0 g in in $s1 $s1 if if (( NOTcondition )) goto goto Lendif; h in in $s2 $s2 clausola-1; i i in in $s3 $s3 Lendif: j j in in $s4 $s4 Codice MIPS: bne $s3, $s4, Lendif # go to Lendif if i j add $s0, $s1, $s2 # f=g+h (saltata if i j) Lendif: 6 3
Esempio if... then... else (1 a versione) Codice C o Java: if (i==j) f=g+h; else f=g-h; Codice MIPS: if if (condition) clausola-1 else else clausola-2 if if (NOTcondition) goto goto Lelse; clausola-1; go go to tolendif; Lelse: clausola-2; Lendif: bne $s3, $s4, Else # if i j go to Else add $s0, $s1, $s2 # f=g+h (skipped if i j) j Endif # go to Endif Else: sub $s0, $s1, $s2 # f=g-h (skipped if i = j) Endif: 7 if... then... else (2 a versione) Codice C: if (i==j) f=g+h; else f=g-h; Codice MIPS: if if (condition) clausola-1 else else clausola-2 if if (condition) goto goto Ltrue; clausola-2; go go to tolendif; Ltrue: clausola-1; Lendif: beq $s3,$s4,true sub $s0,$s1,$s2 j Endif True: add $s0,$s1,$s2 Endif: # branch i==j # f=g-h(false) # go to Endif # f=g+h (true) f in in $s0 $s0 g in in $s1 $s1 h in in $s2 $s2 i i in in $s3 $s3 j j in in $s4 $s4 8 4
Esempio: do while (1) Codice C: do { g = g + A[i]; i = i + j; } while (i!= h) Riformulata con "goto" Loop: g = g + A[i]; i = i + j; if (i!= h) goto Loop; 9 Esempio: do while (2) Codice MIPS: g in in $s1 $s1 h in in $s2 $s2 addr.a in in $s6 $s6 i i in in $s3 $s3 j j in in $s4 $s4 Loop: add $t1, $s3, $s3 # $t1 <- 2 * i add $t1, $t1, $t1 # $t1 <- 4 * i add $t1, $t1, $s6 # $t1 <- addr. of A[i] lw $t0, 0($t1) # $t0 <- A[i] add $s1, $s1, $t0 # g <- g + A[i] add $s3, $s3, $s4 # i <- i + j bne $s3, $s2, Loop # go to Loop if i h 10 5
while Codice C: i while (A[i] == k) i in in $s3 $s3 j i = i + j; j in in $s4 $s4 k in in $s5 $s5 addr.a in in $s6 Codice MIPS: $s6 Loop: add $t1, $s3, $s3 # $t1 2 * i add $t1, $t1, $t1 # $t1 4 * i add $t1, $t1, $s6 # $t1 addr. of A[i] lw $t0, 0($t1) # $t0 A[i] bne $t0, $s5, Exit # go to Exit if A[i] k Exit: add $s3, $s3, $s4 j Loop # i i + j # go to Loop 11 Registro $zero Spesso la verifica di uguaglianza richiede il confronto con il valore 0 Per rendere più veloce il confronto, in MIPS il registro $zero contiene sempre il valore 0 Altro tipo d uso di $zero: g = h; // (in C) g in in $s1 $s1 h in in $s2 $s2 add $s1, $s2, $zero # $s1 $s2 pseudoistruzione move $s1, $s2 # $s1 $s2 12 6
Confronto su minore/maggiore Spesso è utile condizionare l'esecuzione di una istruzione al fatto che una variabile sia minore (maggiore) di una altra: slt $t0, $s3, $s4 # set on less than se $s3 < $s4 assegna il valore 1 a $t0 altrimenti assegna il valore 0 a $t0 Con slt, beq e bne si possono implementare tutti i test sui valori di due variabili (==,!=, <, <=, >, >=) 13 Esempio (if) if (i < j) k = i + j; else k = i - j; i i in in $s3 $s3 j j in in $s4 $s4 k in in $s5 $s5 slt $t0, $s3, $s4 # $t0=1 se(i<j) beq $t0, $zero, Else # goto se NOT(i<j) add $s5, $s3, $s4 # k = i + j; (i<j) j Exit Else: sub $s5, $s3, $s4 # k = i - j; (i>=j) Exit: 14 7
Esempio (for) Codice C Codice MIPS for for (i=0; i<n; i++) { //CORPO DEL DEL CICLO } i i in in $s0 $s0 n in in $s1 $s1 move $s0, $zero # i=0 for: slt $t0, $s0, $s1 # $t0=0 se i >= n beq $t0, $zero, endfor # esce se i >= n # CORPO DEL CICLO addi $s0, $s0, 1 # i++ j for endfor: # istruz. succ. 15 switch Può essere implementata mediante una serie di if-then-else Alternativa: uso di una jump address table cioè di una tabella che contiene una serie di indirizzi di istruzioni alternative 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; } 16 8
Switch (if.. elseif ) if(k==0) f=i+j; else if(k==1) f=g+h; else if(k==2) f=g h; bne $s5,$0,l1 # branch k!=0 else if(k==3) f=i j; add $s0,$s3,$s4 # k==0 so f=i+j j Exit # end of case so Exit L1: addi $t0,$s5,-1 # $t0=k-1 bne $t0,$0,l2 # branch k!=1 f in in $s0 $s0 add $s0,$s1,$s2 # k==1 so f=g+h g in in $s1 $s1 j Exit # end of case so Exit h in in $s2 $s2 L2: addi $t0,$s5,-2 # $t0=k-2 i i in in $s3 $s3 bne $t0,$0,l3 # branch k!=2 j j in in $s4 $s4 sub $s0,$s1,$s2 # k==2 so f=g-h k in in $s5 $s5 j Exit # end of case so Exit L3: addi $t0,$s5,-3 # $t0=k-3 bne $t0,$0,exit # branch k!=3 sub $s0,$s3,$s4 # k==3 so f=i-j Exit: 17 Argomenti Le strutture di controllo Istruzioni di salto if then do... while while switch Pseudoistruzioni 18 9
Pseudoistruzioni Per semplificare la programmazione, MIPS fornisce un insieme di pseudoistruzioni Le pseudoistruzioni sono un modo compatto ed intuitivo di specificare un insieme di istruzioni Non hanno un corrispondente 1 a 1 con le istruzioni dell'isa La traduzione della pseudoistruzione nelle istruzioni equivalenti è attuata automaticamente dall'assemblatore 19 Esempio pseudo istruzioni move $t0, $t1 add $t0, $zero, $t1 mul $s0, $t1, $t2 mult $t1, $t2 mflo $s0 div $s0, $t1, $t2 div $t1, $t2 mflo $s0 20 10
Pseudo istruzione load address la $t0, jtab la $s3, vet la $t4, vet($s5) se $s5 = 12 = 3*4 $t4 = addr. vet[3] $S3 + 0 $S3 + 4 $S3 + 8 $S3 + 12 vet[0] vet[1] vet[2] vet[3] 21 Branch vari (pseudoistruzioni) bge rs, rt, target Branch to instruction at target if rs >= rt bgt rs, rt, target Branch to instruction at target if rs > rt ble rs, rt, target Branch to instruction at target if rs <= rt blt rs, rt, target Branch to instruction at target if rs < rt es. blt $s1, $s2, label equiv. slt $at, $s1, $s2 bne $at, $zero, label es. ble $s1, $s2, label equiv. slt $at, $s2, $s1 #$at = 1 se $s2<$s1 beq $at, $zero, label #branch se $s1<=$s2 22 11
Branch vari beq rs, rt, target Branch to instruction at target if rs = rt bne rs, rt, target Branch to instruction at target if rs!= rt bgez rs, target Branch to instruction at target if rs >= 0 bgtz rs, target Branch to instruction at target if rs > 0 blez rs, target Branch to instruction at target if rs <= 0 bltz rs, target Branch to instruction at target if rs < 0 beqz rs, target Branch to instruction at target if rs = 0 bnez rs, target Branch to instruction at target if rs!= 0 23 Branch vari bge rs, rt, target Branch to instruction at target if rs >= rt bgeu rs, rt, target Branch to instruction at target if rs >= rt (unsigned) bgt rs, rt, target Branch to instruction at target if rs > rt bgtu rs, rt, target Branch to instruction at target if rs > rt (unsigned) ble rs, rt, target Branch to instruction at target if rs <= rt bleu rs, rt, target Branch to instruction at target if rs <= rt (unsigned) blt rs, rt, target Branch to instruction at target if rs < rt bltu rs, rt, target Branch to instruction at target if rs < rt (unsigned) 24 12
25 13