News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Fibonacci sequence

Started by zekyr, July 30, 2011, 11:07:15 PM

Previous topic - Next topic

zekyr

Hi! I wrote my first program that did something in MASM!

It really does look like butthole though. and I'm wondering if anyone had time they could look over it and take a look at ways of cleaning this up or if its standard to have code looking like this, or if maybe theres any pointers or anything someone could point me towards in terms of macros that I haven't used here. I think I used more registers than I should have as well. Any comments would help me out a lot! =)


;i saw someone initialize their code this way, and I wanted to try to write my own file

include \masm32\include\masm32rt.inc

.stack 100h         

.data
    Line1 db "*********************************************",10,13,0
    Line2 db "*  Fibonnaci sequence                       *",10,13,0
    Line3 db "*********************************************",10,13,0
.data?
    buffer db 1024 dup(?)
   
.code

start:
    print OFFSET Line1
    print OFFSET Line2
    print OFFSET Line3
   
    mov eax, OFFSET buffer
    mov ebx, 1024
    xor ecx, ecx

CLEAR_BUFFER:
    mov [eax], ecx
    add eax, 4
    sub ebx, 4          ;when ebx reaches 0 we're done!
    jnz CLEAR_BUFFER

    inkey "Lets try to do something: "
   
    mov eax, OFFSET buffer+4
    mov ebx, 1
    mov [eax], ebx

    add eax, 4   
    xor ecx, ecx
LOOP_UNTIL_OVERFLOW:
    add eax, 4       
    push eax
    print str$(ecx), " ", 0
    pop eax
    mov ecx, [eax-8]   
    add ecx, [eax-4]
    mov [eax], ecx   
    jnc LOOP_UNTIL_OVERFLOW

    inkey
    invoke ExitProcess, 0


end start


its output is


*********************************************
*  Fibonnaci sequence                       *
*********************************************
Lets try to do something:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711
28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 57
02887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296
433494437 701408733 1134903170 1836311903 -1323752223 Press any key to continue
...

qWord

You do not need to clear the buffer, because the BSS (.data?)  segment is initialized with zero-bytes.
Also the print macro follows the WinABI, so that esi, edi and ebx are preserved - you can use this register for storing the last two results, instead of an array.
FPU in a trice: SmplMath
It's that simple!

dedndave

if you want to add an option to go back and start again, you would want the clear buffer stuff
nice work   :U
a couple years ago, we were playing with this in the 16-bit sub-forum   :P

-Alex-

I made a GUI, with 1 button to start, displays a list of all numbers possible in 32bit. Exe + source attached.

zekyr

Thanks for reviewing it guys, I'll try to write it without the buffer then and I'll keep in mind that .data? is initialized, thats some really cool gui code as well! Maybe I should try to mess around with gui stuff next! :)

qWord

maybe also interesting:
Quoteinclude masm32rt.inc
.686p
.code
main proc
LOCAL qw[2]:QWORD

   clc
   mov DWORD ptr qw[0+0],0
   mov DWORD ptr qw[0+4],0
   mov DWORD ptr qw[8+0],1
   mov DWORD ptr qw[8+4],0
   .while !CARRY?
      print uqword$(qw[0]),13,10
      mov eax,DWORD ptr qw[0+0]
      mov edx,DWORD ptr qw[0+4]
      m2m DWORD ptr qw[0+0],DWORD ptr qw[8+0]
      m2m DWORD ptr qw[0+4],DWORD ptr qw[8+4]
      add DWORD ptr qw[8+0],eax
      adc DWORD ptr qw[8+4],edx
   .endw
   mov eax,DWORD ptr qw[0+0]
   mov edx,DWORD ptr qw[0+4]
   print uqword$(edx::eax),13,10
   inkey
   exit
   
main endp
end main
FPU in a trice: SmplMath
It's that simple!

zekyr

I need to review yours qword a little more, definitely interesting! the addressing stuff was getting confusing for me and I think my code was looking bad because of that. I tried rewriting it using esi edi and ebx; this is what i came up; it prints incorrectly on the first iteration because my logic isn't flawless but i can fix that


include \masm32\include\masm32rt.inc

.stack 100h

.data
TOP db "***********************************************", 10, 13,
       "*    I just came to say hello!                *", 10, 13,
       "***********************************************", 10, 13, 0

.code

start:
    print OFFSET TOP

    clc
    xor esi, esi
    mov edi, 1
    xor ebx, ebx
LOOP_UNTIL_OVERFLOW:
    xchg esi, edi
    mov  edi, ebx
    mov  ebx, esi
    print str$(ebx), " ", 0
    add  ebx, edi
   
    jnc LOOP_UNTIL_OVERFLOW

    inkey
    invoke ExitProcess, 0

end start

mineiro

Hello zekyr, I think you can remove the clc instruction in your example, because next instruction (xor reg,reg) clear that flag. You can remove the .stack 100h too.
Follow one example, based in yours, but uses 2 registers.
include \masm32\include\masm32rt.inc
.data
    Line1 db "*********************************************",10,13,
    "*  Fibonnaci sequence                       *",10,13,
    "*********************************************",10,13,0
.code
start:
    print OFFSET Line1
sub esi,esi     ;clc implicit
mov edi,1
@@:
print str$(esi), " ", 0
add esi,edi
jb @F    ;jc to @@: foward
print str$(edi), " ", 0
add edi,esi
jnb @B   ;jnc to @@: back
@@:
    inkey
    invoke ExitProcess, 0
end start

raymond

And to simplify it just a bit more,

   ...
   sub esi,esi
   mov edi,1
   print str$(esi), " ", 0
@@:
   add esi,edi
   jc @F
   print str$(esi), " ", 0
   xchg esi,edi
   jmp @B
@@:
   ...
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

mineiro

Hello Sr raymond, I see you implementation and I have perceived that It appears one overflow in the result.
-1323752223

qWord

Quote from: mineiro on August 01, 2011, 05:55:59 PM
Hello Sr raymond, I see you implementation and I have perceived that It appears one overflow in the result.
-1323752223
-1323752223 (signed) => 0xB11924E1 => 2971215073 (unsigned)
FPU in a trice: SmplMath
It's that simple!

dedndave

a simple mod to Raymond's code
   sub esi,esi
   mov edi,1
   print ustr$(esi), " ", 0
@@:
   add esi,edi
   jc @F
   print ustr$(esi), " ", 0
   xchg esi,edi
   jmp @B
@@:

i am sure that's what he meant   :P

raymond

Quotei am sure that's what he meant

I had  blindly copied the str$ macro from mineiro's code assuming that was what he intended to use. I guess his own code was also "overflowing" one result but he probably never tried it or noticed it.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

mineiro

Hello Sr's, well, sorry about my english, today I see the use of word overflow is a mistake. Really sorry.
If I comment the line:
   jb @F
So, I get that last minus(decimal signed) result. This is why I have put that line in the code.
My intention in that code is stop showing fibonacci if found a minus signal(I called this as overflow in previous posts) and don't echo that result in screen. This is why I don't have used xchg, the x-change I have used is 2 add instructions.

dedndave

just in case zekyr didn't understand the change.....

the str$ macro displays signed dword integers in the range -2147483648 to 2147483647
the ustr$ macro displays unsigned dword integers in the range 0 to 4294967295