This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Confused at usage of stack for MIPS arch
- From: Andrew Haley <aph at redhat dot com>
- To: "PRC" <panruochen at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Thu, 22 Nov 2007 16:13:26 +0000
- Subject: Re: Confused at usage of stack for MIPS arch
- References: <200711222343165159488@gmail.com>
PRC writes:
> I'm a little confused at how stack is used for mips arch by gcc.
>
>
> Here is the C source code:
>
> char *
> strcpy(char *dest, char *src)
> {
> char *tmp = dest;
> while( (*dest++ = *src++) != '\0' ) ;
>
> return tmp;
> }
>
> And the assembly code:
>
> -------------------------------------------------------
> 1.o: file format elf32-littlemips
>
> Disassembly of section .text:
>
> 00000000 <strcpy>:
>
> char *
> strcpy(char *dest, char *src)
> {
> 0: 27bdfff0 addiu sp,sp,-16
> 4: afbe0008 sw s8,8(sp)
> 8: 03a0f021 move s8,sp
> c: afc40010 sw a0,16(s8)
> 10: afc50014 sw a1,20(s8)
> char *tmp = dest;
> 14: 8fc20010 lw v0,16(s8)
> 18: afc20000 sw v0,0(s8)
> while( (*dest++ = *src++) != '\0' ) ;
> 1c: 8fc30010 lw v1,16(s8)
> 20: 27c40014 addiu a0,s8,20
> 24: 8c820000 lw v0,0(a0)
> 28: 90450000 lbu a1,0(v0)
> 2c: 24420001 addiu v0,v0,1
> 30: ac820000 sw v0,0(a0)
> 34: 00601021 move v0,v1
> 38: a0450000 sb a1,0(v0)
> 3c: 24630001 addiu v1,v1,1
> 40: afc30010 sw v1,16(s8)
> 44: 00051600 sll v0,a1,0x18
> 48: 00021603 sra v0,v0,0x18
> 4c: 10400003 beqz v0,5c <strcpy+0x5c>
> 50: 00000000 nop
> 54: 08000007 j 1c <strcpy+0x1c>
> 58: 00000000 nop
>
> return tmp;
> 5c: 8fc20000 lw v0,0(s8)
> } 60: 03c0e821 move sp,s8
> 64: 8fbe0008 lw s8,8(sp)
> 68: 27bd0010 addiu sp,sp,16
> 6c: 03e00008 jr ra
> 70: 00000000 nop
> -------------------------------------------------------
>
>
> The usage of stack inside the function should look like:
>
> sp' +-----+
> | tmp | sp+0
> |_____|
> | | sp+4
> | |
> |_____|
> | | sp+8
> | |
> |_____|
> | | sp+12
> | |
> |_____|
> sp | | sp+16
> | a0 |
> |_____|
> | | sp+20
> | a1 |
> |_____|
>
> As a gerneral rule, the units above the stack top should be protected
> during the call of the function. But why the stack top is used to
> save the a0 and a1 registers here?
They're not saved, they're moved to local stack slots.
> By the way, why sp+4, sp+8 and sp+12 are left unused?
Alognment.
Have a good look at
http://www.sco.com/developers/devspecs/mipsabi.pdf
Pages 3-11 onwards. If there's anything you still don't understand,
let us know.
Andrew.
--
Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, UK
Registered in England and Wales No. 3798903