flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Generate code by condition

Author
Thread Post new topic Reply to topic
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 16 Jan 2021, 18:47
Code:
org     $100

macro get_v rv
{
  local reg, ofs, init
  reg equ rv
  ofs = 0
  init = 1

  match r==val, rv \{
    reg equ r
    if (val)-128 <= v & (val)+127 >= v
      init = 0
      ofs = v-val
    end if
  \}

  if init
    mov reg,v
  end if

  mov ax,[reg+ofs]
}

get_v bx+si=$100
v = $    
Result:
Code:
get_v bx+si=$100
1.fasm [17] get_v [13]:
    mov reg,v
processed: mov bx+si,v
error: invalid operand.    

What should I change to make this work?
If works perfect with argument bx=$100 (and doesn't generate mov bx,v).
Post 16 Jan 2021, 18:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20594
Location: In your JS exploiting you and your system
revolution 16 Jan 2021, 22:30
What do you want it to do?
Post 16 Jan 2021, 22:30
View user's profile Send private message Visit poster's website Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 17 Jan 2021, 00:23
I want to check parameter and if it satisfies the conditions, then use it (or another parameter) in instruction.
The problem is that if check fails, instruction will be illegal (mov reg,v will be illegal if reg is bx+si but it shouldn't be generated if parameter is set to e.g. bx+si=$100 and v is near to $100).
Compiler raises an error in that case (if condition contains refer to variable), even though instruction shouldn't be generated!
I can't use match for condition, because it contain math calculations.
Post 17 Jan 2021, 00:23
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20594
Location: In your JS exploiting you and your system
revolution 17 Jan 2021, 03:30
Please show the output you expect:
Code:
get_v bx=$100 ; generate what code?
get_v bx+si=$100 ; generate what code?    
Post 17 Jan 2021, 03:30
View user's profile Send private message Visit poster's website Reply with quote
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 17 Jan 2021, 08:00
Jin X: v is assigned a value after the macro call. If you swap the last two lines, it should work.
Post 17 Jan 2021, 08:00
View user's profile Send private message Reply with quote
Jin X



Joined: 06 Mar 2004
Posts: 133
Location: Russia
Jin X 18 Jan 2021, 18:32
revolution,
Code:
mov ax,[bx+v-$100]
mov ax,[bx+si+v-$100]    

Using get_v and get_v bx=$1000 should generate:
Code:
mov bx,v
mov ax,[bx]    


Calanor wrote:
Jin X: v is assigned a value after the macro call. If you swap the last two lines, it should work.
Thanks. But I don't want to swap. I want it to work in any case.
Post 18 Jan 2021, 18:32
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4211
Location: vpcmpistri
bitRAKE 18 Jan 2021, 20:02
I'm still having trouble understanding fully your desired output, but perhaps:
Code:
org     $100

macro get_v rv {
  local modreg, ofs

  match ,rv \{
    mov bx,v
  \}

  match any,rv \{
    modreg equ rv
    ofs = 0
    match r==val, rv \\{
      modreg equ r
      if (val)-128 <= v & (val)+127 >= v
        ofs = v-val
      end if
    \\}
    mov ax,[modreg+ofs]
  \}
}

get_v bx+si=$100
v = $    
Baking the BX register within the macro might not be desired, but I can see no other way given your implied use.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 18 Jan 2021, 20:02
View user's profile Send private message Visit poster's website Reply with quote
Calanor



Joined: 19 Jul 2015
Posts: 45
Location: Sweden
Calanor 18 Jan 2021, 20:05
OK, I think I might have misunderstood what you were trying to do. Well, try replacing "mov reg, v" with "mov word[reg], v".
Post 18 Jan 2021, 20:05
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.