flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Macroinstructions > Unusual behavor

Author
Thread Post new topic Reply to topic
Mike Gonta



Joined: 26 Dec 2010
Posts: 202
Unusual behavor
Here is a simple example macro which converts a binary number to hexadecimal text.

Code:
macro bin2hex countdata
  local digit
  repeat count
    digit = (data shr ((count * 4 - 4) - (% - 1) * 4)) and 0x0F
    digit = digit + '0'
    if digit > '9'
      digit = digit + 7
    end if
    db digit
  end repeat
end macro

This:

Code:
  bin2hex 20x55

creates this:

Code:
55

And this:

Code:
  bin2hex 2, (0x55 * 2)

creates this:

Code:
AA

However, this

Code:
  bin2hex 20x55 * 2

creates this:

Code:
0A

fasmg and fasm.

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com
Post 15 Oct 2017, 14:44
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6676
Location: Kraków, Poland
Remember that macro parameters are just a simple substitution of text. So if you put a parameter into an expression, you may end up with text that computes to something else than what you imagined:

Code:
digit = (0x55 * 2 shr ((2 * 4 - 4) - (1 - 1) * 4)) and 0x0F

If you need to use the value of a parameter in an expression, either enclose it with parentheses, or use a proxy variable.
Post 15 Oct 2017, 15:02
View user's profile Send private message Visit poster's website Reply with quote
Mike Gonta



Joined: 26 Dec 2010
Posts: 202

Tomasz Grysztar wrote:
If you need to use the value of a parameter in an expression, either enclose it with parentheses, or use a proxy variable.


Code:
value = +K

What is the significance of the "+".

_________________
Mike Gonta
look and see - many look but few see

http://mikegonta.com
Post 15 Oct 2017, 15:23
View user's profile Send private message Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 6676
Location: Kraków, Poland

Mike Gonta wrote:

Code:
value = +K

What is the significance of the "+".

If the argument is a string, "+" converts it to number.
Post 15 Oct 2017, 15:25
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.