flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > [sharing] Functions via Macros

Author
Thread Post new topic Reply to topic
Overclick



Joined: 11 Jul 2020
Posts: 391
Location: Ukraine
Overclick
Hi
I want to share my new supermacro ideas: What macro doing? It inserts some part of the code as many times as we use it. Let's stop it. We can insert some part as function just once and call it as many times as we need.
Code:
macro   .function name
        {
        if name = 0
                jmp             once#name
                name    = $
                push    rbp
        }
macro   .endf name
        {
                pop             rbp
                ret 0
                once#name:
        end if
        if ~ name = 0
                call    name
        end if
        }
    

Example of declaring:
Code:
ConvertRegToASCII = 0
macro   Convert key,Dest_any,Source_any
        {
        if key eq RegToASCII
                mov             rdi,Dest_any
                if Source_any eq rax
                else
                        mov     rax,Source_any
                end if
                .function ConvertRegToASCII
                        mov     rbx,10
                        xor             rcx,rcx
                        @@:
                                xor     rdx,rdx
                                div     rbx
                                add     dl,30h
                                push    rdx
                                inc             rcx
                                cmp     rax,0
                                jne             @B
                        @@:
                                pop             rdx
                                mov     BYTE[rdi],dl
                        inc     rdi
                        dec             rcx
                        cmp     rcx,0
                        jne             @B
                    mov     BYTE[rdi],0
                .endf ConvertRegToASCII
        end if
        }    

And now if you use your macro more than once it will NOT duplicate the code.
Convert RegToASCII,txtMonitor1,r10
Convert RegToASCII,txtMonitor2,r11
Convert RegToASCII,txtMonitor3,r12
Convert RegToASCII,txtMonitor4,r13


You also may call your newly created function as usually do by
call ConvertRegToASCII

push/pop rbp and ret 0 required for compatibility with proc


Last edited by Overclick on 09 Apr 2021, 09:15; edited 2 times in total
Post 09 Apr 2021, 08:58
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18182
Location: In your JS exploiting you and your system
revolution
Thanks for sharing.

I've moved it to the Macroinstructions forum.
Post 09 Apr 2021, 09:04
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 391
Location: Ukraine
Overclick
Could you move here my previous sharing please (.multisection)
Post 09 Apr 2021, 09:08
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18182
Location: In your JS exploiting you and your system
revolution
Link?
Post 09 Apr 2021, 09:09
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 391
Location: Ukraine
Overclick
Post 09 Apr 2021, 09:12
View user's profile Send private message Visit poster's website Reply with quote
Overclick



Joined: 11 Jul 2020
Posts: 391
Location: Ukraine
Overclick
A better example:
Code:
dataConvertToASCII = 0
ConvertToASCII = 0
macro   ConvertToASCII dest,reg,mem
        {
        .dataonce dataConvertToASCII
                ConvertOperand  = $
                ConvertReset    dq ?
        .code
                if ~ mem eq 
                        mov     reg,mem
                end if
                mov     [ConvertReset],0
                mov     [ConvertOperand],reg
                mov     rdi,dest
        .function ConvertToASCII
                mov     rbx,10
                mov     rax,[ConvertOperand]
                xor     rcx,rcx
                @@:
                        xor     rdx,rdx
                        div     rbx
                        add     dl,30h
                        push    dx
                        inc     rcx
                        cmp     rax,0
                        jne     @B
                @@:
                        pop     dx
                        mov     [rdi],dl
                        inc     rdi
                        dec     rcx
                        cmp     rcx,0
                        jne     @B
                mov     BYTE[rdi],0
        .endf ConvertToASCII
        }    
Post 15 Apr 2021, 05:01
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


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.