flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > fasmg usage examples: set of integers, long list of integers
Author
Grom PE

Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE 21 Sep 2016, 12:07
For fun and practice, I rewrote a puzzle solution I made with Python in fasmg.
Code:
```; fasmg puzzle:
; find the shortest list of numbers from 1 to 13
; where each number is adjacent to every other number
;
; this also shows how to use
; sets of integers and lists of integers with many elements
;
; solution by Grom PE
;
; benchmark:
; equ list, max 180 = 2.1 seconds
; equ list, max 200 = out of memory
; dword list, max 180  = 0.2 seconds
; dword list, max 200  = 0.2 seconds
; dword list, max 2000 = 24.3 seconds
; dword list, max 4000 = 112.6 seconds (12 million elements)

macro display_int value
temp = value
if temp<0
display '-'
temp = -temp
end if
virtual at 0
if temp=0
db '0'
end if
while temp>0
d = '0' + temp mod 10
db d
temp = temp / 10
end while
repeat \$
display d
end repeat
end virtual
end macro

macro set_count b
result = 0
if b
rept bsr b-bsf b+1, n:bsf b
if b and (1 shl n)
result = result + 1
end if
end rept
end if
end macro

macro set_pop b
result = 0
if b
result = bsf b + 1
b = b and not (1 shl (result-1))
else
err 'the set is empty'
result = -1
end if
end macro

max = 13
virtual at 0
rd max/2
l: dd 1 ; starting list: [1]
lap = l ; first element pointer
la = 1 ; first element value
lz = 1 ; last element value
rept max-1,n:2 ; add numbers 2 ... max
s = 1 shl (n-1) - 1 ; set that contains numbers 1 to (n-1)
if ~ n mod 2 ; if n is even
; exclude first and last list elements from the set
s = s and not (1 shl (la-1)) and not (1 shl (lz-1))
; extend list from its start
lap = lap - 4
store n:dword at lap
la = n
end if
set_count s
if result mod 2 ; if set's length is odd
set_pop s
; append to list
dd result,n
lz = n
end if
if s and (1 shl (la-1)) ; if set contains first list element
; exclude first and last list elements from the set
s = s and not (1 shl (la-1)) and not (1 shl (lz-1))
dd n,la
lz = la
end if
while s
set_pop s
dd result,n
set_pop s
dd result
lz = result
end while
end rept
lzp:

; display the list
p = lap
while p < lzp
if %>1
display ' '
end if
display_int x
p = p + 4
end while
end virtual    ```

Attached same code for convenience.

 Description: fasmg puzzle showing sets of integers and lists of integers with many elements Download Filename: puzzle_int_set_list.asm Filesize: 2.3 KB Downloaded: 448 Time(s)

21 Sep 2016, 12:07
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum