----- Ursprüngliche Nachricht -----
Von: Peter Bigot
An: Conrado P. L. Gouvêa
Gesendet am: 08 Mrz 2012 13:35:56
Betreff: Re: [Mspgcc-users] Incorrect generated code

> Yes, unless it is marked as "hosted" main is handled specially; there
> has been recent discussion about why in a thread about creating
> archives that contain the main routine.  See also
> http://www.mail-archive.com/mspgcc-users@lists.sourceforge.net/msg09750.html.

> At a quick glance does appear that the second call is not appropriate,
> though the return value is right (r15 has the correct value when main
> is left).  If you'd like this investigated, please file a ticket on
> the tracker.  https://sourceforge.net/tracker/?group_id=42303&atid=432701

>> int main(void) {
>>  if (dummy() == 1) {
>>    foo();
>>    return 1;
>>  }
>>  return 0;
>> }

>> 0000403e <main>:
>>    403e:       b0 12 62 40     call    #0x4062
>>    4042:       1f 93           cmp     #1,     r15     ;r3 As==01
>>    4044:       01 24           jz      $+4             ;abs 0x4048
>>    4046:       0f 43           clr     r15
>>    4048:       b0 12 58 40     call    #0x4058
>>    404c:       1f 43           mov     #1,     r15     ;r3 As==01

Teh second call is appropriate. The code does what it shall do.
Except that the source code does something that is not expected for main:
It returns. Where to?
The 'problem' in the above code is that main is not expected to end.
So the return statement does not produce a RET instruction
(which otherwise would be between the "clr r15" and the second call)

main is a naked function, so even if you use a return statement, no
RET instruction is generated. And in this specific case, due to the
optimizing reordering, the 'return 0' falls through to the if block.

Well, I must admit I had expected some jump to a dummy
'end of main' label or such. But thinking over it, it is obvious, that
on a naked function you're responsible for exiting the function properly
A return statement sets the return value, but that's it.
i foyu don't add your own jmp/ret/whatever code after the return statement,
it just continues executing what follows.

Returning something form main (or returning from main in general) doesn't make
any sense at all for a microcontroller program that isn't hosted by an OS. :)

JMGross


------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to