Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Microsoft may have 'helpfully' broken that so test it first...) the Sra if you want to have many procedures chained together though. Technically ialto a

image text in transcribed

(Microsoft may have 'helpfully' broken that so test it first...) the Sra if you want to have many procedures chained together though. Technically ialto a label saves the current program counter the location of the instruction being executed) Procedure Calls You are going to convert a simple program (on the next page, you can copy paste) into a simple leaf procedure (leaf means it doesn't call anything else) Fairly simply, you do this with labels -label your program above something useful (say, multiply), read in two integers from the user, and "jal multiply" Combine that program with a labelled procedure that will store a value $s into the stack, and that puts in a new line (that's the program below) SSR: the stack pointer. When you push things (whatever they are) onto the stack you need to pop them back off when you are done and put them back where they belong. How do we do this, we want to Push variables onto the stack to save them, and then pop them off when we're done. .data newline: text asciis " " Ok so that's what you've done before basically. Ser is the stack pointer, it's supposed to be the memory address that is the top of the stack. The custom is to push onto the stack from larger memory addresses to smaller. You do this by copying something to the next available memory address, and then adjusting the stack pointer In a real program you may have numerous procedures (functions or methods) being called, sometimes many layers deep, these are called nested procedures and we're getting there. The goal here is to think from the perspective of the method being called (that is the callee')- you want to provide a service to a caller without mucking up its data. addi $30, Szero, 10 So to push something onto the stack you save it at memory location Ssp-4, then you set Ssp=sp-4 To Pop something off the top of the stack is Ssr (the top element) and then sp=ssp+4 jad 1065683e3tasis So what we need to do is to push the caller data onto a stack (and keep track of what is on the stack) execute our procedure, and then restore the stack. Simple example This examples Pushes the contents of register 15 (r15) into the stack li $v0, 4 la sau, newlink SV5611 #print Conceptually think of a fairly simple problem Foo(parani, param2) li $v0, 1 move Sal, $50 svscout sub $sp, 8x84 Asubtract four from the stack pointer sw $15, $sp Int a, b: Bar(); Bar Int a, b; To pop the stack lw $15, $sp add $sp, 9x84 The reason there aren't 'push' and 'pop' instructions is because, well, RISC #end of program li $v0, 10 scelt increases tact: adda Sae. $ae. -4 = $50, 0(Sae) Or perhaps usefully, and this is what allows us to make a nested procedure So what happens to "a" and "b"? Broadly speaking this is also the problem of scope, and differently languages behave differently in terms of how nested functions can see caller variables and so on (and if it can see them, how you access them). addi $so, $50, 30 #this is the only line that changes so We are working closer to the metal than that. We have a piece of data in a specific memory location, but what if some called function wants to use the same memory location but to store something else, after all, if I'm the callee I can't account for all possible memory states when I get called. sub $sp, Ox04 # set the stack pointer sw $ra, $sp #save it (for a push) jal sum function lw $ra, $sp #restore return address add $sp, 8x84 Hand the stack pointer ir $ra li $v0, 1 move Sa0,$so svsco!! The convention for MIPS Ss registers saved, $t registers are... well, they can be saved. (Ideally $s are caller saved, St callee saved, but you probably won't do that). OK so what do I want you to actually do? Start with this simple program (feel free to copy paste). That prints the number 10. #this restores the previous state in memory 1x $50, 0(Sa) addi Sar. Sar. 4 js Sra #jumps back to where you were before There are two ways to do this caller and callee saving (caller saving, above, would mean Fool.) would save a and b, then call Bar(), then restore a, and b), callee saving would be Foo calls Bar, bar saves a and b, then restores them. add the Band 10 and store in $se Ok some terminology, program counter: the memory location of the instruction being executed. After executing an instruction you go to current program counter + 4 since instructions are 4 bytes long. (You never work with the PC number directly, but it is stored in the return address). .data newline: .asciiz " " .text main: addi $se, $zero, 10 Uprint value 11 sve, 1 move $ae, syscall end of program 11 sve. 10 Syscall Pay attention to what this does, inside the procedure we save any Ss register we want to use by pushing it onto the stack, then we can do whatever we want with that register, and when we finish we pop them back to where they were. Sra: is the return address. It's where you jump to, to return to a previous state (eg. when you are done your procedure you ir Sra and you are back at the calling function). You need to keep track of what is in (Microsoft may have 'helpfully' broken that so test it first...) the Sra if you want to have many procedures chained together though. Technically ialto a label saves the current program counter the location of the instruction being executed) Procedure Calls You are going to convert a simple program (on the next page, you can copy paste) into a simple leaf procedure (leaf means it doesn't call anything else) Fairly simply, you do this with labels -label your program above something useful (say, multiply), read in two integers from the user, and "jal multiply" Combine that program with a labelled procedure that will store a value $s into the stack, and that puts in a new line (that's the program below) SSR: the stack pointer. When you push things (whatever they are) onto the stack you need to pop them back off when you are done and put them back where they belong. How do we do this, we want to Push variables onto the stack to save them, and then pop them off when we're done. .data newline: text asciis " " Ok so that's what you've done before basically. Ser is the stack pointer, it's supposed to be the memory address that is the top of the stack. The custom is to push onto the stack from larger memory addresses to smaller. You do this by copying something to the next available memory address, and then adjusting the stack pointer In a real program you may have numerous procedures (functions or methods) being called, sometimes many layers deep, these are called nested procedures and we're getting there. The goal here is to think from the perspective of the method being called (that is the callee')- you want to provide a service to a caller without mucking up its data. addi $30, Szero, 10 So to push something onto the stack you save it at memory location Ssp-4, then you set Ssp=sp-4 To Pop something off the top of the stack is Ssr (the top element) and then sp=ssp+4 jad 1065683e3tasis So what we need to do is to push the caller data onto a stack (and keep track of what is on the stack) execute our procedure, and then restore the stack. Simple example This examples Pushes the contents of register 15 (r15) into the stack li $v0, 4 la sau, newlink SV5611 #print Conceptually think of a fairly simple problem Foo(parani, param2) li $v0, 1 move Sal, $50 svscout sub $sp, 8x84 Asubtract four from the stack pointer sw $15, $sp Int a, b: Bar(); Bar Int a, b; To pop the stack lw $15, $sp add $sp, 9x84 The reason there aren't 'push' and 'pop' instructions is because, well, RISC #end of program li $v0, 10 scelt increases tact: adda Sae. $ae. -4 = $50, 0(Sae) Or perhaps usefully, and this is what allows us to make a nested procedure So what happens to "a" and "b"? Broadly speaking this is also the problem of scope, and differently languages behave differently in terms of how nested functions can see caller variables and so on (and if it can see them, how you access them). addi $so, $50, 30 #this is the only line that changes so We are working closer to the metal than that. We have a piece of data in a specific memory location, but what if some called function wants to use the same memory location but to store something else, after all, if I'm the callee I can't account for all possible memory states when I get called. sub $sp, Ox04 # set the stack pointer sw $ra, $sp #save it (for a push) jal sum function lw $ra, $sp #restore return address add $sp, 8x84 Hand the stack pointer ir $ra li $v0, 1 move Sa0,$so svsco!! The convention for MIPS Ss registers saved, $t registers are... well, they can be saved. (Ideally $s are caller saved, St callee saved, but you probably won't do that). OK so what do I want you to actually do? Start with this simple program (feel free to copy paste). That prints the number 10. #this restores the previous state in memory 1x $50, 0(Sa) addi Sar. Sar. 4 js Sra #jumps back to where you were before There are two ways to do this caller and callee saving (caller saving, above, would mean Fool.) would save a and b, then call Bar(), then restore a, and b), callee saving would be Foo calls Bar, bar saves a and b, then restores them. add the Band 10 and store in $se Ok some terminology, program counter: the memory location of the instruction being executed. After executing an instruction you go to current program counter + 4 since instructions are 4 bytes long. (You never work with the PC number directly, but it is stored in the return address). .data newline: .asciiz " " .text main: addi $se, $zero, 10 Uprint value 11 sve, 1 move $ae, syscall end of program 11 sve. 10 Syscall Pay attention to what this does, inside the procedure we save any Ss register we want to use by pushing it onto the stack, then we can do whatever we want with that register, and when we finish we pop them back to where they were. Sra: is the return address. It's where you jump to, to return to a previous state (eg. when you are done your procedure you ir Sra and you are back at the calling function). You need to keep track of what is in

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

Students also viewed these Databases questions

Question

RP-1 What is the value of finding flow in our work?

Answered: 1 week ago

Question

Know how productivity improvements impact quality and value.

Answered: 1 week ago

Question

Recommend the key methods to improve service productivity.

Answered: 1 week ago