This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: indexing arrays
- From: Andrew Haley <aph at redhat dot com>
- To: Mischa Baars <mjbaars1977 at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 18 Nov 2013 11:35:53 +0000
- Subject: Re: indexing arrays
- Authentication-results: sourceware.org; auth=none
- References: <528866A4 dot 5030305 at gmail dot com> <8761rqn0q3 dot fsf at mid dot deneb dot enyo dot de> <52898DCA dot 1030509 at gmail dot com> <5289E4A0 dot 9030807 at redhat dot com> <5289F4D8 dot 6000401 at gmail dot com>
On 11/18/2013 11:07 AM, Mischa Baars wrote:
> On 11/18/2013 10:57 AM, Andrew Haley wrote:
>> On 11/18/2013 03:47 AM, Mischa Baars wrote:
>>> On 11/17/2013 11:05 PM, Florian Weimer wrote:
>>>> * Mischa Baars:
>>>>
>>>>> Please have a look at the following example, and let me know what
>>>>> you think.
>>>> The code doesn't contain any comments, so we don't know what you're
>>>> trying to do.
>>> The assembly file does, if you would just look at it.
>>>
>>> As you can see, the 'ebx' register is unprotected when the function is
>>> called from a location other than 'main'. Normally I shouldn't be able
>>> to modify the array index from the assembly.
>> You're doing it wrong. It is the job of the called function to save and
>> restore %ebx:
>>
>> %ebx As described below, this register serves as the global offset
>> table base register for position-independent code. For
>> absolute code, %ebx serves as a local register and has no
>> specified role in the function calling sequence. In either
>> case, a function must preserve the register value for the
>> caller.
>>
>> http://www.sco.com/developers/devspecs/abi386-4.pdf
>
> I see what you are pointing at (p.37), but it also says "DRAFT COPY" :)
That isn't going to change any time soon.
> As you see from the program output, the 'ebx' register IS preserved when
> the 'npx_on_double()' function is called from 'main()', but the 'ebx'
> registers IS NOT preserved when the 'npx_on_double()' function is called
> from another function like 'npx_on_complex()'.
>
> This makes writing low-level functions very sensitive to errors.
No-one can write reliable code by trying something and seeing if it
works. You have know understand what you're doing. When writing
assembly code it's your job to know the ABI, and to follow the rules.
Do that, and you'll be fine.
Andrew.