  # Name: Liam Kirsh, Kevin Yang
  # Section: 1
  # Description: The function takes two integers as inputs - a number (num),
  # and a divisor (div).  You are guaranteed that div is a power of 2.  You
  # want the remainder of num / div.
  
  # #include <stdio.h>

  # int main() {
  #    int num, div;
  #    printf(" Please enter numerator: ");
  #    scanf(" %d", &num);
  #    printf(" Please enter divisor: ");
  #    scanf(" %d", &div);

  #    int mask = div - 1;

  #    int answer = (num - div) & mask;
  #    printf("Remainder = %d\n", answer);
  #    return 0;
  # }
  
# declare global so programmer can see actual addresses.
.globl prompt_num
.globl prompt_div
.globl sumText

#  Data Area (this area contains strings to be displayed during the program)
.data

prompt_num:
	.asciiz " Please enter numerator: "
    
prompt_div:
    .asciiz " Please enter divisor: "

sumText: 
	.asciiz " \n Remainder = "

#Text Area (i.e. instructions)
.text

main:

	# printf(" Please enter numerator: ");
	ori     $v0, $0, 4
	lui     $a0, 0x1001
	syscall

	# scanf(" %d", &num);
	ori     $v0, $0, 5
	syscall
    # $v0 now has the value of the first integer

	# Add 1st integer to s0
	addu    $s0, $v0, $s0
	
	# printf(" Please enter divisor: ");
	# 0x1A is hexidecimal for 26 decimal (the length of the previous prompt message)
	ori     $v0, $0, 4			
	lui     $a0, 0x1001
	ori     $a0, $a0, 0x1A
	syscall

	# scanf(" %d", &div);
	ori	$v0, $0, 5	
	syscall
	# $v0 now has the value of the second integer
    
    # Clear $s1 for div storage
    ori     $s1, $0, 0
	
	# Add 2nd integer to div storage
	addu    $s1, $v0, $s1 
    
    # Clear $s2 for mask storage
    ori     $s2, $0, 0
    
    # int mask = div - 1;
    addiu   $s2, $s1, -1
    
    # int answer = (num - div) & mask;
    not $s1, $s1 # flip div bits
    addiu $s1, $s1, 1 # add one to div
    addu $s0, $s0, $s1 # add -div to num
    and $s0, $s0, $s2 # answer = (num - div) & mask

	# printf("Remainder = %d\n", answer);
	ori     $v0, $0, 4			
	lui     $a0, 0x1001
	ori     $a0, $a0, 0x32 # 0x1001 += decimal 50
	syscall
	
	# Display the remainder
	ori     $v0, $0, 1	# load 1 into $v0 to display an integer		
	add 	$a0, $s0, $0
	syscall
	
	# Exit (load 10 into $v0)
	ori     $v0, $0, 10
	syscall
