flat assembler
Message board for the users of flat assembler.

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

Author
Thread Post new topic Reply to topic
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 $
      load d byte from $-%
      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
  load x dword from p
  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)

Post 21 Sep 2016, 12:07
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.