Author Topic: EAX Register  (Read 8484 times)

0 Members and 1 Guest are viewing this topic.

Offline BitsnBytesTopic starter

  • Contributor
  • Posts: 42
  • Country: pk
EAX Register
« on: February 09, 2021, 04:20:43 pm »
Hello,

EAX Register is used to store the return value. Is there any such register in microcontrollers which holds the value returned by main().
printf("Respect");
 

Online grumpydoc

  • Super Contributor
  • ***
  • Posts: 3038
  • Country: gb
Re: EAX Register
« Reply #1 on: February 09, 2021, 04:27:26 pm »
Hello,

EAX Register is used to store the return value.
That's a big assumption.

Quote
Is there any such register in microcontrollers which holds the value returned by main().
Depends on the calling convention of the compiler and architecture that you are using.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12516
  • Country: us
    • Personal site
Re: EAX Register
« Reply #2 on: February 09, 2021, 04:29:37 pm »
On ARM according to the EABI calling conventions, return values less than 32 bits in size are stored in R0.

And it is different for different calling conventions and architectures. 
Alex
 

Offline BitsnBytesTopic starter

  • Contributor
  • Posts: 42
  • Country: pk
Re: EAX Register
« Reply #3 on: February 09, 2021, 04:50:04 pm »
Quote
On ARM according to the EABI calling conventions, return values less than 32 bits in size are stored in R0.


So can we use the return value to do something?

I am talking about microcontrollers

printf("Respect");
 

Online grumpydoc

  • Super Contributor
  • ***
  • Posts: 3038
  • Country: gb
Re: EAX Register
« Reply #4 on: February 09, 2021, 05:02:28 pm »
I am talking about microcontrollers

The problem is that you haven't constrained your question enough to be able to offer a useful answer.

"Microcontrollers" covers everything from 8 bit PIC upwards, and at the top end gets blurred between Microcontroller, SOC and desktop architectures.

Yes, the return value from main() (which is an int) will probably go in a register, but it does not *have* to, there doesn't even have to be a main() if you are coding in assembler on a platform - or a language other than C/C++

What specific architecture & compiler are you using?
 

Offline hli

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: de
Re: EAX Register
« Reply #5 on: February 09, 2021, 05:02:34 pm »
Is there any such register in microcontrollers which holds the value returned by main().

main() does not return. Or rather, when it does, your controller either crashes or reboots (most likely the former). To be more exact, main is just address where the program gets started (after the startup code did run).
 

Online grumpydoc

  • Super Contributor
  • ***
  • Posts: 3038
  • Country: gb
Re: EAX Register
« Reply #6 on: February 09, 2021, 05:04:09 pm »
Is there any such register in microcontrollers which holds the value returned by main().

main() does not return. Or rather, when it does, your controller either crashes or reboots (most likely the former). To be more exact, main is just address where the program gets started (after the startup code did run).

Yes, but main() is defined in the C standard to return an int, so it *is* meaningful to talk about that return value, even if main() returning is not always a terribly good thing to do.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12516
  • Country: us
    • Personal site
Re: EAX Register
« Reply #7 on: February 09, 2021, 05:11:02 pm »
In the embedded systems main() typically return nowhere. Or in the best case to while (1); loop.

What do you want to do with that value.

And also, yes, you have not specified your MCU.
Alex
 

Offline BitsnBytesTopic starter

  • Contributor
  • Posts: 42
  • Country: pk
Re: EAX Register
« Reply #8 on: February 09, 2021, 05:14:15 pm »
I am working on ATMEGA328P.
printf("Respect");
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: EAX Register
« Reply #9 on: February 09, 2021, 05:18:05 pm »
And compiler?  GCC?

https://gcc.gnu.org/wiki/avr-gcc

int is 16 bits so the result will be in r25:r26.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 12516
  • Country: us
    • Personal site
Re: EAX Register
« Reply #10 on: February 09, 2021, 05:24:26 pm »
GCC startup code calls main() and then jumps to "exit". "exit" can't return. But you can declare your own version and look at those registers and do whatever you want.
Alex
 

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 512
  • Country: gb
    • Woofys Place
Re: EAX Register
« Reply #11 on: February 09, 2021, 05:25:15 pm »
The return value from main() is intended to return execution status to OS's such as *nix.
It is meaningless in microcontrollers like the AVR, which should not return.
Put your code in an endless loop:
 
Code: [Select]
while(1) {}

Offline BitsnBytesTopic starter

  • Contributor
  • Posts: 42
  • Country: pk
Re: EAX Register
« Reply #12 on: February 09, 2021, 06:23:48 pm »
Yeah actually I knew that return is meaningless in microcontroller, but I thought may be I don't know and there is a way to use a return value in microcontrollers.

Thank you all for replies. I always learn from you guys!
printf("Respect");
 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22435
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: EAX Register
« Reply #13 on: February 09, 2021, 06:49:22 pm »
I mean, there's nothing wrong with recursively calling main(args) -- but that particular approach is typically an anti-pattern reserved for the best winnersworst offenders of the IOCCC, and on PC at that, rarely if ever something you'd have enough RAM to do on an MCU.

So, the most common pattern on MCUs is for init to call main, and main to never return; and the compiler may still emit exit (typically an infinite loop), or the return and exit code can be disabled with an option.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Online Siwastaja

  • Super Contributor
  • ***
  • Posts: 11486
  • Country: fi
Re: EAX Register
« Reply #14 on: February 09, 2021, 06:51:40 pm »
They are just general-purpose registers, nothing special on CPU.

For inter-operation of pre-compiled parts (like libraries), it's a very good idea to follow a common convention, though.

Often there is just one widely used convention for each architecture. Sometimes the gcc binary name gives hints which conventions it follows. For example, gcc-arm-none-eabi gives a few hints for you - like that binaries compiled by it follow the EABI conventions. (EABI being just a name for a set of standardized conventions; no one forces you to follow them but it's often a pretty good idea).

My embedded projects define void main(), not int main(), for obvious reasons. Not a single person or tool has complained to me about that (yet). It may be a good idea to add noreturn attribute to main() - I remember avr-gcc generating a lot of excess push/pop code around main() eating up considerable amount of program memory in some of their smallest devices. Adding noreturn attribute lets the compiler do code size and possibly performance optimizations. while(1) loop wasn't enough for avr-gcc to deduce this behavior at least back when I needed to add noreturn.
 

Offline hli

  • Frequent Contributor
  • **
  • Posts: 277
  • Country: de
Re: EAX Register
« Reply #15 on: February 10, 2021, 01:59:56 pm »
Yes, but main() is defined in the C standard to return an int

C0x: http://c0x.coding-guidelines.com/5.1.2.2.1.html
162 The function called at program startup is named main.
163 The implementation declares no prototype for this function.
164 It shall be defined with a return type of int and with no parameters:
        int main(void) { /* ... */ }
165 or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
        int main(int argc, char *argv[]) { /* ... */ }
166 or equivalent;
167 or in some other implementation-defined manner.

So the entry point must be named main, and accepting and returning parameters is optional. void main(void) should be legal then.
If you want to go deeper into that rabbit hole - main does not need to be a function: https://jroweboy.github.io/c/asm/2015/01/26/when-is-main-not-a-function.html
 
The following users thanked this post: harerod

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1920
  • Country: se
Re: EAX Register
« Reply #16 on: February 10, 2021, 02:53:44 pm »
So the entry point must be named main, and accepting and returning parameters is optional. void main(void) should be legal then.
The only authority here is the standard.
To put it very briefly:
  • In a hosted environment the only standard-defined return type for main() is int, and the name is main (duh).
    There are exactly two signatures allowed, int main(void) and int main(int, char**) or their equivalent.
    It allowable, though, for an implementation to define additional signatures, this, as all implementation-defined behaviour, must be documented.
    main is also special in that, while returning an int, does not need an explicit return statement, falling off the closing } returns 0.
  • In a freestanding environment, the name and signature of the function invoked at startup is completely implementation-defined.
    In this case (where I would put most MCU environments) a void main() - or any other function name and signature, as described by the documentation - is acceptable

The relevant sections of the C11 standard (no changes AFAICR from C89, C99 and in C18) are:
5.1.2.1 Freestanding environment
5.1.2.2 Hosted environment
5.1.2.2.3 Program termination
J.1 Unspecified behavior (2nd paragraph)
J.2 Undefined behavior  (4th paragraph)
J.3.2 Environment (4th and 5th paragraphs)

All in all, void main() is then only acceptable as an alternative, implementation-defined and documented signature.
That said, after being traumatized in my infancy by the exposure to Schildt's books, seeing void main() still makes my skin crawl  ;)
Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: grumpydoc

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17866
  • Country: fr
Re: EAX Register
« Reply #17 on: February 10, 2021, 03:14:12 pm »
Yes, I'd even suggest NOT using the 'main' identifier on freestanding environments if you're not going to follow the standard (eg., not returning a value from main.)
That said, "int main(void)" is not harder to write than "void main(void)". To avoid warnings, you then have to return a value. But how hard is it to add "return 0;" at the end of the function. There, you comply with the standard, you get no warning, and everyone is happy.

On MCUs, there commonly is some startup code (usually written in assembly) doing some very low-level init. stuff and then calling main(). The startup code also usually ignores the return value, and then goes into an infinite loop. Now some particular environments may do it differently; sometimes, on very simple targets, the main() function may directly be the entry point, in which case, if the main() function ever returns, the target will usually "crash" (so in this case, never returning, putting an infinite loop at the end of main(), is a good idea.) On some other particular environments, the return value of main() may actually be used.

 
The following users thanked this post: newbrain

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1920
  • Country: se
Re: EAX Register
« Reply #18 on: February 10, 2021, 03:38:26 pm »
To avoid warnings, you then have to return a value.
Not since a long time, with reasonable compilers:
Code: [Select]
$cat noret.c

int main(void)
{
}

int f(void)
{
}

$gcc -std=c11 -Wall -Wextra -pedantic -c noret.c
noret.c: In function ‘f’:
noret.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
$clang -std=c11 -Weverything -pedantic -c noret.c
noret.c:6:5: warning: no previous prototype for function 'f' [-Wmissing-prototypes]
int f(void)
    ^
noret.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
2 warnings generated.
$

EtA: Also Microsoft C++ compiler agrees:
Code: [Select]
C:\Users\git>cl /Wall noret.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

noret.c
c:\users\git\noret.c(7) : warning C4716: 'f': must return a value
Microsoft (R) Incremental Linker Version 14.16.27027.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:noret.exe
noret.obj
« Last Edit: February 10, 2021, 03:44:22 pm by newbrain »
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline BitsnBytesTopic starter

  • Contributor
  • Posts: 42
  • Country: pk
Re: EAX Register
« Reply #19 on: February 10, 2021, 03:49:15 pm »
Quote
And compiler?  GCC?

AVR-GCC
printf("Respect");
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 4879
  • Country: us
Re: EAX Register
« Reply #20 on: February 10, 2021, 04:10:40 pm »
Microcontrollers generally use freestanding rather than hosted C so they are allowed to do whatever they want.

However, void main(void) has never been standard C.  The two standard prototypes in the section quotes above both return int.  Generally this will not be a big deal.  You might get a garbage return value but gcc will actually often zero the return value for you.  A platform can have other platform specific entrypoints but depending on how you parse that quote perhaps they should all return int. 

However in standard C main is not required to be a normal function at all.  Calling it recursively for instance is undefined.  This is because the main function might get renamed to match a platform specific entrypoint like _start.  It might also have a non standard prologue and epilog or use a different calling convention on some platforms. All modern C runtimes that I know of have a function like _libc_init that calls main as a normal function but that is not required.
 
The following users thanked this post: newbrain

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17866
  • Country: fr
Re: EAX Register
« Reply #21 on: February 10, 2021, 08:47:55 pm »
To avoid warnings, you then have to return a value.
Not since a long time, with reasonable compilers:

This is actually not a matter of being "reasonable", but merely being (at least) C99-compliant.
That's mentioned in the C99 standard: "reaching the } that terminates the main function returns a value of 0."

If you compile the code with '-std=c89', you'll get a warning for main(). So, it all depends on which standard you're complying with. And I don't know if avr-gcc, for instance, is C99-compliant (maybe it is, I just have never used it.)

I find that exception a bit peculiar anyway. Defining a function that returns a value but never returns anything explicitely? It's odd. The decision of the committee here is questionable IMO. Why disallow 'void main(void)' and allow it not to return any value, this is bizarre. But this is just so.


 
The following users thanked this post: newbrain

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 4879
  • Country: us
Re: EAX Register
« Reply #22 on: February 11, 2021, 02:21:37 am »
The return type of main is int because the exit status is not optional.  It defaults to zero because that means no error.

Sure the standard could have said that void main will act as if it returned zero but it makes more sense to me to have the return type int and defaults to zero if it reaches the end.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4676
  • Country: us
Re: EAX Register
« Reply #23 on: February 11, 2021, 02:30:12 am »
Quote
And compiler?  GCC?
https://gcc.gnu.org/wiki/avr-gcc
int is 16 bits so the result will be in r25:r26.
correct link; wrong answer...

Quote
Calling Convention
  • An argument is passed either completely in registers or completely in memory.
  • To find the register where a function argument is passed, initialize the register number Rn with R26 and follow this procedure:
    • If the argument size is an odd number of bytes, round up the size to the next even number.
    • Subtract the rounded size from the register number Rn.
    • If the new Rn is at least R8 and the size of the object is non-zero, then the low-byte of the argument is passed in Rn.  Subsequent bytes of the argument are passed in the subsequent registers, i.e. in increasing register numbers.
    • If the new register number Rn is smaller than R8 or the size of the argument is zero, the argument will be passed in memory.
    • If the current argument is passed in memory, stop the procedure: All subsequent arguments will also be passed in memory.
    • If there are arguments left, goto 1. and proceed with the next argument.
  • Return values with a size of 1 byte up to and including a size of 8 bytes will be returned in registers.  Return values whose size is outside that range will be returned in memory.
  • If a return value cannot be returned in registers, the caller will allocate stack space and pass the address as implicit first pointer argument to the callee.  The callee will put the return value into the space provided by the caller.
  • If the return value of a function is returned in registers, the same registers are used as if the value was the first parameter of a non-varargs function. For example, an 8-bit value is returned in R24 and an 32-bit value is returned R22...R25.
  • Arguments of varargs functions are passed on the stack.  This applies even to the named arguments.
For example, suppose a function with the following prototype:
  • int func (char a, long b);
then
  • a will be passed in R24.
  • b will be passed in R20, R21, R22 and R23 with the LSB in R20 and the MSB in R23.
  • The result is returned in R24 (LSB) and R25 (MSB).
Note that there are several compilers for AVR that have significantly different calling conventions (that use separate stacks for data and control paths, for example.)

Using "register 0" is very popular in general, but on AVR A0-A15 are sort-of "2nd class registers."  I'm not sure how they came up with R24.
 

Offline JohnnyMalaria

  • Super Contributor
  • ***
  • Posts: 1154
  • Country: us
    • Enlighten Scientific LLC
Re: EAX Register
« Reply #24 on: February 11, 2021, 02:42:53 am »
According to my copy of The C Programming Language (Kernighan and Ritchie, 2nd ed), it's neither int main() or void main() but just main(). It also says:

Quote
You may have noticed that there is a return statement and the end of main. Since main is a function like any other, it may return a value to its caller, which is in effect the environment in which the program was executed. Typically, a return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions. In the interests of simplicity, we have omitted return statements from our main functions up to this point, but we will include them hereafter, as a reminder that programs should return status to their environment.

Later,

Quote
Within main, return expr is equivalent to exit(expr). exit has the advantage that it can be called from other functions...
 
The following users thanked this post: xrunner


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->