rss
twitter
facebook

Home

Please enter a positive integer in hex

##   C++ version:
##   void factorial(int input, int output){
##     int n = 0;
##     int fact = 0;
##     int i = 0;
##
##     cout << "Please enter a positive integer: ";
##     cin >> hex >> n;
##     if (n > 0){
##       fact = 1;
##       while (i < n){
##         fact = fact * i;
##         i++;
##       }
##       cout << "The fact of ";
##       cout << hex << n << " is " << fact << endl;
##     }
##     else
##       cout << "Number negative WRONG !" << endl;
##   }




   .data

# data items, i.e. all initialized and
# non-initialized items go in here

#strings for making the output look nice

str1:   .asciiz "Please enter a positive integer in hex: "
str2:   .asciiz "The factorial of "
str3:   .asciiz " is in hex "
str4:   .asciiz "Number negative WRONG ! "
newline: .asciiz "\n"

##      Assume that the compiler chooses to use registers for n,fact,i:
##      $8 = n         (user entered character)
##      $9 = fact      (running fact of the first n integers)
##      $10 = i         (integer to be added into sum, runs from 0 to n)
##      $11 = tmp       (used for comparison of i and n)

        .text
        .globl main    # declaration of main as a global variable

main:   li $8, 0
        li $9, 1
        li $10, 1
        li $11, 0

        puts str1

# the following 3 lines read a hex number and store it in $8

        li $2, 500     ## replace "get n" of SAL code since MAL
        syscall         ## does not have "get"
        move $8, $2   ##

        puts newline

if :    sub $11, $8, $11  ## if n > 0
        bltz $11, else 
then:     
while:  sub $11, $8, $10
        bltz $11, endwhile
loop:   mul $9, $9, $10
        add $10, $10, 1
        b while

endwhile:  puts str2
       
        li $2, 600     ## replacement of "put n" of SAL code
        move $4, $8
        syscall

        puts str3
       
        li $2, 600     ## replace "put sum" of SAL code
        move $4, $9
        syscall

        puts newline
      
        b end_if

# here starts the else part

else:   puts str4
       
        li $2, 600     ## replace "put sum" of SAL code
        move $4, $9
        syscall

        puts newline
      
end_if:
# the last line of main

        done

# end of main
# any subroutines may be inserted here:...

# END OF PROGRAM (leave this line here)

0 comentários:

 
Powered by Blogger