.text
	j main

##include <stdio.h>
#
##define TRUE 1
##define FALSE 0
#
.data
#// The values to be sorted
#int values[10];
values:
	.space 40
#
#// How many values are in the values array
#int valc;
valc:
	.word
#
.text
#// Swap two parameters passed by reference
#void swap( int * x, int * y ) {
#  int temp;
#  
swap:
#  temp = *x;
	lw $t0,($a0)
	
#  * x = * y;
	lw $t1,($a1)
	sw $t1,($a0)
	
#  y = * temp;
	sw $t0,($a1)
#}
	jr   $ra#
	
	
#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

#}
	
#// Takes up to 10 numeric arguments and prints them out in sorted order
#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++ ) {
	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
#
#  // BUBBLE SORT!
#
#  // Used to remember whether or not anything is swapped duirng a pass
#  int outOfOrder = TRUE;     Keep in $s3
#	li $s3,1

#  while ( outOfOrder ) {
bubbleLoopStart:
	bne	$s3,$0,bubbleLoopEnd
	
#    outOfOrder = FALSE;
	li $s3,0
	
#    for ( p = 0; p < valc - 1; p++ ) {
	li $s2,0
sweepLoopStart:
	add $t0,$s0,-1
	slt $t0,$s2,$t0
	beq $t0,$0,sweepLoopEnd
	
#      if ( values[p] > values[p+1] ) {
	move $t0,$s2
	sll  $t0,$t0,2
	la   $t1,values
	add  $t1,$t1,$t0
	lw   $t0,($t1)
	lw   $t2,4($t1)
	slt  $t0,$t2,$t0
	beq  $t0,$0,sweepIfJoin
	
#	outOfOrder = TRUE;
	li	$s3,1
	
#	swap( &values[p], &values[p+1] );
	move  $a0,$t1
	addi  $a1,$t1,4
	jal   swap
#      }
sweepIfJoin:
	addi  $s2,$s2,1
	j sweepLoopStart
sweepLoopEnd:
	
	j bubbleLoopStart
#    }
bubbleLoopEnd:
#  }
#
#  // Print the result
#  for ( p = 0; p < valc; p++ ) {    

	li	$s2,0
printLoopStart:
	slt	$t0,$s2,$s0
	beq	$t0,$0,printLoopEnd
	
	
#    printf( "%d ", values[p] );
	li	$v0,1
	move	$t0,$s2
	sll	$t0,$t0,2
	la	$t1,values
	add	$t0,$t0,$t1
	lw	$a0,($t0)
	syscall
	
	li	$v0,4
	la	$a0,blank
	syscall

	addi	$s2,$s2,1
	j printLoopStart
printLoopEnd:
#  }
#
#  printf( "\n" );
	li	$v0,4
	la	$a0,newLine
	syscall
#
#  
#}
	li	$v0,10
	syscall
.data

blank:	.asciiz	" "

newLine: .asciiz "\n"
