flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > display polynome internals: variable term used where not exp

Author
Thread Post new topic Reply to topic
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 23 Jul 2017, 07:53
How to extract polynome internals not into if-checks but into display?
Code:
macro displaypoly arg
  display (0 elementof arg)
  display 13,10
end macro

displaypoly eax+'b'
;shows "c"

macro displaypoly arg
  display (0 metadataof arg)
  display 13,10
end macro

displaypoly eax+'b'
;shows "b"
    

If I used 1 instead of 0 in both cases I got error: variable term used where not expected.

But how to display separate 4 values "x86.r32", "0", "x86.reg", "4", that term eax has inside of self.


Code:
macro displaypoly arg
  disphex (1 metadataof arg) - x86.r32-(0 metadataof arg),1 ;extract x86.r32 index
  display ' - index of ',`arg,' in x86.r32',13,10
  disphex 1 metadataof (1 metadataof arg) - x86.reg,1 ;extract x86.reg index
  display ' - index of ',`arg,' in x86.reg',13,10
  ;how extract x86.r32 & x86.reg theyselfs? to make macro polynome independent? now macro expects some of descendants of x86.r32
  ;in display output index of esi+'a' is wrong - should be just index of esi - how to fix that?
end macro

displaypoly esi+'a' ;ok
displaypoly esi*2+'a' ;error     

_________________
I don`t like to refer by "you" to one person.
My soul requires acronim "thou" instead.
Post 23 Jul 2017, 07:53
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 23 Jul 2017, 08:49
If you want to display complete tree of the metadata information, you need to use recursion:
Code:
macro displaypoly poly
        local buffer,number
        buffer = poly
        number = buffer scale 0
        if number < 0
                display '-'
                number = -number
        end if
        repeat 1, n:number
                display `n
        end repeat
        repeat elementsof buffer
                number = buffer scale %
                if number < 0
                        display '-'
                        number = -number
                else
                        display '+'
                end if
                if number <> 1
                        repeat 1, n:number
                                display `n,'*'
                        end repeat
                end if
                display '['
                displaypoly buffer metadata %
                display ']'
        end repeat
end macro

displaypoly eax+2    
This is probably still not good enough - when an element does not have a metadata it is shown simply as "[0]". As elements by themselves do not have any intrinsic meaning to the assembler, you need to somehow distinguish them yourself, this obviously would depend on what kind of elements yo expect to encounter:
Code:
macro displaypoly poly
        local buffer,number
        buffer = poly
        number = buffer scale 0
        if number < 0
                display '-'
                number = -number
        end if
        repeat 1, n:number
                display `n
        end repeat
        repeat elementsof buffer
                number = buffer scale %
                if number < 0
                        display '-'
                        number = -number
                else
                        display '+'
                end if
                if number <> 1
                        repeat 1, n:number
                                display `n,'*'
                        end repeat
                end if
                if buffer metadata %
                        display '['
                        displaypoly buffer metadata %
                        display ']'
                else if buffer element % eq x86.reg
                        display 'x86.reg'
                else
                        display 'unknown'
                end if
        end repeat
end macro

displaypoly eax+ebx*2+3    
Post 23 Jul 2017, 08:49
View user's profile Send private message Visit poster's website Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 23 Jul 2017, 09:31
Thanks.
Perfect - I tested on polynomes other than general registers too.
One more question polynome element base can be send to display in anyway?
I mean value of (buffer element %) extracted itself and displayed?
Post 23 Jul 2017, 09:31
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 23 Jul 2017, 10:33
Element by itself has no value, so there is nothing to display there.
Post 23 Jul 2017, 10:33
View user's profile Send private message Visit poster's website Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 23 Jul 2017, 11:09
If I override element macro itself and force defining values

Code:
macro element arg&
  element arg
  match elem:bs index,arg
    elem.name equ elem
    elem.base equ bs
  else match elem,arg
    elem.name equ elem
    elem.base equ 
  end if
end macro    


could (buffer element %).name or (buffer element %).base become available?
Post 23 Jul 2017, 11:09
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8367
Location: Kraków, Poland
Tomasz Grysztar 23 Jul 2017, 12:51
The variable defined by ELEMENT is like a value of a symbol, and you cannot get back to symbol from the value.
Code:
a = 2 ; this defines "a" with known constant value 2
element b ; this defines "b" with unknown (variable) value X
c = a + b ; this defines "c" with value "1+X"
; note that "c" no longer has any information about "a" or "b", they were only used to compute the value    
Therefore when you analyze polynomial variables, you can only extract their metadata or compare two variables to see if they are the same one.

So all you could do would be to define the elements in such a way that they would contain some information for displaying purposes in their metadata. For example you could catch the definitions of elements with no metadata and insert your own metadata there:
Code:
element metaname : 'metaname' + metaname

macro element? definition
        match name:metadata, definition
                element name:metadata
        else
                element definition:`definition + metaname
        end match
end macro

; ...

macro displaypoly poly
        local buffer,number
        buffer = poly
        number = buffer scale 0
        if number < 0
                display '-'
                number = -number
        end if
        repeat 1, n:number
                display `n
        end repeat
        repeat elementsof buffer
                number = buffer scale %
                if number < 0
                        display '-'
                        number = -number
                else
                        display '+'
                end if
                if number <> 1
                        repeat 1, n:number
                                display `n,'*'
                        end repeat
                end if
                if buffer metadata % relativeto metaname
                        display string buffer metadata % scale 0
                else
                        display '['
                        displaypoly buffer metadata %
                        display ']'
                end if
        end repeat
end macro    
Because metadata can itself be a polynomial, you can even combine multiple terms for holding various kinds of information.
Post 23 Jul 2017, 12:51
View user's profile Send private message Visit poster's website Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 817
Location: Russian Federation, Sochi
ProMiNick 23 Jul 2017, 14:33
Mostly for myself: the way additional info can be added so it don`t touches any behavior of standart macros that is addition as multiplier. Just affect macros described in this post.
Code:
element al? : x86.r8*'al' + 0
element cl? : x86.r8*'cl' + 1
element dl? : x86.r8*'dl' + 2
element bl? : x86.r8*'bl' + 3
element ah? : x86.r8*'ah' + 4
element ch? : x86.r8*'ch' + 5
element dh? : x86.r8*'dh' + 6
element bh? : x86.r8*'bh' + 7     
Post 23 Jul 2017, 14:33
View user's profile Send private message Send e-mail Reply with quote
m_stery



Joined: 08 Nov 2024
Posts: 3
m_stery 14 Mar 2025, 10:04
Another solution would be to store the symbols under a numerical name and the polynomial with the corresponding coefficients.
Example written using calm language (elements+area labels):
Code:
calminstruction calminstruction?.initsym? var*,val&
        publish var,val
end calminstruction

define polynomial_debug? polynomial_debug?

namespace polynomial_debug?
        define  elemnames elemnames
        polyids = 0

        calminstruction element? decl&
                local   sym,name,mdata,i
                arrange name,decl
                arrange decl,=element decl
                assemble decl
                match   name:mdata,name
                compute polyids,polyids+1+(1+0 scaleof polyids)*name 
                compute i,0 scaleof polyids
                arrange sym,elemnames.i?
                publish sym:,name
        end calminstruction

        calminstruction (name) arealabel? decl&
                local   sym,mdata,any,i
                match   ::any?,decl
                arrange decl,name decl
                assemble decl
                jyes    area_label
                exit
            area_label:
                compute polyids,polyids+1+(1+0 scaleof polyids)*name 
                compute i,0 scaleof polyids
                arrange sym,elemnames.i?
                publish sym:,name
        end calminstruction

;returns a polynomial with the same elements but coefficients corresponding to the numerical name
        calminstruction polynomial_ids? dest*,src*
                local   value,ivalue,rvalue,noe,i,eid
                compute value,src
                compute noe,elementsof value
                compute ivalue,0
                compute i,1
            elem:       
                check   i<=noe
                jno     ready
                compute ivalue,ivalue+i elementof value
                compute i,i+1
                jump    elem
            ready:      
                compute ivalue,ivalue+polyids
                compute i,1
                compute rvalue,0
            getid:
                check   i<=noe
                jno     done
                compute eid,(i scaleof ivalue)-1
                check   eid=0
                jyes    unknownid
                compute rvalue,rvalue+eid*i elementof ivalue
                compute i,i+1
                jump    getid
            unknownid:
                compute rvalue,rvalue-i elementof ivalue
                compute i,i+1
                jump    getid
            done:
                publish dest,rvalue
        end calminstruction

        calminstruction polynomial_text_ids? dest*,src*
                local   value,ivalue,rvalue,noe,i,sym,eid,coef
                initsym sym,ivalue
                call    polynomial_ids,sym,src
                compute value,src
                compute noe,elementsof value
                compute coef,0 scaleof value
                check   coef<0
                jyes    minus_sign0
                arrange rvalue,+coef
                jump    ready
            minus_sign0:
                compute coef,-coef
                arrange rvalue,-coef
            ready:
                compute i,1
            loop:
                check   i<=noe
                jno     done
                compute eid,i scaleof ivalue
                compute coef,i scaleof value
                compute i,i+1
                check   coef<0
                jyes    minus_sign
                arrange rvalue,rvalue+coef
                jump    elname
            minus_sign:
                compute coef,-coef
                arrange rvalue,rvalue-coef
            elname:
                check   eid>0
                jyes    valid_id
                arrange rvalue,rvalue*?
                jump    loop
            valid_id:
                arrange rvalue,rvalue*?eid?
                jump    loop
            done:
                transform rvalue,elemnames
                publish dest,rvalue
        end calminstruction
end namespace

mvmacro element?,polynomial_debug?.element?
mvstruc ?,polynomial_debug?.arealabel?
    


and example:
Code:
calminstruction symdisplay? var*
        local   tmp
        arrange tmp,var
        transform tmp
        arrange tmp,var==tmp
        stringify tmp
        display tmp bappend 10
end calminstruction

include 'packages/fasm2/fasm2.inc'
direct::        db 'cba'
define sr
someterm = ax - bx + 3*cs - 100*eax + direct
polynomial_debug.polynomial_text_ids sr,someterm
symdisplay sr   ;display: sr=+0+1*ax?-1*bx?+3*cs?-100*eax?+1*direct
    
Post 14 Mar 2025, 10:04
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.