Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code_asm: add ability to align the code #210

Open
wtfsck opened this issue Oct 24, 2021 · 4 comments
Open

code_asm: add ability to align the code #210

wtfsck opened this issue Oct 24, 2021 · 4 comments

Comments

@wtfsck
Copy link
Member

wtfsck commented Oct 24, 2021

#207

@marcospampi
Copy link

is there any workaround you recommend?

@Tyler-IN
Copy link

Tyler-IN commented Jan 28, 2023

Would be nice to have some alignment directives without having to run the encoder to get the instruction (data) sizes.

@Tyler-IN
Copy link

Tyler-IN commented Jan 28, 2023

    internal static void int3_align(this Assembler c, int alignment)
    {
        if (c.Instructions.Count == 0)
            return;
        var cw = new SizingCodeWriter();
        c.Assemble(cw, 0);
        var length = cw.Length;
        var pad = alignment - length % alignment;
        for (var i = 0; i < pad; ++i)
            c.int3();
    }
    internal static void nop_align(this Assembler c, int alignment)
    {
        if (c.Instructions.Count == 0)
            return;
        var cw = new SizingCodeWriter();
        c.Assemble(cw, 0);
        var length = cw.Length;
        var pad = alignment - length % alignment;
        if (pad > 0)
            c.nop(pad);
    }
public sealed class SizingCodeWriter : CodeWriter
{
    public int Length { get; private set; }
    public override void WriteByte(byte value) => ++Length;
    public void Reset() => Length = 0;
}

These are some mediocre hacks that serve good enough for my use cases.

@Tyler-IN
Copy link

Tyler-IN commented Jan 31, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants