# int *initArray(int size, int init)
# {int i;
#  int *a = (int *)malloc(size*sizeof(int));
#  for(i=0;ilength!=t->length) return 0;
#  for(i=0;ilength;i++) if (s->chars[i]!=t->chars[i]) return 0;
#  return 1;
# }

string:
	beq $a2, 5, string_ne
	lbu $t0, ($a0)
	lbu $t1, ($a1)
	#beqz $t0, due
	#beqz $t1, no_tot
	j comp
comp_esci:	
	beq $v1, 0, no_tot
	addi $a0, $a0, 1
	addi $a1, $a1, 1
	b string
string_ne:
	lbu $t0, ($a0)
	lbu $t1, ($a1)
	beqz $t0, due2
	beqz $t1, si_tot
	bne $t0, $t1, si_tot
	addi $a0, $a0, 1
	addi $a1, $a1, 1
	b string_ne
due2:
	beqz $t1, no_tot
	j si_tot
no_tot:
	li $v1, 0
	j $ra
due:
	beqz $t1, si_tot
	j no_tot
si_tot:
	li $v1, 1
	j $ra
comp:
	beq $a2, 4, eq
	beq $a2, 6, lt
	beq $a2, 7, le
	beq $a2, 8, gt
	beq $a2, 9, ge
eq:
	beqz $t0, due
	beqz $t1, no
	bne $t0, $t1, no
	j si
gt:
	beqz $t0, no
	beqz $t1, si
	ble $t0, $t1, no
	j si
lt:
	beqz $t1, no
	beqz $t0, si
	bge $t0, $t1, no
	j si
le:
	beqz $t0, si
	beqz $t1, no
	bgt $t0, $t1, no
	j si
ge:
	beqz $t1, si
	beqz $t0, no
	blt $t0, $t1, no
	j si
si:	
	li $v1, 1
	j comp_esci
no:
	li $v1, 0
	j comp_esci


# 
# void print(struct string *s)
# {int i; unsigned char *p=s->chars;
#  for(i=0;ilength;i++,p++) putchar(*p);
# }

print:
move $a0, $a1
li $v0, 4
syscall
j $ra

print_int:
move $a0, $a1
li $v0, 1
syscall
j $ra

input_int:
li $v0, 5
syscall
move $v1, $v0
j $ra

input:
addi $a1, $a1, 1
li $t0, 4
div $a1, $t0
mfhi $t1
sub $a0, $t0, $t1
add $a0, $a1, $a0
li $v0, 9
syscall
move $a0, $v0
li $v0, 8
syscall
move $v1, $a0
j $ra

# void flush()
# {
#  fflush(stdout);
# }

flush:
j $ra

# int main()
# {int i;
#  for(i=0;i<256;i++)
#    {consts[i].length=1;
#     consts[i].chars[0]=i;
#    }
#  return t_main(0 /* static link */);
# }

#int ord(struct string *s)
#{
# if (s->length==0) return -1;
# else return s->chars[0];
#}

ord:
lbu $v1,($a1)
bnez $v1,Lrunt5
li $v1, -1
Lrunt5:
j $ra


# struct string empty={0,""};

# struct string *chr(int i)
# {
#  if (i<0 || i>=256) 
#    {printf("chr(%d) out of range\n",i); exit(1);}
#  return consts+i;
# }

.data
Lrunt30: .asciiz "character out of range\n"
.text

chr:
bgt $a1, 255, Lrunt31
sll $a1, $a1, 1
la   $v1,Runtconsts($a1)
j $ra
Lrunt31:
li   $v0,4
la   $a0,Lrunt30
syscall
li   $v0,10
syscall

# int size(struct string *s)
# { 
#  return s->length;
# }

size:
li $v1, 0
loop:
lbu $a2, ($a1)
beqz $a2, zero
addi $a1, $a1, 1
addi $v1, $v1, 1
j loop
zero:	
j $ra

# struct string *substring(struct string *s, int first, int n)
# {
#  if (first<0 || first+n>s->length)
#    {printf("substring([%d],%d,%d) out of range\n",s->length,first,n);
#     exit(1);}
#  if (n==1) return consts+s->chars[first];
#  {struct string *t = (struct string *)malloc(sizeof(int)+n);
#   int i;
#   t->length=n;
#   for(i=0;ichars[i]=s->chars[first+i];
#   return t;
#  }
# }
# 
.data
Lrunt40:  .asciiz "substring out of bounds\n"
.text
substring:
move $t0, $ra
move $t1, $a1
move $t2, $a2
jal size
move $ra, $t0
move $a1, $t1
move $a2, $t2
bltz $a2, esci
add $t0, $a2, $a3
bgt $t0, $v1, esci
addi $a0, $a3, 1
li $t0, 4
div $v1, $t0
mfhi $t1
sub $a0, $t0, $t1
add $a0, $v1, $a0
li $v0, 9
syscall
move $v1, $v0
li $t2, 0
loop2:	
bge $t2, $a3, fine
add $t1, $a1, $a2
add $t3, $v0, $t2
lbu $t0, ($t1)
sb $t0, ($t3)
addi $t2, $t2, 1
add $a2, $a2, 1
j loop2
fine:
sb $zero, 1($t3)
j $ra
esci:
li $v0, 4
la $a0, Lrunt40
syscall
li $v0, 10
syscall



# struct string *concat(struct string *a, struct string *b)
# {
#  if (a->length==0) return b;
#  else if (b->length==0) return a;
#  else {int i, n=a->length+b->length;
#        struct string *t = (struct string *)malloc(sizeof(int)+n);
#        t->length=n;
#        for (i=0;ilength;i++)
# 	 t->chars[i]=a->chars[i];
#        for(i=0;ilength;i++)
# 	 t->chars[i+a->length]=b->chars[i];
#        return t;
#      }
# }

.text
concat:
move $t0, $a1
move $t1, $a2
move $t9, $ra
jal size
move $t2, $v1
move $a1, $t1
jal size
move $ra, $t9
move $a1, $t0
move $a2, $t1
move $t1, $v1
add $t0, $t2, $t1
addi $t0, $t0, 1
li $t1, 4
div $t0, $t1
mfhi $t2
sub $a0, $t1, $t2
add $a0, $t0, $a0
li $v0, 9
syscall
move $v1, $v0
loop3:
beqz $t2, str_due
lbu $t3, ($a1)
sb $t3, ($v0)
addi $v0, $v0, 1
addi $a1, $a1, 1
addi $t2, $t2, -1
j loop3
str_due:
beqz $t1, esci2
move $a1, $a2
move $t2, $t1
move $t1, $zero
j loop3
esci2:
sb $zero, ($v0)
j $ra

# int not(int i)
# { return !i;
# }
# 

_not:
seq $v1,$a0,0
j $ra


# #undef getchar
# 
# struct string *getchar()
# {int i=getc(stdin);
#  if (i==EOF) return ∅
#  else return consts+i;
# }

.data
getchbuf: .space 200
getchptr: .word getchbuf

.text
getchar:
lw  $a0,getchptr
lbu $v0,($a0)
bnez $v0,Lrunt6
li $v0,8
la $a0,getchbuf
li $a1,200
syscall
la $a0,getchbuf
lbu $v0,($a0)
bnez $v0,Lrunt6
li $v0,-1
Lrunt6:
move $t0, $a0
move $t1, $a0
li $a0, 4
li $v0, 9
syscall
lb $t0, ($t0)
sb $t0, ($v0)
sb $zero, 1($v0)
move $v1, $v0
add $t1,$t1,1	
sw $t1,getchptr
j $ra

.data
Runtconsts: 
.space 512
Runtempty: .word 0
.text

main: 
li $a0,0
la $a1,Runtconsts
li $a2,1
Lrunt20:
sb $a0,($a1)
sb $zero, 1($a1)
addiu $a1,$a1,2
addi $a0, $a0, 1
slt $a3,$a0,256
bnez $a3,Lrunt20
li $a0,0
j t_main
is_digit_3:
move $fp, $sp
addi $sp, $sp, -60

.text
L50:
sw $a1, -8($fp)
sw $a0, ($fp) 
move $a0, $fp
lw $a1, -8($fp)
sw $ra, -28($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -28($fp)
sw $v1, -32($fp)
move $a0, $fp
la $a1, L6
sw $ra, -16($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -16($fp)
lw $t9, -32($fp)
bge $t9, $v1, L8 
L9:
li $s7, 0
L10:
move $v1, $s7 
j L49 
L8:
li $t9, 1
sw $t9, -36($fp)
move $a0, $fp
lw $a1, -8($fp)
sw $ra, -20($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -20($fp)
sw $v1, -24($fp)
move $a0, $fp
la $a1, L7
sw $ra, -12($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -12($fp)
lw $s7, -24($fp)
ble $s7, $v1, L11 
L12:
li $s7, 0
sw $s7, -36($fp)
L11:
lw $s7, -36($fp)
j L10 
L49:
nop
addi $sp, $sp, 60
lw $fp, 4($sp)
j $ra

skip_white_spaces_4:
move $fp, $sp
addi $sp, $sp, -40

.text
L52:
sw $a0, ($fp) 
L19:
li $t9, -8
lw $t8, ($fp)
add $t9, $t9, $t8 
lw $a0, ($t9)
la $a1, L14
li $a2, 4
sw $ra, -16($fp)
sw $fp, 4($sp)
jal string 
lw $ra, -16($fp)
bnez $v1, L16 
L17:
li $t9, -8
lw $t8, ($fp)
add $t9, $t9, $t8 
lw $a0, ($t9)
la $a1, L15
li $a2, 4
sw $ra, -20($fp)
sw $fp, 4($sp)
jal string 
lw $ra, -20($fp)
bnez $v1, L20 
L13:
li $v1, 0
j L51 
L20:
lw $t9, ($fp)
addi $t9, $t9, -8
sw $t9, -12($fp)
move $a0, $fp
sw $ra, -8($fp)
sw $fp, 4($sp)
jal getchar 
lw $ra, -8($fp)
lw $t9, -12($fp)
sw $v1, ($t9) 
j L19 
L16:
j L20 
L51:
nop
addi $sp, $sp, 40
lw $fp, 4($sp)
j $ra

get_int_0:
move $fp, $sp
addi $sp, $sp, -84

.text
L54:
sw $a0, ($fp) 
la $t9, L5
sw $t9, -8($fp)
li $t9, 0
sw $t9, -52($fp)
move $a0, $fp
la $a1, L21
sw $ra, -32($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -32($fp)
move $a0, $fp
sw $ra, -36($fp)
sw $fp, 4($sp)
jal skip_white_spaces_4 
lw $ra, -36($fp)
L24:
move $a0, $fp
addi $t9, $fp, -8
lw $a1, ($t9)
sw $ra, -40($fp)
sw $fp, 4($sp)
jal is_digit_3 
lw $ra, -40($fp)
bnez $v1, L25 
L22:
lw $v1, -52($fp)
j L53 
L25:
lw $t9, -52($fp)
mul $t9, $t9, 10
sw $t9, -44($fp)
move $a0, $fp
addi $t9, $fp, -8
lw $a1, ($t9)
sw $ra, -20($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -20($fp)
lw $t9, -44($fp)
add $t9, $t9, $v1 
sw $t9, -48($fp)
move $a0, $fp
la $a1, L23
sw $ra, -24($fp)
sw $fp, 4($sp)
jal ord 
lw $ra, -24($fp)
lw $t9, -48($fp)
sub $t9, $t9, $v1 
sw $t9, -52($fp)
addi $t9, $fp, -8
sw $t9, -28($fp)
move $a0, $fp
sw $ra, -16($fp)
sw $fp, 4($sp)
jal getchar 
lw $ra, -16($fp)
lw $t9, -28($fp)
sw $v1, ($t9) 
j L24 
L53:
nop
addi $sp, $sp, 84
lw $fp, 4($sp)
j $ra

pretty_print_int_1:
move $fp, $sp
addi $sp, $sp, -60

.text
L56:
sw $a1, -32($fp)
sw $a0, ($fp) 
lw $t9, -32($fp)
blt $t9, 10, L28 
L29:
L30:
lw $t9, -32($fp)
blt $t9, 100, L32 
L33:
L34:
lw $t9, -32($fp)
blt $t9, 1000, L36 
L37:
L38:
lw $t9, -32($fp)
blt $t9, 10000, L40 
L41:
L42:
move $a0, $fp
lw $a1, -32($fp)
sw $ra, -24($fp)
sw $fp, 4($sp)
jal print_int 
lw $ra, -24($fp)
move $a0, $fp
la $a1, L43
sw $ra, -28($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -28($fp)
j L55 
L28:
move $a0, $fp
la $a1, L27
sw $ra, -8($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -8($fp)
j L30 
L32:
move $a0, $fp
la $a1, L31
sw $ra, -12($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -12($fp)
j L34 
L36:
move $a0, $fp
la $a1, L35
sw $ra, -16($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -16($fp)
j L38 
L40:
move $a0, $fp
la $a1, L39
sw $ra, -20($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -20($fp)
j L42 
L55:
nop
addi $sp, $sp, 60
lw $fp, 4($sp)
j $ra

print_hello_2:
move $fp, $sp
addi $sp, $sp, -40

.text
L58:
sw $a2, -16($fp)
sw $a1, -12($fp)
sw $a0, ($fp) 
lw $a0, ($fp)
lw $t9, -12($fp)
addi $a1, $t9, 1
sw $ra, -8($fp)
sw $fp, 4($sp)
jal pretty_print_int_1 
lw $ra, -8($fp)
move $a0, $fp
lw $t9, -12($fp)
sll $t9, $t9, 2
lw $t8, -16($fp)
add $t9, $t8, $t9 
lw $a1, ($t9)
sw $ra, -20($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -20($fp)
move $a0, $fp
la $a1, L44
sw $ra, -24($fp)
sw $fp, 4($sp)
jal print 
lw $ra, -24($fp)
j L57 
L57:
nop
addi $sp, $sp, 40
lw $fp, 4($sp)
j $ra

t_main:
move $fp, $sp
addi $sp, $sp, -48

.data
L5: .asciiz " "
L6: .asciiz "0"
L7: .asciiz "9"
L14: .asciiz " "
L15: .asciiz "\n"
L21: .asciiz "Insert an int nuber: "
L23: .asciiz "0"
L26: .asciiz "Hello World"
L27: .asciiz " "
L31: .asciiz " "
L35: .asciiz " "
L39: .asciiz " "
L43: .asciiz "] "
L44: .asciiz "!\n"
L45: .asciiz "Ciao  Mondo"
.text
L60:
move $a0, $fp
sw $ra, -16($fp)
sw $fp, 4($sp)
jal get_int_0 
lw $ra, -16($fp)
sw $v1, -20($fp)
lw $a0, -20($fp)
la $a1, L26
sw $ra, -12($fp)
sw $fp, 4($sp)
jal initArray 
lw $ra, -12($fp)
sw $v1, -8($fp)
lw $t9, -20($fp)
sub $t9, $t9, 1
li $t8, 2
sll $t9, $t9, $t8 
lw $t8, -8($fp)
add $t9, $t8, $t9 
la $t8, L45
sw $t8, ($t9)
li $t9, 0
sw $t9, -24($fp)
L47:
lw $t9, -20($fp)
sub $t9, $t9, 1
lw $t8, -24($fp)
ble $t8, $t9, L48 
L46:
li $v1, 0
j L59 
L48:
move $a0, $fp
lw $a1, -24($fp)
lw $a2, -8($fp)
sw $ra, -4($fp)
sw $fp, 4($sp)
jal print_hello_2 
lw $ra, -4($fp)
lw $t9, -24($fp)
addi $t9, $t9, 1
sw $t9, -24($fp)
j L47 
L59:
nop
addi $sp, $sp, 48
lw $fp, 4($sp)
j $ra