The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: zekyr on July 30, 2011, 11:07:15 PM

Title: Fibonacci sequence
Post by: zekyr on July 30, 2011, 11:07:15 PM
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
...
Title: Re: Fibonacci sequence
Post by: qWord on July 30, 2011, 11:49:24 PM
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.
Title: Re: Fibonacci sequence
Post by: dedndave on July 30, 2011, 11:59:09 PM
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
Title: Re: Fibonacci sequence
Post by: -Alex- on July 31, 2011, 01:24:49 AM
I made a GUI, with 1 button to start, displays a list of all numbers possible in 32bit. Exe + source attached.
Title: Re: Fibonacci sequence
Post by: zekyr on July 31, 2011, 02:19:12 AM
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! :)
Title: Re: Fibonacci sequence
Post by: qWord on July 31, 2011, 02:32:40 AM
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
Title: Re: Fibonacci sequence
Post by: zekyr on July 31, 2011, 03:18:13 AM
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
Title: Re: Fibonacci sequence
Post by: mineiro on July 31, 2011, 06:34:02 AM
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
Title: Re: Fibonacci sequence
Post by: raymond on August 01, 2011, 03:49:41 AM
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
@@:
   ...
Title: Re: Fibonacci sequence
Post by: 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
Title: Re: Fibonacci sequence
Post by: qWord on August 01, 2011, 06:05:46 PM
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)
Title: Re: Fibonacci sequence
Post by: dedndave on August 01, 2011, 06:07:55 PM
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
Title: Re: Fibonacci sequence
Post by: raymond on August 02, 2011, 12:56:10 AM
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.
Title: Re: Fibonacci sequence
Post by: mineiro on August 02, 2011, 04:12:38 PM
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.
Title: Re: Fibonacci sequence
Post by: dedndave on August 02, 2011, 05:58:57 PM
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
Title: Re: Fibonacci sequence
Post by: zekyr on August 02, 2011, 11:15:33 PM
I got it! :) I'm just a little behind on figuring out and finding all the macro's I need to use. I figured -1323752223 was a two's compliment interpretation or whatever just didnt know how to correct it. good to know that i can use ustr$ for unsigned stuff.

there is a lot these macro's do that I get confused what they assemble into or how they handle some of their parameters; for example
    invoke CreateWindowEx, 0, wcex.lpszClassName, "title", 010c80000h, 0,0,256,256,0,0,wcex.hInstance,0
will error; it doesnt like "title" as a parameter and i got to make it its own var and pass it the offset

but
    mov eax, rv(CreateWindowEx, 0, wcex.lpszClassName, "title", 010c80000h, 0,0,256,256,0,0,wcex.hInstance,0)
doesn't.

i dont know what rv is doing with my string or where its sticking it or anything. im guessing it somehow fits "title" into .data and does like push OFFSET title or something
Title: Re: Fibonacci sequence
Post by: dedndave on August 03, 2011, 02:26:20 AM
try ,chr$('title'),
Title: Re: Fibonacci sequence
Post by: zekyr on August 03, 2011, 04:54:42 AM
that worked :) thanks a lot dave!
Title: Re: Fibonacci sequence
Post by: dedndave on August 03, 2011, 10:50:44 AM
the chr$ macro assigns memory space for the string in the initialized data section
if you use the string 'title' several times throughout the program, it may be better to declare it the regular way
AppName db 'title',0
then refer to the ADDR or OFFSET of AppName
of course, it depends on the length of the string and how many times it's used   :P
Title: Re: Fibonacci sequence
Post by: cobold on August 06, 2011, 12:40:18 AM
Hello,

take 8192 DWORDS as a "bignumber" and keep adding you will arrive at Fibonaccinumber 377599 which has 78914 decimal digits:

149446500387691722........754064175901

:bdg

of course you could even take 32768 DWORDS and calculate F1510391 with 315653 decimal digits.  :eek

or - if you like it extreme: allocate all of your available memory for bignumber. What would be the biggest fib you are able to calculate?  :dazzled:

Title: Re: Fibonacci sequence
Post by: zekyr on August 06, 2011, 12:49:49 AM
i was actually thinking about doing something like this! but i never got to writing anything because i saw the gui example and i wanted to figure out how to do guis in ASM :) but i do need to figure out how to allocate memory without using the .data? stuff and i have to figure out how to use the FPU still
Title: Re: Fibonacci sequence
Post by: cobold on August 06, 2011, 01:00:29 AM
Hello zekyr,

Quotehow to allocate memory without using the .data?

- use the alloc-macro or
- invoke GlobalAlloc,GPTR,number_of_bytes    ; GPTR = GMEM_FIXED AND GMEM_ZEROINIT

Quotefigure out how to use the FPU still

Raymond is the man to help you. You may want to read his excellent fputute at
http://www.website.masmforum.com/tutorials/fptute/

Title: Re: Fibonacci sequence
Post by: dedndave on August 06, 2011, 03:38:05 AM
i would think the FPU has limitations, here
the round-off errors happen fast in fibonacci world   :P
i can help you convert large integers to ASCII, though....

LLKF9_1.zip
http://www.masm32.com/board/index.php?topic=12363.msg94779#msg94779