  # Name: Liam Kirsh, Kevin Yang
  # Section: 1
  # Description: Write a function which divides a
  # 64-bit unsigned number with a 31-bit unsigned number.

  # void divide(unsigned int p1, unsigned int p2, unsigned int divi){
  #    unsigned int tempD = divi >> 1;
  #    int tf = 0;
  #    unsigned msbSet = 1 << (sizeof(unsigned int) * 8 - 1);
  #    while(tempD){    
  #       if(p1 & 0x1)
  #          tf = 1;
  #       else
  #          tf = 0;
  #       p1 >>= 1;
  #       p2 >>= 1;
  #       if(tf)
  #          // 8 bits in one byte
  #          p2 |= msbSet;
  #          //0x80000000; or like 10000...
  #       else
  #          p2 &= ~1 >> 1;
  #          //0x7FFFFFFF; or like 01111...
  #       tempD >>= 1;
  #    }
  #    printf("\n%d %d\n", p1, p2);
  # }

# global addresses for programmmers
.globl welcome
.globl prompt
.globl firstInt
.globl secondInt

.data

welcome:
   .asciiz "Divide a 64 bit number represented as two 32 bit with a 31 bit number.\n"

prompt:
   .asciiz "\nInput an integer:"

firstInt:
   .asciiz "\nUpper integer is:"

secondInt:
   .asciiz "\nLower integer is:"

.text

main:
# Show the welcome message (load 4 into $v0 for display)

ori $v0, $0, 4

# generates starting address for welcome message.
# assues register first contains 0

lui $a0, 0x1001
syscall

# Get the upper bound 32 bits
jal getInteger
add $s0, $v0, $0

# Get the lower bound 32 bits
jal getInteger
add $s1, $v0, $0

# Get the 31 bit integer
jal getInteger
add $s2, $v0, $0

# to determine whether to carry over
add $t1, $0, $0

# mask
addi $t2, $0, 1
sll $t2, $t2, 31

srl $s2, $s2, 1

divLoop:
   andi $t3, $s0, 1
   srl $s0, $s0, 1
   srl $s1, $s1, 1
   beq $t3, $0, ignoreOr
   or $s1, $s1, $t2
ignoreOr:
   srl $s2, $s2, 1
   bne $s2, $0, divLoop

#Display message
ori $v0, $0, 4
lui $a0, 0x1001
ori $a0, $a0, 0x5b
syscall

add $a0, $s0, $0
jal returnInteger

#Display message
ori $v0, $0, 4
lui $a0, 0x1001
ori $a0, $a0, 0x6e
syscall

add $a0, $s1, $0
jal returnInteger

ori $v0, $0, 10 #10 in $v0 exists
syscall

returnInteger:
   # display the sum, load 1 into $v0 to display
   ori $v0, $0, 1
   syscall
   jr $ra

getInteger:
   ori $v0, $0, 4 # 4 to display prompt
   # load starting address of prompt
   lui $a0, 0x1001
   ori $a0, $a0, 0x48
   syscall

   ori $v0, $0, 5 # Load 5 into $v0 for input
   syscall
   jr $ra
