Hi all,

I'm new to assembly so I wondering if you can help me out.  I have got a simple code and I need to learn how to translate it to assembly. 

A Fibonacci number series contains the sum of the last two natural numbers. The series starts from 0.

So, the series will be 0, 1, 1, 2, 3, 5, 8, 13, 21...
up to 15 times.

Here is my first attempt.

.data

.text
.globl main
main:

_start
li $t0, 0          #a = 0        use move
$t0, $t1  to copy
li $t1, 1                          #b = 1
li $t4, 0                        #counter
                                #loop start    loop:
add $t3, $t1, $t0                #c = a + b
move $t0, $t1                    #a = b
move $t1, $t3                    #b = c
                                 
li $v0, 1
syscall

add $t4, $t4, 1
bne $t4, 15, loop
jal loop

li $vo, 1

add $a0, $a0, $t1
li $v0, 1


loop:
        add $t0 , $t1, $

    syscall


end:
     
        jr $ra


THank you
Posted on 2008-09-24 07:03:58 by tlk2bobby
I just wrote this from the top of my head.
If you dont like the highlevel loop directives, I don't really care :)
Clarity before optimizing.


xor eax,eax    ;set to zero
push eax
DbgDec eax   ;somehow show this value
inc eax          ;set to one
push eax       ;push the most recent value
DbgDec eax   ;somehow show this value

xor ecx,ecx      ;loop from 0 to 15
.while ecx<15
pop eax          ;pop the last two values
pop edx
push edx        ;push the most recent
add eax,edx    ;add the last two values
push eax        ;push the sum of the last two values
DbgDec eax    ;somehow show the most recent sum
inc ecx           ;increment loop counter
.endw

pop eax         ;restore the stack
pop eax

Posted on 2008-09-24 07:16:13 by Homer
well, thanks for your input, but I don't really understand what you have written.  My code is correct, I just need some finishing touches so it can run on SPIM and set it to stop on 15 digits.
Posted on 2008-09-24 10:06:14 by tlk2bobby
Looks like the formatting of your code has gone nuts after posting it to the board..

There are two labels "loop"; the first one behind a comment where the loop should likely start and one later on towards the label "end". Also, there's an instruction "add $t0 , $t1, $". Is this some kind of shortcut or does it miss a register maybe?

Caveat emptor though, I'm also new to assembly... :D
Posted on 2008-09-24 22:02:13 by cp
Ok, I have gotten the code correct where I can get the results, now I need help with putting space and comma.

I have it going 0112358......

I need to make it 0, 1, 1, 2, 3, 5, 8,....., 377



.data

.text
.globl main
main:

_start:

li $t0, 0                              #a = 0   

li $t1, 1                                #b = 1

li $t4, 0                           

bne $t4, 15, fib_loop

jal fib_loop

################################

fib_loop:

move $t0, $t1

move $t1, $t3

        add $t3 , $t0, $t1

move $a0, $t1

li $v0, 1

syscall

add $t4, $t4, 1

li $t7, 15

beq $t4, $t7, loopends

b fib_loop

loopends:

move $a0, $t1

jr  $ra

Posted on 2008-09-25 11:33:10 by tlk2bobby
What assembler is that?
You just need to emit a comma after each output value except the last one, right?
Look on google for an ASCII TABLE, it will show you the character code for comma.
Just spew out a comma after each outputted value, where the loop counter is not Max.
Too easy.
Posted on 2008-09-26 01:54:50 by Homer

What assembler is that?


indeed, what is it? although BNE and BEQ remind me the M68xxx chip...
Posted on 2008-09-26 03:34:36 by damarck


What assembler is that?


indeed, what is it? although BNE and BEQ remind me the M68xxx chip...



It is MIPS assembly. And it makes use of the SPIM simulator.


now I need help with putting space and comma.


As already pointed out, you need to output the filling sequence (", " in this case) after each number you put out, except after the last number. I'd do this with another syscall in the loop and an additional statement after .data and before .text ...
Posted on 2008-09-26 08:05:04 by cp
You could use the stack as well:


#
  li $v0, 4
  li $t8, 0x........ # load your string into $t8
# [...]
  subu $sp, $sp, 4 # adjust stack pointer
  sw $t8, ($sp)    # push onto the stack
  move $a0, $sp    # use sp as argument for syscall
  syscall
# [...]
Posted on 2008-09-26 21:08:42 by cp
thanks people, Im using Spim simulator.  I figured it out.

Is there a place where I can find  like Assembly language or Mips for Dummies like me.  Like how I can use assembly to practical use or implement it to something.  Im not really sure why we need this low level language when processors are so fast that it seems Im moving a step backwards.  Cause everyone learn high level before low level.

Prove me wrong, tell me something that will blow my mind away.
Posted on 2008-09-28 06:26:58 by tlk2bobby
I've posted a while ago a series of tutorials called Masm for Morons.
It's geared toward complete beginners, as a bridge toward Iczelions tutorials (basically, I was asked to do this because absolute beginners had trouble understanding iczelions tutorials and there needed to be something to teach them how to learn).
My dumpsite is http://stig.servehttp.com/homer you can find it there.

Once you get a grip on masm, you can look at ObjAsm32, where we move up from asm to oopasm, and show those C++ coders that rapid programming can be done in lowlevel, smaller, faster, better, stronger - heh.

For the record, I don't like masm.
But I do use it, and I support oopasm as a viable and superior alternative to HLLs.
Posted on 2008-09-28 08:14:19 by Homer

thanks people, Im using Spim simulator.  I figured it out.

Is there a place where I can find  like Assembly language or Mips for Dummies like me.  Like how I can use assembly to practical use or implement it to something.


I'm currently working through this fine tutorial. It's kind of slow-paced and more or less geared towards Freshmen Like Us. Exactly what I need for now. Might have some tidbits for you, too.
Posted on 2008-09-28 12:50:41 by cp
Thanks for the links CP.  That link tutorial looks very helpful. 
Posted on 2008-09-29 03:02:39 by tlk2bobby
Glad to hear that. Let's hope it actually turns out being helpful to you.. :)
What resources did you use so far? Anything worth reading?
Posted on 2008-09-29 17:35:41 by cp