flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Displaying constant values

Author
Thread Post new topic Reply to topic
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX
How can I design a displayNum macro that displays a given number in all of the following conditions

The following code
Code:
macro displayNum num
{
    display `num,13,10
}


; Works
displayNum 123

; Doesent Work
x equ 123
displayNum x

; Doesn't work
y = 123
displayNum y

; Doesn't work
z:  ; A label
displayNum z
    


Gives me the following output
Code:
flat assembler  version 1.73.02  (16384 kilobytes memory)
123
x
y
z
1 passes, 0 bytes.
    
Post 18 Sep 2021, 13:21
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3307
Location: vpcmipstrm
bitRAKE

_________________
¯\(°_o)/¯ unlicense.org
Post 18 Sep 2021, 15:02
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX
That would work if I only want to display a value. What about the following situation? Is there a way to reliably use the ` operator to convert a processor value to a string? For example when using the following code...
Code:
macro dbNum num
{
    db `num,0
}

; Case 1
dbNum 123

; Case 2
y = 123
dbNum y

; Case 3
z equ 123
dbNum z
    


Case 1 assembles into to what I want but cases 2 and 3 do not
Code:
db "123",0 ; Case 1
db "y",0   ; Case 2
db "z",0   ; Case 2
    
Post 18 Sep 2021, 23:18
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 161
Location: Russia
macomics
Code:
; Case 2
y = 123
dbNum y

; Case 3
z equ 123
dbNum z
    


So this is how it works. In these cases, the preprocessor does not operate with constants, but with label names. So everything is correct.

https://flatassembler.net/docs.php?article=manual#2.3.3 wrote:
Also conversion of name(not value!) into a quoted string is possible, with the ` operator, which likewise can be used inside the macroinstruction. It converts the name that follows it into a quoted string - but note, that when it is followed by a macro argument which is being replaced with value containing more than one symbol, only the first of them will be converted, as the ` operator converts only one symbol that immediately follows it.


Try it like this

Code:
macro     dbNum directive, [Val]
{
 if Val <> 0
  Acc=Val
  Divider=1000000000000000000
  Highest0=0
  if Acc < 0
   directive "-"
  end if
  repeat 19
   Digit= Acc/Divider
   Acc= Acc mod Divider
   Divider= Divider/10
   if Digit <> 0
    Highest0= 1
   end if
   if Highest0 = 1
    directive Digit+"0"
   end if
  end repeat
 else
  directive "0"
 end if
 if directive in <db, du>
 directive 0
 else
 directive 0xa
 end if
}

y=123
z equ 123

dbNum db, 123, y, z
    
Post 19 Sep 2021, 00:24
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7938
Location: Kraków, Poland
Tomasz Grysztar
rCX wrote:
Case 1 assembles into to what I want but cases 2 and 3 do not

For case 3 (which uses a preprocessor-stage symbol) it is possible to extract the numeric value from variable with help of REPT:
Code:
z equ 123
rept 1 c:z { db `c }    
and you could make it into a macro like this:
Code:
macro dbNum num { rept 1 c:num \{ db \`c \} }    


But in case 2 it is not possible, because "=" definitions are evaluated at assembly stage, when the final code is being resolved, and all the preprocessing is already finished by then. (It is possible in fasmg, which combines preprocessing and assembly into a unified process - there you can use the same REPT trick for both EQU and "=" definitions.) For such cases you need assembly-times loop for number conversion, as shown by others in this thread.
Post 19 Sep 2021, 07:34
View user's profile Send private message Visit poster's website Reply with quote
rCX



Joined: 29 Jul 2007
Posts: 172
Location: Maryland, USA
rCX
OK makes sense. Thanks everyone
Post 22 Sep 2021, 02: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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.