#Poorak Mody Lab-4 .data #---------strings tobe used in the program------- promptx: .asciiz "\n\nEnter x: " divsans: .asciiz "\n the largest power of 2 that x is divisible by is: " nds: .asciiz "\n not divisible by a power of 2" promptn: .asciiz "\n\nEnter n: " ans: .asciiz "\n n!=" anshi: .asciiz "\n n!(highword)= " anslo: .asciiz "\n n!(lowword) = " .text main: #------calling part of 6.3----- li $v0, 4 #load sys code for printing strings la $a0, promptx #load address of string to be printed syscall #prints the string li $v0, 5 #load sys code for reading ints syscall #reads it move $a0,$v0 #copies $v0 to $a0 beq $a0,$zero,nd #checks if a0 is zero jal divs #jumps & links to procedure divs move $t1,$v0 #copies result of divs into $t1 beq $t1,$zero,nd #branches to nd when x=0 li $v0, 4 #sys code for printing strings la $a0, divsans #load address of string to be printed syscall #prints the string li $v0,1 #load sys code for printing ints move $a0,$t1 #a0=t1 syscall #prints the int j part2a #jump to part 2 #---(prob 6.3)divs checks the power of 2 that x is divisible by divs: move $t0,$zero #setting t0 to zero move $v0,$zero #setting $v0 to zero loop: add $v0,$t0,$zero #copy $t0 -> $v0 and $t1,$a0,1 #and x with 1 beq $t1,1,donelp #branches to donelp if x AND 1 holds true addi $t0,$t0,1 #increments $t0 by 1 srl $a0,$a0,1 #shift register right by one bit(unsigned) j loop #repeat donelp: jr $ra #return to calling part of main nd: li $v0, 4 #sys code for printing strings la $a0, nds #load address of string to be printed syscall #prints the string #---(prob 6.8 a)fact calculates n!--------------------------- part2a: li $v0,4 #load sys code for printing string la $a0,promptn #string to be printed syscall #prompt for n li $v0, 5 #load sys code for reading ints syscall #reads it move $a0,$v0 #copies $v0 to $a0 jal fact #branch and link to fact move $t2,$v0 #copy value in $v0 into $t2 li $v0, 4 #load system code forprinting strings la $a0,ans #load string syscall #print string li $v0,1 #load sys code for printing ints move $a0,$t2 #load value of int to be printed syscall #print int (n!) j part2b #branch to part2 b fact: li $t1,1 #loads 1 into $t1 bgt $a0,$t1,factcc #branches to factcc if n>1 li $v0,1 #returns n jr $ra factcc: addi $sp,$sp,-4 sw $ra,0($sp) #holdig $ra for later addi $sp, $sp, -4 #move stack pointer sw $a0, 0($sp) #store n on stack addi $a0, $a0, -1 #n-1 jal fact #(recursion) calls fact(n-1) lw $ra,4($sp) #bringing back $ra lw $t1,0($sp) #bringing back n addi $sp, $sp,8 #resetting the stack pointer mul $v0,$v0,$t1 # $v0 = $v0*n jr $ra #return to calling part of part2a #---(prob 6.8 b)factdw calculates n! and result is a double word------------------- part2b: li $v0,4 #load sys code for printing string la $a0,promptn #string to be printed syscall #prompt for n li $v0, 5 #load sys code for reading ints syscall #reads it move $a0,$v0 #copies $v0 to $a0 li $v1,1 #initilize li $v0,0 jal factdw #branch and link to fact move $t2,$v0 #copy value in $v0 into $t2 move $t6,$v1 li $v0, 4 #load system code forprinting strings la $a0,anshi #load string syscall #print string li $v0,1 #load sys code for printing ints move $a0,$t2 #load value of int to be printed syscall #print int (n!)hi li $v0, 4 #load system code forprinting strings la $a0,anslo #load string syscall #print string li $v0,1 #load sys code for printing ints move $a0,$t6 #load value of int to be printed syscall #print int (n!)lo j end #end program factdw: li $t1,0 #loads 1 into $t1 bgt $a0,$t1,factdwcc #branches to factcc if n>0 li $v1,1 #returns n li $v0,0 jr $ra factdwcc: addi $sp,$sp,-4 sw $ra,0($sp) #holdig $ra for later addi $sp, $sp, -4 #move stack pointer sw $a0, 0($sp) #store n on stack addi $a0, $a0, -1 #n-1 jal factdw #(recursion) calls fact(n-1) lw $ra,4($sp) #bringing back $ra lw $t1,0($sp) #bringing back n addi $sp, $sp,8 #resetting the stack pointer mult $v0,$t1 # $v0*n mflo $t3 # store low of vo*n in t3 mult $v1,$t1 # $v1*n mflo $t5 # store low of v1*n in t5 mfhi $t4 # store hi of v1*n in t4 add $v0,$t4,$t3 #store in v0 the hi(n*v1)+lo(n*v0) move $v1,$t5 #store in v1 the lo(n*v1) jr $ra #return to calling part of part2a end: #-----------------THE END------------------------------------