flat assembler
Message board for the users of flat assembler.

 Index > Macroinstructions > Unusual behavor
Author
Mike Gonta

Joined: 26 Dec 2010
Posts: 243
Mike Gonta 15 Oct 2017, 14:44
Here is a simple example macro which converts a binary number to hexadecimal text.
Code:
```macro bin2hex count, data
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 2, 0x55    `
creates this:
Code:
`55    `
And this:
Code:
`  bin2hex 2, (0x55 * 2)    `
creates this:
Code:
`AA    `
However, this
Code:
`  bin2hex 2, 0x55 * 2    `
creates this:
Code:
`0A    `
fasmg and fasm.

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

https://mikegonta.com
15 Oct 2017, 14:44
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 15 Oct 2017, 15:02
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.
15 Oct 2017, 15:02
Mike Gonta

Joined: 26 Dec 2010
Posts: 243
Mike Gonta 15 Oct 2017, 15:23
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

https://mikegonta.com
15 Oct 2017, 15:23
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 15 Oct 2017, 15:25
Mike Gonta wrote:
Code:
`value = +K    `
What is the significance of the "+".
If the argument is a string, "+" converts it to number.
15 Oct 2017, 15:25
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum