Hi,

Am trying to do up a Fibonacci sequence for MIPS (School requires us to make use of HASE Simulator). I have done up the following code but the output seems to be a tad wrong.

ADDI R2 R0 1
ADDI R3 R0 1
ADDI R4 R0 1
ADDI R1 R0 10
loop1:
ADD R5 R2 R3
ADDI R7 R4 1
ADDI R6 R4 2
ADDI R4 R4 3
SLL R8 R7 2
ADD R3 R5 R2
SLL R9 R6 2
SLL R10 R4 2
SW R5 0(R8)
ADD R2 R3 R5
SW R3 0(R9)
SW R2 0(R10)
BNE R1 R4 loop1
NOP 
BREAK 
NOP 

My ouput is as follows: 0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.

I seem to be missing another 1 before the 2. Hmm, can anybody figure out why?

Thanks!

Dani AI

Generated

Good catch, — the missing 1 was not a math error but an initialization/order problem. The simulator’s memory cell you were reading still held 0, so the sequence printed 0, 1, 2, … until you explicitly wrote the seed into memory. The root causes to watch for in HASE/MIPS code are (a) not storing the initial seed(s) before you start indexing memory, and (b) overwriting a register you still need before you’ve stored its value.

A simpler, less error‑prone pattern is to keep a pointer to the data area and increment it by 4 each time (word size), store the two seeds once, then loop N−2 times computing next = a + b, storing next, and sliding the registers. That avoids repeated SLL/index math and reduces off‑by‑one mistakes. Example pattern:

ADDI R16 R0 0      ; ptr = base address (0 used here for simplicity)
ADDI R2  R0 1      ; a = 1
ADDI R3  R0 1      ; b = 1
SW   R2  0(R16)    ; store a
ADDI R16 R16 4
SW   R3  0(R16)    ; store b
ADDI R16 R16 4
ADDI R1  R0 9      ; loop count = N-2
loop:
ADD  R5  R2 R3     ; next = a + b
SW   R5  0(R16)    ; write next
ADDI R16 R16 4
ADD  R2  R3 R0     ; a = b
ADD  R3  R5 R0     ; b = next
ADDI R1  R1 -1
BNE  R1  R0 loop
NOP

Quick troubleshooting checklist: step the program in HASE and watch the memory window after each SW; confirm register usage and that you store seeds before they’re needed; avoid using complex index math unless necessary; ensure word alignment (addresses multiple of 4). Also decide explicitly whether your sequence should start 0,1,... or 1,1,... and seed accordingly.

Oh I forgot to SW R2 0(R0) in between the 4th and 5th lines to store the value 1 in the place of 0 in the data memory. Seems to work alright now. Output is now 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.