.text
	j main

##include <stdio.h>
##include "bools.h"
#
.data
#// The values from the command line to be searched
#int values[10];
values:
	.space 40
#
#// The number of values that were entered 
#int valc;
valc:
	.word
#
.text
#int atoi( char * str ) {   str in $a0
atoi:
#   int result = 0;    result in $t0
	li	$t0,0
#   int p;	       p in $t1
#   for ( p = 0; str[p] != 0; p++ ) 
	li  $t1,0
atoiLoopStart:
	move $t3,$t1
	add  $t3,$t3,$a0
	lb   $t2,($t3)              # str[p] now in $t2
	beq  $t2,$0,atoiLoopEnd
	
#   	if ( 0 <= str[p] && str[p] <= '9' ) {
  	
  	li   $t3,'0'
  	slt  $t3,$t2,$t3
  	bne  $t3,$0,atoiIfElse
  	
#    	  result = result * 10 + str[p]-`'0'`;
	subi	$t2,$t2,'0'
	li 	$t3,10
	mult	$t3,$t0
	mflo	$t0
	add	$t0,$t0,$t2
	j	atoiIfJoin
	
#       } else {
atoiIfElse:
#   	  return result;
	j	atoiLoopEnd
#   	}	
atoiIfJoin:
	addi $t1,$t1,1
	j atoiLoopStart
#    }
atoiLoopEnd:
	move  	$v0,$t0
	jr	$ra

#}
	

#// Determine whether the parameter value can be found in values
#int inList( int val ) {     val in $a0
inList:
#  int p;          use $t0 for p
#
#  for ( p = 0; p < valc; p++ ) {
	li  $t0,0
inListLoopStart:
	slt $t1,$t0,$s0
	beq $t1,$0,inListLoopEnd

#    if ( values[p] == val ) {
	move $t1,$t0
	sll  $t1,$t1,2
	la   $t2,values
	add  $t2,$t1,$t2
	lw   $t1,($t2)
	bne  $t1,$a0,inListIfSkip
#      return TRUE;
	li $v0,1
	jr $ra
#    }
inListIfSkip:
#  }
	addi $t0,$t0,1
	j inListLoopStart
inListLoopEnd:
#  return FALSE;
	li $v0,0
	jr $ra
#}
#
##int main( int argc, char * argv[] ) {   
main:

#  must move argc and argv into $s0 and $s1 repectively
	move $s0,$a0
	move $s1,$a1
	
#  int p;	Keep p in $s2
#
#  for ( p = 0; p < argc; p++ ) {   // in Mars, 0th argument is really the first
	li	$s2,0
convertLoopStart:
	slt  $t0,$s2,$s0
	beq  $t0,$0,convertLoopEnd
	
#    values[p] = atoi( argv[p] );
	move	$t0,$s2
	sll	$t0,$t0,2
	add	$t0,$a1,$t0
	lw	$a0,($t0)
	
	jal	atoi
	
	la	$t0,values
	move	$t1,$s2
	sll	$t1,$t1,2
	add	$t1,$t1,$t0
	sw	$v0,($t1)
	
	addi	$s2,$s2,1
	j 	convertLoopStart
#  }
#
convertLoopEnd:
#  valc = argc;   # In MARS argc does not include program name so keep using $s0

#  // Repeatedly search for numbers entered by the user
#  printf("Enter a number: " );
	li	$v0,4
	la	$a0,enterMess
	syscall
	
# int searchNum;          use $a0
#  scanf( "%d", &searchNum );
	li	$v0,5
	syscall
	
	move $a0,$v0
##  while ( searchNum > 0 ) {
mainLoopStart:
	slt $t0,$0,$a0
	beq $t0,$0,mainLoopEnd
	
	li  $v0,1
	syscall
	
#    if ( inList( searchNum ) ) {
	jal	inList
	beq	$v0,$0,mainIfElse
	
#      printf( "%d is in the list\n", searchNum );
	li  $v0,4
	la  $a0,isInMess
	j   mainIfJoin
#    } else {
mainIfElse:
#      printf( "%d is not in the list\n", searchNum );
	li  $v0,4
	la  $a0,isNotInMess
#    }
mainIfJoin:
	syscall
	
#    printf("Enter a number: " );
	li	$v0,4
	la	$a0,enterMess
	syscall
	
#    scanf( "%d", &searchNum );    leave searchNum in $a0 
	li	$v0,5
	syscall
	
	move $a0,$v0
#
#  }
	j mainLoopStart
mainLoopEnd:
	li	$v0,10
	syscall
#}

.data
enterMess:	
	.asciiz "Enter a number: "
	
isInMess:
	.asciiz " is in the list\n"
	
isNotInMess:
	.asciiz " is not in the list\n"
