1

This program calculates the Fibonacci sequence.

How can i do it with unsigned number?( 'cause obviously I don't need negative number and if I use 2's complement I can't represent the 47th Fibonacci number

I use qtspim and I can't use sys call 36 to print unsigned number.....it says unknown sys call 36

Also how can I resolve an eventual arithmetic overflow?

.text
.globl main

main:
    la $a0, richiesta        
    li $v0,4
    syscall

    li $v0, 5                 
    syscall
    sw $v0, indice                  

    lw $a0, indice       
    jal FIBO        


    sw $v0, numero        

    la $a0, risultato       
    li $v0, 4
    syscall

    lw $a0, numero     
    li $v0, 1
    syscall

    li $v0, 10
    syscall







FIBO:           
        li $s0, 0        # Inizializzazione di "primo inserito" penultimo
        li $s2, 0        # Se n= 0 il numero di Fibonacci è 0
        beq $a0, 0, fine    # Se n = 0 il risultato è già noto e non occorre calcolarlo

#inizializzazione del secondo elemento cioè 1
        li $s1, 1        # Inizializzazione di "secondo inserito" ultimo
        li $s2, 1        # Se n= 1 il numero di Fibonacci è 1
        beq $a0, 1, fine    # Se n = 1 il risultato è già noto e non occorre calcolarlo



        move $t1, $a0        

        addi $t1, $t1, -1   

ciclo:      #è un ciclo while
        beq $t1, $zero, fine    




        add $s2, $s0, $s1    

        move $s0, $s1        # "ultimo" divena "penultimo"
        move $s1, $s2        # "nuovo" diventa "ultimo"
        addi $t1, $t1, -1    # Decremento del contatore di ciclo
        j ciclo


fine:

        move $v0, $s2        # "nuovo" (cioè $s2) è il risultato
        jr $ra            # Ritorno dalla procedura






.data
indice: .word 0        #l'indice del numero di Fibonacci da calcolare
numero: .word 0        #il numero calcolato
richiesta: .asciiz "Inserire l'indice del numero di Fibonacci da calcolare (maggiore o uguale a 0): "
risultato: .asciiz "Il numero di Fibonacci è: "

I use qtspim and I can't use sys call 36 to print unsigned number.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Elisa
  • 19
  • 3
  • Write your own integer-to-string conversion routine. It's not particularly complicated. – Michael Mar 10 '18 at 12:18
  • 1
    Unlike MARS, SPIM may not have a syscall for unsigned numbers. I found this (https://stackoverflow.com/questions/26197777/print-mips-register-contents), which says only MARS has unsigned hex output, and it wouldn't be surprising if unsigned decimal was missing, too. You may have to do it manually (see that link) – Peter Cordes Mar 10 '18 at 12:19
  • How can I do it? – Elisa Mar 10 '18 at 12:19
  • I don't know how to write a integer-to-string conversion routine. – Elisa Mar 10 '18 at 12:48
  • Basically the same way like human does write numeric values on paper. If you have value 123 in head, you will first write hundreds (10^2) amount, that's single hundred, i.e. digit "1", then you write remaining tens (10^1) = digit "2", and then you write remaining ones (10^0) = digit "3". So in your routine, for 32 bit integers, you can start with counting 1e9 (as max unsigned 32b value is `4294967295`) in the value, then 1e8, ... down to 1e0 (=1) and output digits like that. Or often the reversed loop is used, dividing by 10 and storing remainder as digit from right to left (try w/ 123). – Ped7g Mar 10 '18 at 13:18
  • About overflow handling... the MIPS has actually two instructions for most of the arithmetic operations (`add` vs `addu` and similar), one should fire up overflow trap mechanism (interrupt), the other ignores it. I never used it myself while helping with MIPS questions on SO, so I didn't study details, how exactly this overflow mechanism is used (and it is too different from x86/Z80 CPUs, which are my main "domain"), so check first for some tutorial/example how to deal with overflow on MIPS... that may include some example of 64b+64b math with 32b registers (common theme for tutorials). – Ped7g Mar 10 '18 at 13:25

0 Answers0