flat assembler
Message board for the users of flat assembler.

Index > Programming Language Design > [fasmg] help understand if ns.imm eq ns.imm element 1

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 28 Apr 2016, 14:23
I'm trying to understand fasmg functionality and capabilities following examples.
In 8086.inc example, macro x86.parse_operand_value has the following condition:
Code:
if ns.imm eq ns.imm element 1    

Could you please clarify how this works?

I have elements defined:
Code:
element x86.reg
element x86.r8 : x86.reg + 1
element x86.r16 : x86.reg + 2

element al? : x86.r8 + 0
element cl? : x86.r8 + 1
element dl? : x86.r8 + 2
element bl? : x86.r8 + 3
element ah? : x86.r8 + 4
element ch? : x86.r8 + 5
element dh? : x86.r8 + 6
element bh? : x86.r8 + 7    

let's say we have "add al, 2" instruction
for destination operand, ns.imm, if I understand correctly, will be equal to al.
Then "ns.imm element 1" extracts variable from the ns.imm poly. This will be "get variable from poly al", which is al
Then we compare variable with itself....

Oh, I think I got the idea while I typed this question Smile
If we can extract a variable from the given poly, then expression is correct. If such register is not defined, then we won't be able extract the variable. Right? It's like "check if element is defined". Please correct me if I'm wrong.

BTW, as I understood, element 1 is always a variable to get, element 0 is constant, which equals to 0 by default. But can there be element 2 or more? If yes, when can this happen?
Thanks in advance!
Post 28 Apr 2016, 14:23
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8395
Location: Kraków, Poland
Tomasz Grysztar 28 Apr 2016, 16:37
zhak wrote:
I'm trying to understand fasmg functionality and capabilities following examples.
In 8086.inc example, macro x86.parse_operand_value has the following condition:
Code:
if ns.imm eq ns.imm element 1    

Could you please clarify how this works?
The "ns.imm element 1" expression extracts the first variable of ns.imm polynomial, with the coefficient 1. This is equal to the value of ns.imm when ns.imm consists of just a single element, like "ebx". The value like "ebx*2" or "ebx+eax" does not fulfill such condition (because "element 1" is then simply "ebx"), and neither does a plain number other than zero (because "element 1" of a scalar value is zero).

The other, more rigorous way to check for such condition is "if ns.imm scale 0 = 0 & ns.imm scale 1 = 1 & ns.imm scale 2 = 0". This would also be true for expressions like "ebx", but not for "ebx*2" (because then "scale 1" would be 2) nor "ebx+eax" (because then "scale 2" would be 1). Note that this variant does not pass the "0" value like the "if ns.imm eq ns.imm element 1" does, because for the zero value the "scale 1" is also zero. But in "8086.inc" this difference does not matter, because there are further restrictions on the "metadata 1" value.

zhak wrote:
BTW, as I understood, element 1 is always a variable to get, element 0 is constant, which equals to 0 by default. But can there be element 2 or more? If yes, when can this happen?
Let me demonstrate it with a simple table:
Code:
value           scale 0         element 1       scale 1         element 2       scale 2         element 3       scale 3

ebx             0               ebx             1               0               0               0               0
ebx*2           0               ebx             2               0               0               0               0
ebx+2           2               ebx             1               0               0               0               0
ebx+eax         0               ebx             1               eax             1               0               0
eax+ebx*2+1     1               eax             1               ebx             2               0               0
eax+ebx+ecx     0               eax             1               ebx             1               ecx             1    
The "element 0" is always 1. Any value "v" can be represented as "(v scale 0)*(v element 0)+(v scale 1)*(v element 1)+(v scale 2)*(v element 2)+..."
Post 28 Apr 2016, 16:37
View user's profile Send private message Visit poster's website Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 28 Apr 2016, 22:42
Tomasz, thank you for such a detailed explanation! Now everything looks clear and... simple Smile
Post 28 Apr 2016, 22:42
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.