Reputation: 197
Consider the following code, which reads the first command line argument and then prints it:
lw $a0, 0($a1)
li $v0, 4
syscall
This makes sense, because arguments are always stored in word-aligned offsets from $a1. However, supposes the first program argument is 'jklfjdsaklfjdsklfjsdklfjsklfjsklfjsdaklfjlsdkjfjfksalfjsadlkf'. There's no way this gigantic string fits into the four bytes between 0($a1) and 4($a1). Yet the code still runs and prints the string. Why?
Also how does the entire string get put into $a0? Because again it shouldn't really fit.
Upvotes: 0
Views: 886
Reputation: 71546
char fun0 ( char *x )
{
return(x[11]);
}
char fun1 ( char **x )
{
return(x[1][1]);
}
mips
00000000 <fun0>:
0: 8082000b lb $2,11($4)
4: 03e00008 jr $31
8: 00000000 nop
0000000c <fun1>:
c: 8c820004 lw $2,4($4)
10: 00000000 nop
14: 80420001 lb $2,1($2)
18: 03e00008 jr $31
1c: 00000000 nop
arm
00000000 <fun0>:
0: e5d0000b ldrb r0, [r0, #11]
4: e12fff1e bx lr
00000008 <fun1>:
8: e5903004 ldr r3, [r0, #4]
c: e5d30001 ldrb r0, [r3, #1]
10: e12fff1e bx lr
x86
0000000000000000 <fun0>:
0: 0f b6 47 0b movzbl 0xb(%rdi),%eax
4: c3 retq
5: 90 nop
6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1)
d: 00 00 00
0000000000000010 <fun1>:
10: 48 8b 47 08 mov 0x8(%rdi),%rax
14: 0f b6 40 01 movzbl 0x1(%rax),%eax
18: c3 retq
so when you have int main ( int argc, char *argv[]) argv is an address and the compiler passes that address. If you are working at a different level, then the operating system/environment defines that the command line string is passed that way and that would cover the "why" question. You didnt provide enough information to answer the question.
And it basically has nothing to do with mips, it has to do with an implementation of some system that is targeted to mips. The same types of implementations can be used on non-mips targets as well.
Upvotes: 1