Close
Faqja 2 prej 2 FillimFillim 12
Duke shfaqur rezultatin 11 deri 11 prej 11
  1. #11
    Programues Softueresh Maska e edspace
    Anëtarësuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Postime
    2,565
    Zakonisht $a1 $a2... nuk ruhen ne kapice (stack) por nje prej rasteve qe ruhen eshte kur kemi riperseritje (recursion) si ne funksionin fibonacci.

    Shiko kodin me poshte si shembull. Programi pyet perdoruesin per nje numer dhe pastaj jep pergjigjen nqs eshte prim apo jo. Ne fillim eshte kodi ne C++ dhe pastaj eshte perkthyer ne MIPS

    Mqns profesori insistonte qe programi te thiret nga cdo funksion, ka mundesi qe te therese edhe vetveten ose nga nje funksion riperserites. Duke mos ditur se kush e theret programin tend, ndonjehere eshte me mire te jesh i sigurte dhe te ruash regjistrat e argumentave ne kapice (stack).

    Nejse...nuk eshte me aq rendesi.

    U mesuan keq keta forumistat ketu ne forum. I marrin 10-tat pa u munduar fare por nuk po mesojne gje.

    Kodi PHP:
    #include <iostream>

    using namespace std;

    bool isPrime(int n);
    bool searchFactor(int rangeint n);

    int main()
    {
        
    int n;
        
        
    cout << "Please enter a positive integer to be tested: ";
        
    cin >> n;
        
    cout << "\nYou entered n = " << << endl;
        
        if ( 
    isPrime(n) ) 
            
    cout << "\nRecursive isPrime retuned TRUE\n\n";
        else
            
    cout << "\nRecursive isPrime retuned FALSE\n\n";
        
        return ( 
    EXIT_SUCCESS );
    }

    // Recursive Primality Tester Algorithm
    // Input: Positive integer n >=2
    // Output: true if n is prime
    //         false if n is composite
    bool isPrime(int n)
    {
        
    int i;
        
        if ( 
    )
        {
            
    cout << "The number entered is less than 2!\n"
            exit(
    EXIT_FAILURE);
        } 
        
        return !
    searchFactor(2n);;
    }

    // Recursive search for factors 
    // Input: Positive integer n >=2
    //        Positive integer range
    // Output: true if exists an integer in [range, n-1] that is a factor of n (i.e., n is not prime)
    //         false if there is no such integer
    bool searchFactor(int rangeint n)
    {
        if ( 
    range >= ) return false;
        
        if ( ( 
    range ) == ) return true;
        else return 
    searchFactor(range+1n);

    Kodi:
    # Main program that calls the subroutine isprime and prints the result.
    	
    	.text			# assembly directive that indicates what follows are instructions
    	.globl  main		# assembly directive that makes the symbol main global
    main:				# assembly label
    	sub	$sp,$sp,8	# push stack to save registers needed by the system code that called main
    	sw	$ra,0($sp)	# save return address
    
    	li	$v0,4		# set the code to print_str
    	la	$a0,str1	# load the string to be printed
    	syscall			# print "Please enter a positive integer to be tested: "
    
    	li	$v0,5		# read from the keyboard the input and store to register $v0
    	syscall			# execute the instruction
    	add	$a1,$v0,$zero	# copy the value just entered to the $a1 register
    
    	li	$v0,4		# set the code to print_str
    	la	$a0,str2	# load the string to be printed
    	syscall			# print "You entered n = "
    
    	li	$v0,1		# set the code to print_int
    	add	$a0,$a1,$zero	# load the int to be printed
    	syscall			# print n
    
    	li	$v0,4		# set the code to print_str
    	la	$a0,str6	# load the string to be printed 
    	syscall			# print "\n"
    
    	jal	isprime		# call subroutine isprime to check if number is prime
    	sw	$v0,4($sp)	# result returned in $v0 and stored on the stack
    
    	bne	$v0,$zero,prime	# if( isprime(n) != zero ) then it is a prime
    				
    				# else it is not a prime
    	li	$v0,4		# set the code to print_str
    	la	$a0,str4	# load the string to be printed
    	syscall			# print "\nIterative isPrime retuned FALSE\n\n"
    	j	exit
    
    prime:	li	$v0,4		# set the code to print_str
    	la	$a0,str3	# load the string to be printed
    	syscall			# print "\nIterative isPrime retuned TRUE\n\n"
    	j	exit
    		
    exit:	lw	$ra,0($sp)	# restore return address used to jump back to system
    	add	$sp,$sp,8	# pop stack to prepare for the return to the system
    	jr	$ra		# [jump register] return to the system 
    
    
    	.data             # Assembly directive indicating what follows is data
    str1:	.asciiz "Please enter a positive integer to be tested: "
    str2:	.asciiz "\nYou entered n = "
    str3:	.asciiz "\nIterative isPrime retuned TRUE\n\n"
    str4:	.asciiz "\nIterative isPrime retuned FALSE\n\n"
    str5:	.asciiz "The number entered is less than 2!\n"
    str6:	.asciiz "\n"
    
    
    #===============================================================================================
    # Function isprime - check if input is a prime number and return True or False
    # Inputs:
    #   int n passed in registers $a1
    # Output:
    #   bool (T/F)(1/0) returned in $v0
    # Temporaries:  $t0, $t1
    
    
    	.text
    isprime:
    	sub	$sp,$sp,12	# push stack to save registers needed by the system code that called main
    	sw	$ra,8($sp)	# save return address
    	sw	$a1,4($sp)	# save argument n //optional
    
    	slti	$t0,$a1,2	# test if n < 2 
    	bne	$t0,$zero,fail	# if n < 2 then exit_failure
    	
    	li	$a0,2		# load 2 in $a0 = range
    
    	jal	srchfct		# call subroutine srchfct(2, n);
    
    				# invert the answer: $v0 = $v0 + 1 - ( 2 * $v0 )
    	mul	$t0,$v0,$a0	# if $v0==0 then $t0=0; if $v0==1 then $t0=2
    	addi	$t1,$v0,1	# if $v0==0 then $t1=1; if $v0==1 then $t1=2
    	sub	$v0,$t1,$t0	# if $v0==0 then $v0=1; if $v0==1 then $v0=0
    	sw	$v0,0($sp)	# result returned in $v0 and stored on the stack
    
    	lw	$a1,4($sp)	# restore argument n //optional
    	lw	$ra,8($sp)	# restore return address used to jump back to system
    	add	$sp,$sp,12	# pop stack to prepare for the return to the system
    	jr	$ra		# return to calling procedure
    
    fail:	li	$v0,4		# set the code to print_str
    	la	$a0,str5	# load the string to be printed
    	syscall			# print "The number entered is less than 2!\n"
    
    	li	$v0,10		# set the code to exit
    	syscall			# print "\nIterative isPrime retuned FALSE\n\n"
    
    
    
    #===============================================================================================
    # Function srchfct - Recursive search for factors 
    # Input: 
    #   Positive integer n >=2
    #   Positive integer range
    #   inputs stored in $a0, $a1
    # Output: 
    #   bool (T/F)(1/0) returned in $v0
    #   true if exists an integer in [range, n-1] that is a factor of n (i.e., n is not prime)
    #   false if there is no such integer.
    # Temporaries:  $t0
    
    
    	.text
    srchfct:
    	sub	$sp,$sp,12	# Push stack to create room for 3 items
    	sw	$ra,8($sp)	# save the return address
    	sw	$a1,4($sp)	# save the argument n
    	sw	$a0,0($sp)	# save the argument range
    	
    	sge	$t0,$a0,$a1	# test if range >= n
    	bne	$t0,$zero,false	# if range >= n then return false
    
    	rem	$t0,$a1,$a0	# $t0 = n % range
    	beq	$t0,$zero,true	# if ( a % n ) == 0 then return true
    				# else
    	addi	$a0,$a0,1	# range = range + 1
    	jal	srchfct		# srchfct(range+1, n)
    	
    exits:	lw	$a0,0($sp)	# restore the argument range
    	lw	$a1,4($sp)	# restore the argument n
    	lw	$ra,8($sp)	# restore the return address
    
    	add	$sp,$sp,12	# pop 3 items from stack
    	jr	$ra		# return to calling procedure
    
    true:	li	$v0,1		# return true
    	j	exits
    
    false:	li	$v0,0		# return false
    	j	exits
    Ndryshuar për herë të fundit nga edspace : 05-06-2004 më 02:06
    Edi

Faqja 2 prej 2 FillimFillim 12

Tema të Ngjashme

  1. Fizike: Si te gjej dendesine e ajrit ne dhomen time?
    Nga vaalmir në forumin Mentori akademik
    Përgjigje: 16
    Postimi i Fundit: 05-11-2014, 11:18
  2. Hetimet e Kryeprokurorisë për tragjedinë e Gërdecit
    Nga RaPSouL në forumin Tema e shtypit të ditës
    Përgjigje: 625
    Postimi i Fundit: 07-07-2009, 23:56
  3. Diplomacia, një detyrë e vështirë
    Nga Albo në forumin Kulturë demokratike
    Përgjigje: 1
    Postimi i Fundit: 22-05-2006, 13:42
  4. Ofertat me te reja per pune
    Nga ganoid në forumin Ekonomi & biznes
    Përgjigje: 27
    Postimi i Fundit: 21-01-2005, 13:30

Regullat e Postimit

  • Ju nuk mund të hapni tema të reja.
  • Ju nuk mund të postoni në tema.
  • Ju nuk mund të bashkëngjitni skedarë.
  • Ju nuk mund të ndryshoni postimet tuaja.
  •