15

私は MIPS アセンブリ言語にまったく慣れておらず、現在、MIPS コーディングに関する大きなセクションがあるコンピューター アーキテクチャのクラスを受講しています。過去に他のいくつかの高水準プログラミング言語 (C、C#、Python) を勉強したので、プログラミングの基礎をいくつか持っています。

ここでの私の質問は具体的に尋ねます: MIPS はスタック内の配列にメモリをどのように割り当てますか? この質問に答えることで、MIPS 言語とそのアーキテクチャの概念をまだ少し理解していないので、MIPS の全体的な理解が深まることを願っています。この全体に関して、ポインターがどのように機能するかはよくわかりません...

誰かがこの混乱した学生を助けるために時間を割いてくれたら最高です! :)

4

3 に答える 3

25

C と同様に、MIPS には基本的に 3 つの異なるメモリ割り当て方法があることに注意してください。

次の C コードを検討してください。

int arr[2]; //global variable, allocated in the data segment

int main() {
    int arr2[2]; //local variable, allocated on the stack
    int *arr3 = malloc(sizeof(int) * 2); //local variable, allocated on the heap
}

MIPS アセンブリは、これらすべてのタイプのデータをサポートします。

データ セグメントに int 配列を割り当てるには、次を使用できます。

.data

arr: .word 0, 0 #enough space for two words, initialized to 0, arr label points to the first element 

スタックに int 配列を割り当てるには、次を使用できます。

#save $ra
addi $sp $sp -4  #give 4 bytes to the stack to store the frame pointer
sw   $fp 0($sp)  #store the old frame pointer
move $fp $sp     #exchange the frame and stack pointers
addi $sp $sp -12 #allocate 12 more bytes of storage, 4 for $ra and 8 for our array
sw   $ra  -4($fp)

# at this point we have allocated space for our array at the address -8($fp)

ヒープに領域を割り当てるには、システム コールが必要です。スピム シミュレータでは、これはシステム コール 9です。

li $a0 8 #enough space for two integers
li $v0 9 #syscall 9 (sbrk)
syscall
# address of the allocated space is now in $v0
于 2013-10-26T22:37:59.267 に答える