flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > FASMG: push_regs

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 02 May 2023, 15:58
Hi,

I'm starting with ARM64 code using thsq include file.

I want to implement a "push/pop" macro that accepts multiple registers such as:

Code:
push x0 x1 x2 x3 x4.....

pop  x4, x3, x2, x1, x0
    


I can create a "push" macro that accepts one argument but I'm lost accepting many of arguments:

Code:
macro push reg
    str reg, [sp, -8]!
end macro 

macro pop reg
    ldr reg, [sp], 8
end macro 
    


Any help?

Thanks!
Post 02 May 2023, 15:58
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 02 May 2023, 16:06
If your multiple arguments are always going to be plain names, you can split them with a simple MATCH in a loop, like this:
Code:
calminstruction push? sequence*&
        local item
    loop:
        match item sequence?, sequence
        jno done ; if match failed, the remaining sequence was empty
        arrange item, =str item, [=sp, -8]!
        assemble item
        jump loop
    done:
end calminstruction    
You can also check the variant I used in the x86 implementation, which even further limits what kind of arguments are allowed (with help of two-argument TRANSFORM to detect specific set of names).
Post 02 May 2023, 16:06
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 02 May 2023, 17:03
Thanks Tomasz! It worked great!

I just realized that the stack needs to be 16-byte aligned, so the ideal would be to insert two registers at once, like:

Code:
stp   x1, x2, [sp, 16]!
    


I'm not sure if that could be covered in fasmg where I read "pairs" of parameters, so something like:

Code:
push x1 x2 x3 x4
    

would generate:

Code:
stp   x1, x2, [sp, 16]!
stp   x3, x4, [sp, 16]!
    


Thanks again!
Post 02 May 2023, 17:03
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 02 May 2023, 17:15
Just code the loop accordingly:
Code:
calminstruction push? sequence*&
        local item, item2
    loop:
        match item sequence?, sequence
        jno done
        match item2 sequence?, sequence
        jno single
        arrange item, =stp item, item2, [=sp, 16]!
        assemble item
        jump loop
    single:
        arrange item, =str item, [=sp, -8]!
        assemble item
    done:
end calminstruction    
You can structure CALM code in a way similar to how you would write assembly code - it was intended this way.
Post 02 May 2023, 17:15
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 02 May 2023, 20:33
Amazing Tomasz!

Thanks a million!
Post 02 May 2023, 20:33
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.