Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 524249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:32:52+00:00 2026-05-13T08:32:52+00:00

I’m working with GNU assembler on i386, generally under 32-bit Linux (I’m also aiming

  • 0

I’m working with GNU assembler on i386, generally under 32-bit Linux (I’m also aiming for a solution under Cygwin).

I have a “stub” function:

    .align 4
stub:
    call *trampoline
    .align 4
stub2:

trampoline:
    ...

The idea is that the data between stub and stub2 will be copied into allocated memory, along with a function pointer and some context data. When the memory is called, the first instruction in it will push the address of the next instruction and go to trampoline which will read the address off the stack and figure out the location of the accompanying data.

Now, stub gets compiled to:

ff 15 44 00 00 00      call *0x44
66 90                  xchg %ax,%ax

This is a call to an absolute address, which is good because the address of the call is unknown. The padding has been turned into what I guess is a do-nothing operation, which is fine and anyway it will never be executed, because trampoline will rewrite the stack before jumping to the function pointer.

The problem is that the return address pushed by this call will point to the non-aligned xchg instruction, rather than the aligned data just past it. This means trampoline needs to correct the alignment to find the data. This isn’t a serious problem but it would be slightly preferable to generate something like:

66 90                  xchg %ax,%ax
ff 15 44 00 00 00      call *0x44
# Data will be placed starting here

So that the return address points directly at the data. The question is, then: how can I pad the instruction so that the end of it is aligned?

Edit A little background (for those who haven’t already guessed). I’m trying to implement closures. In the language,

(int -> int) make_curried_adder(int x)
{
    return int lambda (int y) { return x + y; };
}

(int -> int) plus7;
plus7 = make_curried_adder(7);
print("7 + 5 = ", plus7(5));

The { return x + y } is translated into a normal but anonymous function of two parameters. A block of memory is allocated and populated with the stub instructions, the address of the function, and the value 7. This is returned by make_curried_adder and when called will push the additional argument 7 on the stack then jump to the anonymous function.

Update

I’ve accepted Pascal’s answer, which is that assemblers tend to be written to run in a single pass. I think some assemblers do have more than one pass to deal with code like “call x; … ; x: …”, which has a forward reference. (In fact I wrote one a long time ago — it would go back and fill in the correct address once it had reached x.) Or perhaps all such holes are left for the linker to close. Another problem with end-padding is that you need syntax to say “insert padding here so that there is aligned”. I can think of an algorithm that would work for simple cases like that, but it may be such an obscure feature as to not be worth implementing. More complicated cases with nested padding might have contradictory results…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:32:52+00:00Added an answer on May 13, 2026 at 8:32 am

    Unfortunately, most assemblers are one-pass simple translators, which limit the flexibility of alignment directives they can offer. Even among all the alignment options that assemblers working in several passes could offer, many are neglected because there are too specific. Yours is one of those, I am afraid. It could work in a one-pass assembler as long as it’s only one instruction you intend to move, but it’s very specific.

    I have seen the manual of a sophisticated multi-pass assembler that let you substract the addresses of two labels to get the length of a sequence of instruction, and would let you insert a directive to insert a sequence of NOPs, say, (4 – this length modulo 4) in the place of your choice (as long as it remained possible to converge on a definite position for each instruction). I can’t remember what assembler it was. Definitely not gas, which is one-pass as far as I know. It may have been the venerable A386.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My problem was that I had multiple mysql servers on… May 14, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer Add this before the last rule: RewriteBase / RewriteCond %{HTTP_HOST}… May 14, 2026 at 10:34 pm
  • Editorial Team
    Editorial Team added an answer If this is a WinForms application you could use the… May 14, 2026 at 10:34 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.