ì Computer Systems and Networks ECPE 170 – Jeff Shafer – University of the Pacific MIPS Assembly (Functions)
2 Lab Schedule Activities Assignments Due This Week Lab 10 ì ì Due by Apr 11 th 5:00am Lab work time ì ì MIPS functions ì Lab 11 ì Due by Apr 18 th 5:00am ì Lab 12 ì Due by Apr 30 th 5:00am ì Computer Systems and Networks Spring 2019
3 ì Program Counter Computer Systems and Networks Spring 2019
4 Program Counter ì Instructions are stored in memory sequentially ì Each MIPS32 instruction occupies 4 bytes ì How does the processor know from where to fetch the next instruction? A special 32-bit register called Program Counter ( PC ) ì holds the address of the next instruction Computer Systems and Networks Spring 2019
5 Program Counter in Action What is the C Address Instruction code for this MIPS Assembly? 4 addi $t0, $zero, 0 8 addi $t1, $zero, 2 t0 = 0; t1 = 2; 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 while(t0<t1) { 20 j <label to addr. 12> t1++; } 24 li $v0, 10 //exit 28 syscall Computer Systems and Networks Spring 2019
6 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 PC addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 4 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
7 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 PC 8 addi $t1,$zero, 2 PC 8 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
8 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 PC 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
9 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 16 12 bge $t0, $t1, <label to addr 24> PC 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
10 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 20 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 PC 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
11 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 PC 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
12 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 16 12 bge $t0, $t1, <label to addr 24> 16 PC addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
13 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 20 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 PC 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
14 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 12 PC 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
15 Program Counter in Action Instructions are stored in memory and each occupy 4 bytes Address Instruction 4 addi $t0,$zero,0 8 addi $t1,$zero, 2 PC 24 12 bge $t0, $t1, <label to addr 24> 16 addi $t0, $t0, 1 20 j <label to addr. 12> PC 24 li $v0, 10 28 syscall Computer Systems and Networks Spring 2019
16 ì MIPS Functions Computer Systems and Networks Spring 2019
17 Function Requirements? ì What happens when we call a function? Place function arguments in standard location where 1. function can find them Save current program location to return to later 2. (the “Program Counter” register) Jump to the function location 3. Function runs using provided arguments 4. Function produces output (return value) and saves it 5. in standard location Jump to original program location (return) 6. 1. Technically, +1 instruction Computer Systems and Networks Spring 2019
18 Function Requirements ì Can a function change local variables of its calling function? ì No! The function operates in its own “bubble” ì What happens if the function changes $s0 which was also used by the calling function? ì Problem! Your function has corrupted the calling function Computer Systems and Networks Spring 2019
19 Functions in Assembly In assembly, you must do all the background work for functions that the compiler did automatically in a higher level language Functions still allow for code re-use (good!), but they’re more complicated than in C or C++ Computer Systems and Networks Spring 2019
20 Registers Name Use Constant value: ZERO $zero Local variables $s0-$s7 (Convention: These are saved if a function needs to re-use them) Temporary results $t0-$t9 (Convention: These are not saved if a function needs to re-use them) Arguments to pass to function (max of 4) $a0-$a3 New! Return value to obtain from function (max of 2) $v0-$v1 Return address of function $ra Stack pointer (current top of stack) $sp Computer Systems and Networks Spring 2019
21 More Jumps ì Jump and Link (side effect: $ra stores address of next instruction) jal <destination> Use this to call a function! ì Jump Register (destination address is stored in <reg1> jr <reg1> Use this to return from a function! Computer Systems and Networks Spring 2019
22 Basic Functions in MIPS 1. Program saves the context (registers) of calling function (caller) 2. Program saves the arguments in registers ( $a0 - $a3 ) 3. Program calls the callee via jump-and-link instruction jal <function label> jal saves the address of the next instruction in return address reg., $ra Program Counter (PC) points to the callee’s location. Callee saves return values in regs $v0-$v1 4. Callee returns via jump register instruction jr <register name> #usually $ra jr sets PC to $ra . PC continues there onwards Computer Systems and Networks Spring 2019
23 Problem 1 : Write Code #include <stdio.h> Place arguments ì in $a0-$a3 int function(int a); int main() Place return values ì { in $v0-$v1 int x=5; int y; Return address saved ì y = function(x); automatically in $ra printf("y=%i\n", y); Ignore the stack for this ì return 0; example. (Thus, the function } will destroy registers used by int function(int a) calling function) { return 3*a+5; P1 } Computer Systems and Networks Spring 2019
24 # Simple routine to demo functions # NOT using a stack in this example. # Thus, the function does not preserve values # of calling function! # ------------------------------------------------------------------ # ------------------------------------------------------------------ .text # FUNCTION: int fun(int a) # Arguments are stored in $a0 .globl main # Return value is stored in $v0 main: # Return address is stored in $ra (put there by jal instruction) # Register assignments # Typical function operation is: # $s0 = x # $s1 = y fun: # Do the function math li $s0, 3 # Initialize registers mul $s1,$s0,$a0# s1 = 3*$a0 (i.e. 3*a) lw $s0, x # Reg $s0 = x addi $s1,$s1,5 # 3*a+5 lw $s1, y # Reg $s1 = y # Save the return value in $v0 # Call function move $v0,$s1 move $a0, $s0 # Argument 1: x ($s0) jal fun # Save current PC in $ra, and jump to fun # Return from function move $s1,$v0 # Return value saved in $v0. This is y ($s1) jr $ra # Jump to addr stored in $ra # Print msg1 # ------------------------------------------------------------------ li $v0, 4 # print_string syscall code = 4 la $a0, msg1 # Start .data segment (data!) syscall .data x: .word 5 # Print result (y) y: .word 0 li $v0,1 # print_int syscall code = 1 msg1: .asciiz "y=" move $a0, $s1 # Load integer to print in $a0 lf: .asciiz"\n" syscall # Print newline li $v0,4 # print_string syscall code = 4 la $a0, lf syscall # Exit li $v0,10 # exit syscall Computer Systems and Networks Spring 2019
25 Preserving Registers ì What if we don’t want to destroy registers used by the calling function? Perhaps $s0-$s7 are in use with important data… ì Or $ra holds the return address of a previous call… ì ì Need to save those registers somewhere while our function runs (like memory!) ì A stack is a good structure for this Computer Systems and Networks Spring 2019
Recommend
More recommend