Program requests and reads in exactly one character, which represents an exponent. The program calculates the base 2 power, and then prints the calculated value.
# Assignment 2: Powers of 2 Assembly # by Hanly De Los Santos # Copyright ©2009, Hanly De Los Santos. All Rights Reserved. # http://www.hdelossantos.com # # Program requests and reads in exactly one character, # which represents an exponent. The program calculates # the base 2 power, and then prints the calculated value. # .text .globl __start # Load acceptable ascii decimal value for number ranges # since input is read in as a character. __start: li $8, 48 # This is the decimal equivalent of 0 li $9, 57 # This is the decimal equivalent of 9 # Prompt the user for input. la $11, prompt puts $11 # Grab user input and store value in register 10 getc $10 la $11, newline # Create new line lb $17, ($11) putc $17 add $15, $10, 0 # Make copy of contents of r10 in r15. sub $15, $15, 48 # Convert char to int. # Test to determine whether or not the user input value is acceptable. # If user input is less than 48 and greater than 57, branches to # exit_donothing, where the user is told the input is wrong. bgt $10, $9, exit_donothing blt $10, $8, exit_donothing sub $10, $10, 48 # After check, convert char to int. # If the value was acceptable, proceed to calculate the power. # Checks to determine if $10 is 0 or 1 since they are special cases. # If value is 0 or 1, branches to their respective prints. li $11, 2 # Load multiplier value into $11. beqz $10, result_zero li $17, 1 beq $10, $17, result1 li $17, 2 # Initialize 17 to multiplier value sub $10, $10, 1 # Since 2^1 has been taken care of # Multiplication loop. While $10 != 0 keep multiplying $17 by 2, and # decrement $10 by 1 every time this is done. Once $10 == 0, print_result. multiply_for: mult $17, $11 mflo $17 sub $10, $10, 1 bnez $10, multiply_for beqz $10, print_result # Display the value of the exponential function. print_result: la $14, str3 puts $14 # Print out "2^" li $v0, 1 move $a0, $15 syscall # Print out value of user input. la $16, str4 puts $16 # Print out '=' li $v0, 1 move $a0, $17 syscall # Print out result. la $13, newline # Create new line lb $12, ($13) putc $12 b endofline # Print out the result for 2 to the power of 0 (special case). result_zero: la $14, str3 puts $14 # Print out "2^" li $v0, 1 move $a0, $15 syscall # Print out value of user input. la $16, str4 puts $16 # Print out '=' li $v0, 1 li $a0, 1 syscall la $13, newline # Create new line lb $12, ($13) putc $12 b endofline # Print the result for 2 to the power of 1 (special case). result1: la $14, str3 puts $14 # Print out "2^" li $v0, 1 move $a0, $15 syscall # Print out value of user input. la $16, str4 puts $16 # Print out '=' li $v0, 1 li $a0, 2 syscall la $13, newline # Create new line lb $12, ($13) putc $12 b endofline # If the input is not appropriate, print str2 and exit. exit_donothing: la $13, str2 puts $13 b endofline # This is the exit statement. endofline: done # END OF PROGRAM # Define strings which will be used throughout program .data prompt: .asciiz "Enter a digit '0'-'9': " str2: .asciiz "Bad user input. Quitting.\n" str3: .asciiz "2^" str4: .asciiz " = " newline: .byte '\n'
