flat assembler
Message board for the users of flat assembler.

Index > Main > Remove trailing b when representing binary

Author
Thread Post new topic Reply to topic
donn



Joined: 05 Mar 2010
Posts: 321
donn 11 Jul 2018, 05:43
Just out of curiosity, is it possible to remove db and replace b with a space on a line in fasm1?

For example, instead of:

Code:
        mov r8, [rax]
        db 10111000b,0b,0b,0b,0b                                ; 0b8h,0b,0,0,0 mov rax, 0b
        cmp r8, rax

    


This:

Code:
        mov r8, [rax]
        LINE_START equ db                               ; Define start of line with db 
        SPACE equ ,                                     ; Define space as comma
        10111000 0 0 0 0                                ; 0b8h,0b,0,0,0 mov rax, 0b
        restore LINE_START                              ; Restore definition
        restore SPACE                                   ; Restore definition
        cmp r8, rax
    


Doesn't provide any functional difference, but would just format binary without the trailing b's and would remove the preceding db. Sometimes I forget to include the b and it becomes 10 (ten). Multiple lines beginning with the same db would also look simpler. I need to dabble more in these types of directives and macros. They really can enhance productivity.

Also, apologize if this was asked before, did not see a post yet when searching.
Post 11 Jul 2018, 05:43
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 11 Jul 2018, 05:50
Nevermind, this was addressed here:

https://board.flatassembler.net/topic.php?p=195977

Not sure if this applies yet, but will read through it.
Post 11 Jul 2018, 05:50
View user's profile Send private message Reply with quote
ProMiNick



Joined: 24 Mar 2012
Posts: 812
Location: Russian Federation, Sochi
ProMiNick 11 Jul 2018, 07:09
Code:
macro bins vals& {
  irps val,vals \{
    match a,val\#b \\{
      ;if a>$FFFFFFFFFFFFFFFFFFFF
      ;  display "fasm supports 129bit",13,10
      ;  display "for internaly calcs,",13,10
      ;  display "but not for data definition",13,10
      ;  ddq a ;.err ; but ddq a same as .err ,no ddq in fasm1
      ;else
      if a>$FFFFFFFFFFFFFFFF ; even more if a >$FFFFFFFFFFFFFFFF compilation will crash in comparison moment
        display "definition dt present,",13,10 ; so these wil never be shown
        display "but not operate binary,",13,10
        dt a ;.err ; but dt a same as .err, where a is not in float numeric format
      else if a>$FFFFFFFF
        dq a
      else if a>$FFFF
        dd a
      else if a>$FF
        dw a
      else
        db a
      end if
    \\}
  \}
}

    
Code:
        mov r8, [rax] 
        bins \
        10000000_0000_0000_0000_00000000_0000_0000_0000_0000_0000_0000_0000_0000_0000 1 
        cmp r8, rax     

Is this helps?

for hexes:
Code:
macro $ vals& {
  irps val,vals \{
    match a,$\#val \\{
      ;if a>$FFFFFFFFFFFFFFFFFFFF
      ;  display "fasm supports 129bit",13,10
      ;  display "for internaly calcs,",13,10
      ;  display "but not for data definition",13,10
      ;  ddq a ;.err ; but ddq a same as .err ,no ddq in fasm1
      ;else
      if a>$FFFFFFFFFFFFFFFF
        display "definition dt present,",13,10
        display "but not operate binary,",13,10
        dt a ;.err ; but dt a same as .err
      else if a>$FFFFFFFF
        dq a
      else if a>$FFFF
        dd a
      else if a>$FF
        dw a
      else
        db a
      end if
    \\}
  \}
}    
Code:
$ 5a 4d    


that one $ macro I liked myself (it looks organicaly in code and not crashing my eyes like bins does)
Post 11 Jul 2018, 07:09
View user's profile Send private message Send e-mail Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 11 Jul 2018, 17:30
Cool, that's perfect!

fasm 1.71.39 was showing
Code:
Error: reserved word used as symbol
    

on the _ but space worked fine. fasm 1.73.04 had no error with _. I like the $ and multiline support with \

Code:
        mov r8, [rax]
        $ \
        10111111 01111111               \
        01000000 11100000
        cmp r8, rax     
Post 11 Jul 2018, 17:30
View user's profile Send private message Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 27 Dec 2021, 18:48
Followup questions...
1. Converting to fasmg
2. Stripping dq and commas , from data definitions:
i.e. changing:
Code:
dq var2Addr,3951,var3Addr,22
    

to:
Code:
var2Addr 3951 var3Addr 22
    

and have them mean the same.

I tried converting to fasmg and can assemble now. Trying to figure out how to edit it to use the above 2 requirements, not the binary or hex requirements for time being:

Code:


macro $ vals&
  iterate val, vals
    match a,val
      ;if a>$FFFFFFFFFFFFFFFFFFFF
      ;  display "fasm supports 129bit",13,10
      ;  display "for internaly calcs,",13,10
      ;  display "but not for data definition",13,10
      ;  ddq a ;.err ; but ddq a same as .err ,no ddq in fasm1
      ;else
      dq a
    end match
  end iterate
end macro    
    


But getting assemble errors when using like this:

Code:
section '.data' data readable writeable align 16

someVals:
$ \
3 4 898 var2Addr
43

    


Code:
flat assembler  version g.izxz
        $ \
        3 4 898 var2Addr
macro $ [9] dq [13] qword [1] (CALM)
Error: invalid expression.
    


So, I think the macro takes in the vals, then iterates them, then matches them (no longer matching binary or hex numbers), and on each token, defines it with dq. Or, in theory, think that's what I'm trying to get macro to do.

Summarizing informally speaking: I have a lot of dq definitions that contain addresses like var2Addr and literal number values, separated by commas. Trying to just remove the need to keep typing the commas and precede block by dq since I have so many of these definitions and it's making it cluttered. I can live with it, but just curious if can be optimized. I'll stick with base 10 numbers for now, understand how to remove the b binary suffix and hex from prior examples.

Is this possible? Can I remove dq and the commas, when some tokens are address labels, or does this only work with literal values? Also, how does the macro stop executing when it's multiline with \? When there's a blank line? Is this controllable, like is there set amount of whitespace that can end a block so whitespace can be used to organize the data visually?

Lot of questions! Interesting how was looking through docs and was able to get macro to assemble with fasmg, the docs are really holding up, macros just not my forte yet, focusing on using core x86 currently with a Vulkan project.
Take care..!
Post 27 Dec 2021, 18:48
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8361
Location: Kraków, Poland
Tomasz Grysztar 27 Dec 2021, 19:30
With fasmg this is easier than with fasm 1, because you can use interceptor macros to parse complete lines:
Code:
macro $
        macro ?! line&
                match , line            ; upon seeing empty line
                        purge ?         ; stop intercepting
                else
                        local buffer
                        define buffer line
                        while 1
                                match item= tail, buffer
                                        dq item
                                        define buffer tail
                                else
                                        dq buffer       ; just one item left
                                        break
                                end match
                        end while
                end match
        end macro
end macro


someVals:
$
3 4 898 var2Addr
43

var2Addr = 400000h    
In this example backslashes are not needed at all, the interceptor just keeps consuming complete lines until it sees an empty one. You could alter it to use a different demarcation rules, as needed.

Another example, this time using CALM for the line processing, and a special syntax to stop intercepting:
Code:
struc dqs
        label . : qword
        calminstruction ?! line&
                match /=dqs, line
                jyes purge
            loop:
                local item
                match item line?, line
                jno done
                arrange item, =dq item
                assemble item
                jump loop
            purge:
                arrange line, =purge =?
                assemble line
            done:
        end calminstruction
end struc


someVals dqs
3 4 898
43
/dqs    
I hope these two are a good base to continue tweaking.
Post 27 Dec 2021, 19:30
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 27 Dec 2021, 20:56
It is too easy for fasm_1.
Tomasz, why do you still pushing fasm_g for that trivial? ))
Code:
irps bin, \ 
10111000 10111000 10111000 10111000
{ db bin#b }    
Post 27 Dec 2021, 20:56
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 27 Dec 2021, 21:06
So awesome. I commented some lines to enhance my understanding, required 3 lines to terminate the macro processing, and ran a test to verify the first couple values. Test includes printing the address of one token as well as its value:

Code:

macro $
        blankLinesReached = 0
        macro ?! line&                          ; ? and ! are special characters, similar example to multiline comment example in doc
                match , line            ; upon seeing empty line
                        if blankLinesReached < 2
                                blankLinesReached = blankLinesReached + 1
                        else
                                blankLinesReached = 0
                                purge ?         ; stop intercepting
                        end if
                else
                        blankLinesReached = 0
                        local buffer            
                        define buffer line              ; Start buffer as incoming line
                        while 1
                                match item= tail, buffer        ; "item= tail" matches an item and everything after it which goes in the buffer. buffer is updated each iteration. = matches tokens literally, like "," separators
                                        dq item
                                        define buffer tail
                                else
                                        dq buffer       ; just one item left
                                        break
                                end match
                        end while
                end match
        end macro
end macro    


Code:

someVals:
$
3 4 898 vkPipelineShaderStageCreateInfoRef.sType
43

49


331

213



    



Some output from test:
Code:
898                           ; someVals + 16
140701382084288    ; someVals + 24
18                            ; Value from address at someVals + 24
43                            ; someVals + 32
49                            ; someVals + 40
    


Very very useful. calm version and more to learn next..
Post 27 Dec 2021, 21:06
View user's profile Send private message Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 27 Dec 2021, 21:11
You can adopt my "Multisection" superior macro to do all things you need after named directive like ".binary"
Post 27 Dec 2021, 21:11
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 670
Location: Ukraine
Overclick 27 Dec 2021, 21:13
Ok, I'm out of this fasmG secta ))
Post 27 Dec 2021, 21:13
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8361
Location: Kraków, Poland
Tomasz Grysztar 27 Dec 2021, 21:50
donn wrote:
Very very useful. calm version and more to learn next..
The CALM version I posted has a small problem - it just cuts individual tokens, so it would split the name like "vkPipelineShaderStageCreateInfoRef.sType" into components, which is not what you'd expect. Instead it should use "= " to split only on actual spaces, just like the MACRO version does, but this requires structuring it a bit differently:
Code:
struc dqs
        label . : qword
        calminstruction ? line&
                match /=dqs, line
                jyes purge
            loop:
                local item
                match item= line, line
                jno last
                arrange item, =dq item
                assemble item
                jump loop
            last:
                arrange item, =dq line
                assemble item
                exit
            purge:
                arrange line, =purge =?
                assemble line
        end calminstruction
end struc


someVals dqs
3 4 898
43
/dqs    
Note that "!" can be omitted if you do not need to intercept empty lines. And this also makes it not intercept lines containing control directives, so it allows then to do things like:
Code:
someVals dqs
3 4

if some_condition
    898
end if

43
/dqs    
Post 27 Dec 2021, 21:50
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 28 Dec 2021, 04:45
I was mapping out how this calminstruction could handle X amount of blanklines to stop processing:

Code:
struc dqs
        label . : qword
        calminstruction ? line&
                ; blankLinesCount = 0
                match /=dqs, line
                jyes blanklineReached
            loop:
                local item
                match item= line, line
                jno last
                arrange item, =dq item
                assemble item
                jump loop
            last:
                arrange item, =dq line
                assemble item
                exit
            blanklineReached:
                ; if blankLinesCount < 2
                ; blankLinesCount = blankLinesCount + 1
                ; exit
                ; end if
                ; fallthrough to purge otherwise
            purge:
                ; blankLinesCount = 0 ; reset count for next time we run the calminstruction on a block from scratch
                arrange line, =purge =?
                assemble line
        end calminstruction
end struc    


but then just realized you can end the processing with /dqs so that's already done.

Again, interesting..! Still some terms to understand, but this or the macro version really reduces clutter.

Yeah, advanced macros not always needed, but they allow fine-tuning that save tons of space over time, and change the game. Saving too much space is a problem too though, if you've ever worked with Matlab or GLSL, you may see that it's hard to understand how efficient the computations are. Then at a lower-level, with things like SPIR-V, there are all of these repetitive tokens. Sometimes, there's a middle ground.

Good stuff, opens doors.
Post 28 Dec 2021, 04:45
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8361
Location: Kraków, Poland
Tomasz Grysztar 28 Dec 2021, 21:49
If you're still interested in modifying the CALM variant to allow terminating the definition with just empty lines, I finished your attempt:
Code:
struc dq$
        label . : qword
        local blankLinesCount
        blankLinesCount = 0
        calminstruction ?! line&
                match , line
                jyes blanklineReached
                compute blankLinesCount, 0
            loop:
                local item
                match item= line, line, ()
                jno last
                arrange item, =dq item
                assemble item
                jump loop
            last:
                arrange item, =dq line
                assemble item
                exit
            blanklineReached:
                check blankLinesCount < 2
                jno purge
                compute blankLinesCount, blankLinesCount + 1
                exit
            purge:
                arrange line, =purge ?
                assemble line
        end calminstruction
end struc


someVals dq$
3 4 898 (1 shl 40)
43


    
I also used the third argument of CALM MATCH to make it respect the brackets, as demonstrated with value "(1 shl 40)" in the example. The enclosed value is not further cut on spaces.
Post 28 Dec 2021, 21:49
View user's profile Send private message Visit poster's website Reply with quote
donn



Joined: 05 Mar 2010
Posts: 321
donn 20 Jan 2022, 06:02
I'll definitely follow-up on the calm version, but it will take me some time. I'm deep in using the pre-calm version and experimenting with some other techniques heavily. equ is my new secret weapon. It can replace definitions and offsets in a more shorthand version
Post 20 Jan 2022, 06:02
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.