flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [CALM] Error when stringify negative number

Author
Thread Post new topic Reply to topic
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak
Code:
calminstruction (name) tostring text&
    local value
    transform text
    compute value, +text
    arrange value, value
    stringify value
    publish name, value
end calminstruction

a tostring -1000000000
display a


flat assembler  version g.j6ia
value
test.asm [16]:
        a tostring -1000000000
tostring [4] (CALM)
Error: the value of symbol 'tostring:value' is not valid for this use.
    


Why negative numbers cannot be stringified?
Post 05 May 2021, 22:04
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7933
Location: Kraków, Poland
Tomasz Grysztar
The error is given by ARRANGE, not STRINGIFY (and it is a documented limitation of ARRANGE). The routine that converts numeric value to a decimal token only works for non-negative values (this also affects the numeric parameters defined by REPEAT/REPT, for example).
Post 05 May 2021, 22:29
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 7933
Location: Kraków, Poland
Tomasz Grysztar
I have this snippet laying around that I can show as a demonstration of how I've been doing it myself. It uses the inline macro package but replaces the definitions of inline macros with CALM implementations:
Code:
include 'inline.inc'

inlinemacro dec?()
end inlinemacro

calminstruction (return) inlinemacro.dec? number*
        compute number,+number
        check number < 0
        jyes negative
        arrange number,number
        jump ready
    negative:
        compute number,-number
        arrange number,-number
    ready:
        stringify number
        publish return,number
end calminstruction

inlinemacro hex?()
end inlinemacro

calminstruction (return) inlinemacro.hex? value*
       local buffer
       compute buffer, ''
       compute value, +value
   loop:
       compute buffer, string (buffer shl 8) + '0123456789ABCDEF' shr (value shl 3 and 1111000b) and 0FFh
       compute value, value shr 4
       check value = 0
       jyes done
       check value = -1
       jno loop
       compute buffer, string (buffer shl 32) + 'F..F'
   done:
       publish return, buffer
end calminstruction 


Value = 0xcafe

display 'Dec: ',DEC(Value),10,'Hex: ',HEX(Value)    
Post 05 May 2021, 22: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
Thank you for the tip. This helped a lot.
Post 05 May 2021, 23:04
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.