Close
Duke shfaqur rezultatin -19 deri 0 prej 11
  1. #1

    MIPS - Detyr shtpie

    Si keni qen cuna. Kam 1 jav tash qe po munohem me ndrit i assgn. nMIPS-assembly ene s'po del gja. Ene 1 jav koh kam.
    Jan dy pytje, Tparn do e postoj nanglisht sic a ene kerkesa qe mos tu ngatroj se gjanat jan shum specifik

    In this assignment, you are asked to write a MIPS program to simulate just the adder
    portion of a 4 bit ALU. Your program must include a procedure adder1 that will
    perform the function of the 1-bit adder as described in the text.
    The MIPS program will accept two decimal numbers between 0 and 15 inclusive as
    input. Theses numbers are used exclusively to set up the 4-bit patterns for the two
    numbers that will be added together by your 4-bit ALU unit simulator.
    The 1-bit adder routine adder1 will accept 3 inputs; a, b, and carry-in, from the calling
    program. Each of these represents the 3 bit inputs into the adder discussed in the text. It
    will return 2 results; s and carry-out, to the calling program. In the building of this 1-bit
    adder, adder1, you must use logical instructions. Thus, in order to produce the result,
    adder1 must evaluate two logic expressions, one for each of s and carry-out.
    The main program in adding the two 4-bit binary numbers, has to call the adder1 routine
    four times. DO NOT use a loop to do this. Assume the following memory content is
    already declared for the .data section of your program.
    .data
    result: .space 6
    str: .ascii "0\n"
    prt: .asciiz "The sum is: "
    Below is a sample run showing the input and output specifications under the assumption
    that your program is to add the two 4 bit binary numbers 0110 and 1101 together:
    Input the two decimal number [0-15], each on a separate line:
    6
    13
    The sum is 1 0011
    As indicated in the above output, you have to print the carry-out bit from the most
    significant bit as well, separated from the 4 sum bits by two spaces.
    In your documentation (a2.html file) you must define and explain the logical expressions
    used in adder1, and how you derive them. You also should explain other part of your
    program such as how you dealt with the input and output of your program. In order to
    make your adder1 routine minimal, you will need to simplify these two expressions as
    much as possible. The simpler they are the less MIPS instructions that are needed to
    implement them. Show all the steps of the simplification in your documentation.
    Some additional specifications are:
    Assume that the entry is valid, i.e. each is an integer greater than or equal to zero
    and less than or equal to 15.
    The input should not be validated. We have assumed that the entry is valid as
    above.
    The two integers are read on two separate lines. This means you need to issue two
    "read int" syscalls.
    Once the output is generated, the program ends. Whether or not you output a new
    line character after the message is up to you, i.e. it is optional.
    Assume that for any arithmetic operation there is no overflow that is the result of
    any operation is a valid integer (greater than -2147483648 and less than
    +2147483647)
    In the construction of your subprograms, you must ensure that the subprogram
    may be used by any calling program with any register usage, without looking at
    the specific code.


    Per pytjen e dyt duhet me ndertu nje string_copy procedure dhe main ne te cilen prompt perdorusin per i string ene e kopjon karakter per karakter ene i kthen mrapsh t'dyja. Ene sikur me qen vec procedura pa main noten e merr t'plot, ma mir me qen rekursiv po ene po s'qe s'ka gja.
    Kush m'nimo do i jem borxhli

  2. #2
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Hajde detyre hajde. Per ta shitur e keni kete detyren ?
    Kjo duhet bere edhe ne MIPS se te ishte dicka ne PHP a Java a cfaredo skripti a gjuhe tjeter te siperme, s'do na lodhje shume, per me teper qe duhet rikujtuar pak ndertimi i "Mbledhesit" ne nje Alu.
    Nuk e di sa kohe ke ne dispozicion megjithese une sot jam disi i lire dhe kjo detyra duket mjaft interesante.
    Per sa i perket pyetjes se dyte do ishte me mire qe te pakten ate ta beje vete se duket e lehte.
    Megjithate me trego sa kohe ke se them deri neser e ke gati
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  3. #3
    Programues Softueresh Maska e edspace
    Antarsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Mosha
    38
    Postime
    2,573
    Do ta beja me qejf kete detyre se edhe une po marr nje klase te ngjashme ne kolegj por kjo eshte java e fundit dhe javen tjeter kam provimet.

    Cunimartum, po te shpjegoj me poshte si punon nje adder.

    Bitet qe do mblidhen jane A, B, dhe numri qe mbajme mend nga shifra me pare eshte Cin (carry-in). Tepricen qe do mbajme mend per shifren tjeter e quajme Cout (carry-out).

    Shuma/Sum = ( A xor B xor C )
    Cout = ( (A and B) or (A and Cin) or (B and Cin) )

    Kodi:
    A	B	A and B	    A xor B
    0	0	   0	       1
    0	1	   0	       0
    1	0	   0	       0
    1	1	   1	       1
    Shembull:

    Nqs do mbledhim 9 + 3
    I kthejme ne numra binar
    9 = 8 + 1 = 1001
    3 = 2 + 1 = 0011

    Kodi:
    Carry 	- - - 0
    A	1 0 0 1
    B       0 0 1 1
    -----------------------
    Sum     - - - -

    Fillojme nga biti me i vogel ne krahun e djathte.
    sum0 = 1 xor 1 xor 0 = 1 xor 0 = 0
    cout0 = (1 and 1) or (1 and 0) or (1 and 0) = 1 or 0 or 0 = 1

    Kodi:
    Carry 	- - 1 0
    A	1 0 0 1
    B       0 0 1 1
    -----------------------
    Sum     - - - 0

    Vazhdojme me bitin e dyte nga krahu i djathte (A = 0) (B = 1) (Cin = 1)

    sum1 = 0 xor 1 xor 1 = 0 xor 1 = 0
    cout1 = (0 and 1) or (0 and 1) or (1 and 1) = 0 or 0 or 1 = 1

    Kodi:
    Carry 	- 1 1 0
    A	1 0 0 1
    B       0 0 1 1
    -----------------------
    Sum     - - 0 0


    Vazhdojme me bitin e trete nga krahu i djathte (A = 0) (B = 0) (Cin = 1)

    sum2 = 0 xor 0 xor 1 = 1 xor 1 = 1
    cout2 = (0 and 0) or (0 and 1) or (0 and 1) = 0 or 0 or 0 = 0

    Kodi:
    Carry 	0 1 1 0
    A	1 0 0 1
    B       0 0 1 1
    -----------------------
    Sum     - 1 0 0

    Vazhdojme me bitin e katert (A = 1) (B = 0) (Cin = 0)

    sum3 = 1 xor 0 xor 0 = 1 xor 1 = 1
    cout3 = (1 and 0) or (1 and 0) or (0 and 0) = 0 or 0 or 0 = 0

    Kodi:
    Carry 	0 1 1 0
    A	1 0 0 1
    B       0 0 1 1
    -----------------------
    Sum     1 1 0 0

    Pra shuma eshte numri binar 1100 ose 2^3 + 2^2 = 8 + 4 = 9 + 3 = 12
    Ndryshuar pr her t fundit nga edspace : 03-06-2004 m 23:58
    Edi

  4. #4
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Edspace tani po e shoh kete shkrimin tend por programin e kam mbaruar. E mbaja mend mire si punon "adderi" por do ishte mire ta kisha pare pak me pare shkrimin tend sepse mu desh te harxhoja ca kohe per tu kujtuar qe "Shuma" = a xor b xor c

    Panther mos kerko me detyra per te zgjidhur se me iku gjith mbasditja, paska vajtur ora 1

    Be qef

    Kodi:
    .data
    result:     .space     6
    str:        .ascii     "0\n"
    prt:        .asciiz    "The sum is  "
    prompt:     .asciiz     "Input the two decimal number [0-15], each on separate line:\n"
    .globl	main
    .text
    
    main:
    sub	$sp, $sp, 4
    sw	$ra, 0($sp) 
    
    ################
    #  Pjesa 1     #
    ################
    
    #-----------------#	Prompt perdoruesin per nje vlere
    la	$a0, prompt
    addi	$v0, $0, 4
    syscall
    #------------------#	Lexo dy numrat
    addi	$v0, $0, 5
    syscall
    add	$t0, $v0, $0
    #-------------------
    addi	$v0, $0, 5
    syscall
    add	$t1, $v0, $0
    #------------------#  
    
    ################
    #   Pjesa 2    #
    ################
    
    sll     $s0, $t0, 31
    srl     $s0, $s0, 31	# $t2 eshte biti 31 i numrit te pare
    sll     $s1, $t0, 30
    srl     $s1, $s1, 31	# $t3 eshte biti i 30 
    sll     $s2, $t0, 29
    srl     $s2, $s2, 31	# $t4 eshte biti i 29
    sll     $s3, $t0, 28
    srl     $s3, $s3, 31	# $t5 eshte biti i 28 
    
    #---Mbaron numri i pare fillon i dyti
    
    sll     $s4, $t1, 31
    srl     $s4, $s4, 31	# $t6 eshte biti 31 i te dytit
    sll     $s5, $t1, 30
    srl     $s5, $s5, 31	# $t7 eshte biti i 30
    sll     $s6, $t1, 29
    srl     $s6, $s6, 31	# $t8 eshte biti i 29
    sll     $s7, $t1, 28
    srl     $s7, $s7, 31	# $t9 eshte biti i 28
    
    #--Therresim adder1 me 3 parametra
    
    add	$a0, $s0, $0	# fut 2 bitet dhe carry-in ne a0, a1, a2 per ti kaluar ne subroutine 
    add	$a1, $s4, $0     
    add	$a2, $0,  $0
    jal	adder1          # Merr shumen e pare dhe Carry-Out
    add	$s0, $v0, $0    #-- Ne s0 shuma e pare
    add	$s4, $v1, $0
    
    #-----------------
    
    add	$a0, $s1, $0
    add	$a1, $s5, $0
    add	$a2, $s4, $0
    jal	adder1          # Merr shumen e dyte dhe Carry-Out
    add	$s1, $v0, $0    #-- Ne s1 shuma e dyte
    add	$s5, $v1, $0
    
    #-----------------
    
    add	$a0, $s2, $0
    add	$a1, $s6, $0
    add	$a2, $s5, $0                
    jal	adder1		# Merr shumen e trete dhe Carry-Out
    add	$s2, $v0, $0	#-- Ne s2 shuma e trete
    add	$s6, $v1, $0
    
    #-----------------
    
    add	$a0, $s3, $0
    add	$a1, $s7, $0
    add	$a2, $s6, $0
    jal	adder1		# Merr shumen e katert dhe Carry-Out
    add	$s3, $v0, $0	#-- Ne s3 shuma e katert
    add	$s7, $v1, $0					
    j print
    
    #################
    #     Pjesa 3   #
    #################
    
    ######## Procedura -- NenRutina adder1
    
    adder1:   
    addi	$sp, $sp, -4
    sw	$ra, 0($sp)
    
    #----------------
    
    xor	$t1, $a0, $a1    # $t1 = a xor b
    xor	$t1, $t1, $a2    # $t1 = $t1 xor carryIn
    
    #--- Mbaron punen me shumen
    
    and	$t8, $a1, $a2    # $t8 = b and carryIn
    and	$t9, $a0, $a2    # $t9 = a and carryIn
    and	$t0, $a0, $a1    # $t0 = a and b
    or	$t8, $t8, $t9    # $t8 = (b and carryIn) + (a and carryIn)
    or	$t8, $t8, $t0    # $t8 = (b and carryIn) + (a and carryIn) +  
    
    #--- Mbarimi i CarryOut  #       + (a and b)
    
    add	$v0, $t1, $0
    add	$v1, $t8, $0
    
    #---------------- Nxirr regjistrin $ra nga staku
    
    lw	$ra, 0($sp)
    addi	$sp, $sp, 4
    jr	$ra
    
    ##################
    #     Pjesa 4    #
    ##################
    
    #----------------------------
    ## Printo rezultatin       ##
    #----------------------------
    
    print:      
    la	$t9, result	# Fut adresen e rezultatit ne  $t9
    la	$t8, str        # Fut adresen e stringut ne $t8
    add	$t9, $t9, $t8   # Kemi 8 bite per te bere afishimin
    sw	$t9, result
    la	$t9, result
    addi	$t8, $0, 32     # 32 eshte hapesira ne ASCII
    addi	$t7, $0, 48     # 48 eshte 0 ne ASCII
    add	$t0, $s7, $t7       
    sb	$t0, 0($t9)                  
    sb	$t8, 1($t9)	# hapesire
    sb	$t8, 2($t9)     # e dyfishte
    add	$t0, $s3, $t7 
    sb	$t0, 3($t9)    
    add	$t0, $s2, $t7 
    sb	$t0, 4($t9)   
    add	$t0, $s1, $t7 
    sb	$t0, 5($t9)                 
    add	$t0, $s0, $t7 
    sb	$t0, 6($t9) 
    sb	$0,  7($t9)     # null ne fund te stringut si perhere
    la	$a0, prt
    addi	$v0, $0,  4
    syscall                   
    addi	$v0, $0,  4
    add	$a0, $0,  $t9 
    syscall 
    
    #-- Kthe kontrollin
    
    lw	$ra, 0($sp)
    addi	$sp, $sp, 4	
    jr	$ra
    Ndryshuar pr her t fundit nga edspace : 04-06-2004 m 01:43
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  5. #5
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Po lexoja dhe njehere kerkesen dhe me ra ne sy nje pike ne te cilen theksohej se nenRutinat duhet te jene te perdorueshme nga cfaredo lloj Thirresi.
    Atehere te nenRutina "print" s0, s1, s2 duhen ruajtur ne stack bashke me $ra, adresen e kthimit nga nenRutina.
    pra para print bej:

    print:
    addi $sp, $sp -16
    sw $ra, 12($sp)
    sw $s0, 8($sp)
    sw $s1, 4($sp)
    sw $s2, 0($sp)

    .
    .
    .
    bei lw te tera ato qe ruajte ne fund dhe coe stakun ku ishte
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  6. #6
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Po bashkangjis dhe nje log file me poshte me vlerat 5 dhe 8 ne hyrje.

    Per te tere ata qe s'e dine MIPS eshte procesor ashtu si edhe x86 e te tjere dhe futet nen grupin RISC(reduced instr. set.). Si c'do procesor ka dhe bashkesine e tij te instruksioneve te ofruara qe formojne edhe gjuhen assembly me te cilen u be programi me siper. Per te pare nje simulim te programit me siper duhet te merrni programin simulues ketu: http://www.cs.wisc.edu/~larus/spim.html
    MIPS perdoret ne Nintendo, Sony Playstation, Nec etj.
    Skedart e Bashkngjitur Skedart e Bashkngjitur
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  7. #7
    Programues Softueresh Maska e edspace
    Antarsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Mosha
    38
    Postime
    2,573
    PantherTouch,

    I takon nje darke e mire cunit te martum se e ka qare mips-in.

    Cunimartum,
    E pashe qe nuk ishte ruajtur formatimi kur e kishe kodin ne forum dhe e formatova kodin qe te jete me i qarte per ata qe e lexojne ne forum. Nuk besoj te kem ndryshuar gje me rendesi.

    Tek pjesa qe ruhen variablat ne stack, ben mire qe te shtohen edhe $a0, $a1 $a... te cilat ti i ndryshon kur kerkon numrat nga perdoruesi.
    Ndryshuar pr her t fundit nga edspace : 04-06-2004 m 02:09
    Edi

  8. #8
    Nuk di ca me t'than o vlla. Po kjo punoka bre, un rrotullovna sa mdolen trut ene s'ishte gja.
    Do nderrroj komentat edhe a gati.
    Nigjo du me ta shperbly, do tdergoj imail, ma posto nji imail ktu po spate bezdi
    Varablat ka staku i rregullova jan mir fare, a* regjistrat sipas konvents MIPS nuk duhen shtyt ne nenprocedur te pakten per ket detyr asht nregull se pyta
    Falemnder ene edspace per ndihm
    Mos mharro imail
    Falemners

  9. #9
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Po kjo punoka bre
    LOL Normal qe punon, Do behesh "shkencetar"apo jo, c'do gje e ka nje zgjidhje. Sa per shperblimin ne programusat e kemi 50$ oren, shumezoe me 6 dhe bei vete llogarite. Meqe ra fjala ti c'e do posten elektronike, me mire te kishe kerkuar llogarine e bankes

    Panther duhet te kuptosh qe ne ketu ndihmojme njeri tjetrin thjesht duke u nisur nga ndjenja Shqiptare, dhe ne njefare menyre lidhemi me shume me njeri tjetrin. Une e di qe ti je perpjekur dhe i ke pasur idete ne koke por nje ndihme e vogel thjeshte te kursen kohe. Nje ndihme te tille enderroja edhe une kur isha ne fillime. Rastisi qe dje isha i lire se ka pasur plot detyra interesante ketu qe s'me ka dale koha te merrem. Per me teper ti e kishe ne MIPS qe s'para i intereson kujt, ta kishe ne PHP ose dicka tjeter s'do me binte rradha mua. Pra tregon qe ne programuesit jemi me patriotet LOL
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  10. #10
    Larguar Maska e cunimartum
    Antarsuar
    07-06-2002
    Vendndodhja
    Canada
    Mosha
    39
    Postime
    680
    Edspace rrofsh per formatimin, nuk e kuptova c'ndodhi me duket "Tab"-et u hapen
    Me sa di une regjistrat e argumentave a0, a1, a2, a3 nuk ka nevoje te futen ne Stack sepse ideja eshte qe nje regjister shtyhet ne Stack per te ruajtur vlerat ne kalimet midis Procedurave.
    Ne rastin e argumentave i vetmi rast qe perdoret stacku me sa mbaj mend eshte kur te duhen me shume se 4 te dhenet me konvente. Ne kete rast con e merr nga stacku. Jam i sigurte qe ti e di qe gjithe ideja eshte te simulosh kompiluesin ose shpesh te ndertosh nje kompilues ne MiPS prandaj perdorim konventat e Procesorit.
    Fen e ke krejt personale. MEMEDHEUN E KEMI TE PERBASHKET.

  11. #11
    Programues Softueresh Maska e edspace
    Antarsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Mosha
    38
    Postime
    2,573
    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 pr her t fundit nga edspace : 05-06-2004 m 02:06
    Edi

Tema t Ngjashme

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

Ruaj Lidhjet

Regullat e Postimit

  • Ju nuk mund t hapni tema t reja.
  • Ju nuk mund t postoni n tema.
  • Ju nuk mund t bashkngjitni skedar.
  • Ju nuk mund t ndryshoni postimet tuaja.
  •