mips programming
DESCRIPTION
MIPS Programming. Arrays Writing Procedures: Calling Convention. Memory Setup in C/Java. C++: int *intarray = new int[10]; Java: int[] intarray = new int[10]; What does this do? What does the memory look like? Where is intarray[5] located? intarray + 20 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/1.jpg)
MIPS Programming
Arrays
Writing Procedures:
Calling Convention
![Page 2: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/2.jpg)
Memory Setup in C/Java• C++: int *intarray = new int[10];• Java: int[] intarray = new int[10];• What does this do? What does the memory look
like?• Where is intarray[5] located? intarray + 20• Where is intarray[i] located? intarray + 4*i
intarray intarray + 20&(intarray[0]) + 20
intarray&(intarray[0])
![Page 3: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/3.jpg)
Declaring & InitializingGlobal Arrays in HLL
int GlobalA = 3;int GlobalB[] = {0x20040002, 0x20080001, 0x200b0001, 0x8b502a,
0x15400003, 0x01084020, 0x20840000, 0x800fffa, 0x10010200, 0x00000000};
int main(int argc, char *argv[]){
}
public class MyClass{ public static int GlobalA = 3; public static int GlobalB[] = {0x20040002, 0x20080001, 0x200b0001, 0x8b502a,
0x15400003, 0x01084020, 0x20840000, 0x800fffa, 0x10010200, 0x00000000};
};
![Page 4: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/4.jpg)
Declaring & Initializing Global Arrays in MIPS
.dataGlobalA: .word 0x03;GlobalB:.word 0x20040002 0x20080001 0x200b0001 0x8b502a .word 0x15400003 0x01084020 0x20840000 0x800fffa .word 0x10010200 0x00000000
.text
main:
![Page 5: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/5.jpg)
Declaring & InitializingLocal Arrays
int main(int argc, char *argv[]){ int LocalA = 5; int LocalB[] = {1,2,3};
}
add $sp, $sp, -(24 + x + 4 + 12) # where x is space for preserved regsaddi $t0, $0, 5sw $t0, 12($sp)addi $t0, $0, 1sw $t0, 0($sp)addi $t0, $0, 2sw $t1, 4($sp)addi $t0, $0, 3sw $t1, 8($sp) // and so forth
Not possible in Java!!!!!In Java, arrays are references to Array objects.
![Page 6: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/6.jpg)
Declaring & InitializingHeap Arrays in HLL
int main(int argc, char *argv[]){ int *LocalA = (int *)malloc(4); *LocalA = 5; int *LocalB = (int *)malloc(12); LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3;}
public class MyClass{ public static void main(int argc, String argv) { int LocalA[] = new int[1]; LocalA[0] = 5; int LocalB[] = new int[3]; LocalB[0] = 1; LocalB[1] = 2; LocalB[2] = 3; }};
![Page 7: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/7.jpg)
Declaring & InitializingHeap Arrays in MIPS
add $sp, $sp, -(24 + x + 8) # where x is space for preserved regsaddi $a0, $0, 4jal mallocsw $v0, 4($sp) // store the reference into the stackaddi $t0, $0, 5sw $t0, 0($v0) // initialize first elements as 5 (*LocalA = 5)addi $a0, $0, 12jal mallocsw $v0, 0($sp) // store the reference into the stackaddi $t0, $0, 1sw $t0, 0($v0) // LocalB[0] = 1addi $t0, $0, 2sw $t0, 4($v0) // LocalB[1] = 2addi $t0, $0, 3sw $t0, 8($v0) // LocalB[2] = 3
![Page 8: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/8.jpg)
MIPS Example 5Translate int A[100]; // ints are 4 bytes in C/Javachar B[100]; // chars are 1 byte in C
char c = B[50]; A[1] = A[5] + 7;
Assumptions: A & B are globalc is in the stack,6 bytes from $sp
Use LoadByte, not LoadWord, because char (in C) is 1 byte
la $s1, Blb $t1, 50($s1) # $t1 = B[50];
lw $t0, 5 ($s0) ; x = A[5];lw $t0, 20 ($s0)# $t0 = A[5];
addi $t0, $t0, 7 # $t0 = A[5] + 7;
sw $t0, 4 ($s0) # A[1] = A[5] + 7;
sb $t1, 6($sp) # c = B[50];
la $s0, A
![Page 9: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/9.jpg)
MIPS Example 6Translate int A[100];int i; …x = A[i];
sll $t1, $s1, 2 # $t1 = i<<2 or i * 4add $t1, $s0, $t1 # $t1 = (i*4+&A[0]) or &(A[i])lw $t0, 0($t1) # $t0 = A[i];
Assumptions: &(A[0]) is in $s0,x is in $t0i is in $s1
![Page 10: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/10.jpg)
Procedure Calls
• Procedure must work the same from any call
• Procedure uses regs that main was using
• We need a convention to– pass arguments– preserve registers, stack– pass return values
Main
Procedure
Call Procedure
Call Procedure
![Page 11: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/11.jpg)
Name Reg Number Usage Preserved across call?
$zero 0 The constant 0 Yes
$v0-$v1 2-3 Function results No
$a0-$a3 4-7 Function Arguments No
$t0-$t7 8-15 Temporaries No
$s0-$s7 16-23 Saved Yes
$t8-$t9 24-25 More temporaries No
$gp 28 Global pointer Yes
$sp 29 Stack pointer Yes
$fp 30 Frame pointer Yes
$ra 31 Return address Yes
Page 140, Figure 3.13
MIPS-specific info
![Page 12: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/12.jpg)
MIPS-specific info – who cares?
• Preserved – Value is same after call– Caller – excellent! no worries!
– Procedure – may not destroy value• must store at beginning and restore at end to
use
• Not preserved – No guarantees– Caller – loses value in register
• most store before call and restore after call
– Procedure – may use without worries
![Page 13: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/13.jpg)
Steps for caller
0. Store any temp regs whose values we need
• Pass function parameters to procedure
• Transfer control to procedure
• (then procedure executes)
• Get return value
• Restore any temp regs we saved away
![Page 14: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/14.jpg)
Steps for procedure
1. Allocate stack space
2. Store preserved regs we may use
3. Perform task
4. Place result in proper location for caller
5. Restore preserved regs we may have used
6. Transfer control back to caller
![Page 15: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/15.jpg)
Caller: 4. Restore regs
Assume: g,h are in $s2,$s3. We want return value in $s0.Caller wants to preserve $t0 across function call.
Callee:
int MyFunc(int g, int h){ return (g + h);}
Caller:sw $t0, 0($sp)add $a0, $s2, $zeroadd $a1, $s3, $zerojal MyFuncadd $s0, $v0, $zerolw $t0, 0($sp)
Load from same location in the stack
![Page 16: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/16.jpg)
Definitions
• Leaf function– Makes no function calls– You need not store $ra into the stack
• Non-leaf function– Contains a function call– You MUST store $ra in the stack and restore
before returning to caller
![Page 17: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/17.jpg)
Allocating stack space
$sp
$sp$sp Stack space for
this function
Only allocate once per function!!!!
$sp contains address of bottom of stack. What
operation on $sp allocates space?
Minimum allocation:
24 bytes
![Page 18: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/18.jpg)
Callee: 7. Return to caller
Assume: g,h are in $s2,$s3. We want return value in $s0.Caller wants to preserve $t0 across function call.
int MyFunc(int g, int h){ return (g + h);}
Caller:sw $t0, 0($sp)add $a0, $s2, $zeroadd $a1, $s3, $zerojal MyFuncadd $s0, $v0, $zerolw $t0, 0($sp)
Callee:addi $sp, $sp, -32sw $s0, 0 ($sp)add $s0, $a0, $a1add $v0, $s0, $zerolw $s0, 0 ($sp)addi $sp, $sp, 32jr $ra
![Page 19: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/19.jpg)
What actually goes in stack
$ra
Extra Arguments
Extra outgoing arguments
$spbefore call
$spduring call
padding
local dataL*4 bytes for local data
P*4 bytes for preserved regs ($s0-$s7)
A*4 bytes for outgoing args
$fp
preserved registers(and $a0-$a3)
$fpduring call
![Page 20: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/20.jpg)
Example
int foo(int arg1, int arg2){
int myarray[64];myarray[3] = 5;…bar(a1, a2, a3, a4, a5);…return (myarray[3]);
}
Local 256-byte array
Non-leaf function
Assume it needs 2 saved
registers
![Page 21: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/21.jpg)
outgoing arg 5
$s1$s0
padding
addi $sp, $sp, - (1+64+1+2+6)*4sw $ra, 73*4($sp)sw $fp, 72*4($sp)sw $s1, 67*4($sp)sw $s0, 66*4($sp)addi $t0, $zero,5 # $t0 = 5sw $t0, (1+3)*4 ($sp)# myarray[3] = 5…lw $ra, 73*4($sp)lw $fp, 72*4($sp)lw $s1, 67*4($sp)lw $s0, 66*4($sp)addi $sp, $sp, (1+64+1+2+6)*4jr $ra
myarray
$spbefore call
$spduring call
Caller’s Stack
$a3$a2
$ra$fp$a0$a1
Minimum Allocation
![Page 22: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/22.jpg)
Another Example:
int SumToN(int N){ int sum = 0; while (N > 0) { sum += N;
N--; } return sum; }
SumToN:
add $v0, $0, $0
loop: slt $t0, $0, $a0beq $t0, $0, endadd $v0, $v0, $a0
addi $a0, $a0, -1 j loop
end:
jr $ra
![Page 23: MIPS Programming](https://reader033.vdocuments.net/reader033/viewer/2022061612/56814fbb550346895dbd73a1/html5/thumbnails/23.jpg)
Third Example:
int SumNtotheM(int N, int M){ int sum = 0; while (N > 0) { sum += power(N,M);
N--; } return sum; }
SumToNtotheM:add $sp, $sp, -32sw $ra, 14($sp)add $t0, $0, $0
loop: slt $t0, $0, $a0beq $t0, $0, endsw $a0, 28($sp)sw $a1, 24($sp)sw $t0, 0($sp)jal powerlw $t0, 0($sp)lw $a0, 28($sp)lw $a1, 24($sp)add $t0, $v0, $t0addi $a0, $a0, -1j loop
end: add $v0, $t0, $0lw $ra, 14($sp)add $sp, $sp, 32jr $ra