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

  • Home
  • SEARCH
  • 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 8898843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:42:51+00:00 2026-06-15T00:42:51+00:00

I have started developing a small 16-bit OS under GCC/G++. I am using a

  • 0

I have started developing a small 16-bit OS under GCC/G++.
I am using a GCC cross-compiler, which I compiled under Cygwin, I am putting asm(".code16gcc\n") as the first line of each .CPP file, using Intel ASM syntax and the command lines for compiling and linking a .CPP file look like this:

G++: i586-elf-g++ -c $(CPP_FILE) -o $(OBJECT_OUTPUT) -nostdinc -ffreestanding -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fpermissive -masm=intel

LD: i586-elf-ld -T $(LD_SCRIPT) $(OBJECT_OUTPUT) -o $(BINARY_OUTPUT)

The problem I am currently facing is the way GCC translates function-calling code into assembly.

To be more specific, instead of using the PUSH instruction to pass the arguments, GCC "calculates" the offsets relative to ESP the arguments should be located at, and then uses the MOV instruction to write the stack manually.

This is not beneficial for me, since I rely on the PUSH instruction in my assembly code. To illustrate my problem clearer, take these 2 functions:

void f2(int x);

void f1(){
    int arg = 8;
    asm("mov eax, 5");  // note: super hacky unsafe use of GNU C inline asm
    asm("push eax");    // Writing registers without declaring a clobber is UB
    f2(arg);
    asm("pop eax");
}
void f2(int x){
}

In function f1, I am saving EAX using the PUSH instruction, and I would expect to have it restored to 5 after calling f2 and executing the "POP EAX" instruction. It turns out however that EAX becomes 8, not 5. That’s because the ASSEMBLY CODE GCC generates looks like this (I’ve included the source as well for clarity):

void f1()
C++: {
    push ebp
    mov ebp,esp
    sub esp,byte +0x14

    C++: int arg = 8;
        mov dword [ebp-0x4],0x8
    
    C++: asm("mov eax, 5");
        mov eax,0x5
    
    C++: asm("push eax");
        push eax
    
    C++: f2(arg);
        mov eax,[ebp-0x4]
        mov [dword esp],eax =======>>>>>> HERE'S THE PROBLEM, WHY NOT 'PUSH EAX' ?!!
        call f2
        
    C++: asm("pop eax");
        pop eax
    
C++: }
    o32 leave
    o32 ret
    
void f2(int x)
C++: {
    push ebp
    mov ebp,esp
C++: }
    pop ebp
    o32 ret

I have tried using some G++ compilation flags like -mpush-args or -mno-push-args and another one which I can’t remember and GCC still doesn’t want to use PUSH. The version I’m using is i586-elf-g++ (GCC) 4.7.2 (Cross-Compiler recompiled in Cygwin).

Thank you in advance!

UPDATE: Here’s a webpage I’ve found: http://fixunix.com/linux/6799-gcc-function-call-pass-arguments-via-push.html

That just seems really stupid for GCC to do, considering that it limits the usability of inline assembly for complex stuff. 🙁 Please leave an answer if you have a suggestion.

  • 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-06-15T00:42:53+00:00Added an answer on June 15, 2026 at 12:42 am

    I’ve been very lucky finding a solution to this problem, but it finally does what I want it to do.
    Here’s what the GCC manual for version 4.7.2 state:

    -mpush-args
    -mno-push-args
    Use PUSH operations to store outgoing parameters. This method is shorter
    and usually equally fast as method using SUB/MOV operations and is enabled
    by default. In some cases disabling it may improve performance because of
    improved scheduling and reduced dependencies.
    
    -maccumulate-outgoing-args
    If enabled, the maximum amount of space required for outgoing arguments will
    be computed in the function prologue. This is faster on most modern CPUs
    because of reduced dependencies, improved scheduling and reduced stack usage
    when preferred stack boundary is not equal to 2. The drawback is a notable
    increase in code size. This switch implies ‘-mno-push-args’.
    

    I’m saying I am lucky because -mpush-args does not work, what works is instead “-mno-accumulate-outgoing-args”, which is not even documented!

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

Sidebar

Related Questions

Yesterday I have started developing my first Metro style App using JavaScript. I've used
The last months I have tried to get started with web developing using Java.
I have just started having a go at developing a small KDE app as
I have started developing facebook fanpages using HTML and CSS . The size of
I have started developing a full-web application by using the ASP .NET MVC 3
I have recently started on a PoC project wherein we are developing a small
Its been quite some time since i have started developing web pages using JSF
I have started developing a new Rails app on my server using RVM, Rails
I have just started developing for Symbian. I am currently using Nokia Qt. I
I started developing my rails application using sqlite. I have reached a point where

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.