Spring 2014 feb 10 spring elec 5200 0016200 001

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: st be saved. Spring 2014, Feb 10 . . . Spring ELEC 5200-001/6200-001 Lecture 4 29 Saved Registers for 4! Example Initial $sp $sp Return address within main program Argument register, $a0 = 4 Return address within fact Argument register, $a0 = 3 Return address within fact Argument register, $a0 = 2 Return address within fact Argument register, $a0 = 1 Spring 2014, Feb 10 . . . Spring ELEC 5200-001/6200-001 Lecture 4 } fact calls fact, $a0 = 3 } fact calls fact, $a0 = 2 } fact calls fact, $a0 = 1 } fact calls fact, $a0 = 0 30 Assembly Code for fact Assembly fact fact: addi sw sw $sp, $sp, -8 $ra, 4($sp) $a0, 0($sp) slti beq $t0, $a0, 1 # $t0 = 1, if n ≤ 1, i.e., n = 0 $t0, $zero, L1 # go to L1, if n ≥ 1 addi $v0, $zero, 1 # return $v0 = 1 to caller addi $sp, $sp, 8 jr $ra # no need to restore registers, since none was changed, but must restore stack pointer # return to caller instruction after jal Spring 2014, Feb 10 . . . Spring # adjust stack for two items # save return address of caller # save caller supplied argument n ELEC 5200-001/6200-001 Lecture 4 31 Assembly Code Continued L1: addi $a0, $a0, -1 jal fact # set $a0 to n-1 # call fact with argument n-1 lw $a0, 0($sp) lw $ra, 4($sp) addi $sp, $sp, 8 # on return from fact, restore n # restore return address # adjust stack pointer mul $v0...
View Full Document

This document was uploaded on 02/23/2014 for the course COMUPER AR 6200 at Auburn University.

Ask a homework question - tutors are online