1
votes

Imagine that I have a function in C that has 5 parameter.

sum(n1,n2,n3,n4,n5);

In assembly. I get the first four parameter from registers 4 to 7 and the last parameter is acceded like:

 lw $8, 16($29)

First question

If lw $8, 16($29) puts n5 in register $8, why doesn't this

lw      $9, 0($29)
lw      $10, 4($29)
lw      $11, 8($29)
lw      $12, 12($29)

puts n1 to n4 in registers $9 to $12?

Second question

Since the parameters in sum(n1,n2,n3,n4,n5); are somewhere stored in memory and in assembly, the first parameter is in $4 how can I get the memory address of $4 to $7?

If i do this:

    .data
array:  .word 3,4,2,6,12,7,18,26,2,14,19,7,8,12,13
    .text
main:
    li  $8,1
    la  $9,array

the last instruction puts the address location of my array in $9. If I do

main:
    li  $4,1
    la  $9,0($4)

The value on $9is still 1 and not the address of $4

1
See stackoverflow.com/q/10214334/583570 for more on the MIPS calling convention.markgz

1 Answers

3
votes

First question

To answer this, you need to understand how parameter-passing is generally handled. There are conventions for parameter passing. The first four parameters are always passed through registers $4 through $7, and the remaining parameters are passed through the stack. There's no point in passing the first four parameters on the stack if they're already passed by registers, right? So it only pushes to the stack what's left after the first four.

Second question

There is a distinction between registers and memory. CPU registers aren't mapped to any memory address. They are special "pieces" of memory that are separate from the rest of your RAM, ROM, etc. They are much faster than the memory in your RAM or ROM, and so there are MIPS instructions that operate directly on them, rather than indirectly through memory addresses.

Think about exactly what you're doing:

main:
    li  $4,1
    la  $9,0($4)

What's the first instruction doing? It's loading a value of 1 into register 4.

What's the second instruction doing? It's treating the value inside of register 4 as a label (or an address), adding 0 to it, and storing the result in register 9. So of course register 9 will end up having a value of 1.

In MIPS, and in nearly all architectures, there is no concept of addressing registers. You operate directly on them.

EDIT: I should make a distinction between processor registers and hardware registers. Of course, I'm referring to the processor registers. Hardware registers (outside of the CPU) are a slightly different concept, and they explicitly are addressable. However, I think that's beyond the scope of what Favolas is asking.