# ifelse.s .text .globl main main: lw $t0,x # esempio di codice opzionale blez $t0,endif1 # se x <= 0 salto il codice opzionale mul $t1,$t0,2 # codice opzionale: sw $t1,y # y = x*2 endif1: # riprende il codice comune lw $t0,x # esempio di scelta a due rami blez $t0,else2 # se x <= 0 salto al ramo else mul $t1,$t0,2 # ramo then: $t1 = x*2 j endif2 else2: neg $t1,$t0 # ramo else: $t1 = -x endif2: sw $t1,y # salvo $t1 in y # soluzione alternativa con i due rami scambiati lw $t0,x # esempio di scelta a due rami bgtz $t0,then3 # se x > 0 salto al ramo then neg $t1,$t0 # ramo else: $t1 = -x j endif3 then3: mul $t1,$t0,2 # ramo then: $t1 = x*2 endif3: sw $t1,y # salvo $t1 in y lw $t0,x # esempio di scelta annidata lw $t1,y blez $t0,elsec bltz $t1,elsea mul $t1,$t0,2 j endif4 elsec: neg $t1,$t0 j endif4 elsea: addi $t1,$t1,1 endif4: sw $t1,y .data x: .word 5 # provare con 5 e con -5 y: .word 0