Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have posted my answer to question 1 at the very bottom. Please answer Question

ID: 3790394 • Letter: I

Question

I have posted my answer to question 1 at the very bottom. Please answer Question 3 Part a-f.

Homework 2: String manipulation, loops, branches, and subroutine calls in MIPS Background: .asciiz stores a string with the null terminator 3 data 4 lower asci iz "hola 5 upper .asciiz ''xxxx'' 0x10010000 a l o h x x x 10 10 10 10 x .ascii stores a string without a null terminator 3 data 4 lower ascii "hola 5 upper .ascii "xxxx" 0x10010000 a l o h x x x x Since MIPS stores a string as ASCII bytes, it will make sense to use the lbu/sb load/store byte instructions. 1. Write a MIPS program to copy a lowercase string to another string, converted to uppercase. For example: "hola" should become "HOLA". Store stringiower as asciiz and reserve space for string upper using .space. Look up an ASCII table in hex online or in the notes and notice the difference in hex between upper and lower case letters to figure out how to easily convert from one to the other. Your program should loop until it encounters the null terminator. Take a screen shot of the program code. Take another screen shot of the memory area after the program ran, but check the ASCII box below the memory window.

Explanation / Answer


ub TestSub Wscript.Echo "We're in a subroutine." Wscript.Echo "This is not a good use of a subroutine." End Sub

As you can see, this is simply two Wscript.Echo statements grouped together inside Sub and End Sub statements. Following the opening Sub statement you see the name TestSub. This is the name of the subroutine. If we simply pasted this subroutine as-is into Notepad, saved it as a .vbs file and tried to run it, nothing would happen. We wouldn’t get an error message, but we also wouldn’t see the statements we echoed. Why? Because the whole point of naming a subroutine is so that you can use that name to call the subroutine. Try this:

TestSub Sub TestSub Wscript.Echo "We're in a subroutine." Wscript.Echo "This is not a good use of a subroutine." End Sub

As you can see, in our first line we call the TestSub subroutine. That, in turn, runs the two lines of code within the subroutine and echoes the two statements. Now take a look at this subroutine:

Sub TestSub(x) Wscript.Echo x Wscript.Echo "Still not a good use of a subroutine." End Sub

This time we’ve included a variable enclosed in parentheses - which is called a parameter or argument - following the name of the subroutine. We do this so we can pass a value from the main part of the script to the subroutine:

x = 4 TestSub x Sub TestSub(x) Wscript.Echo x Wscript.Echo "This is not a good use of a subroutine." End Sub

When you run this script, the value 4 will be echoed to the screen, followed by the string of text. In reality, you could have simply done this:

x = 4 TestSub Sub TestSub Wscript.Echo x Wscript.Echo "This is not a good use of a subroutine." End Sub

In this case we didn’t pass x to the subroutine, but the results will be the same. However, there are several reasons for passing parameters. The whole point of creating a subroutine is to be able to call the same section of code from multiple places in a script. So it stands to reason that when the subroutine is called, the parameter will change, as will the variable passed to the parameter. Kind of like this:

x = 4 y = 3 z = 7 TestSub x y = x + y + z TestSub y Sub TestSub(a) Wscript.Echo a End Sub

In this script, we assign values to several variables. We then call TestSub, passing it the variable x which contains a value of 4. TestSub will then echo that value. After we echo the value we reach the end of the subroutine. At that point we go back to the main part of the script; kind of like we took a side trip and now we have to go back where we turned off and continue straight through our script. So we continue with the line that adds the three variables and assigns that value to the variable y. We then call TestSub again, this time echoing back the value we passed to it. At this point the script ends. Your output will look like this:

4 14

You might have noticed that we passed the variables x and y to the subroutine, but the subroutine used the variable a to echo the value. The names of the variables don’t really matter. We’re not passing the variable to the subroutine, we’re telling the subroutine where the variable is in memory and the subroutine is assigning its own variable to that section of memory. So even though we use the variable a in the subroutine, if we change the value of a in the subroutine that will also change the value of x in the main part of the script, because both variables are looking at the same piece of memory. Give it a try:

x = 7 Wscript.Echo "Before subroutine call x = " & x TestSub x Wscript.Echo "After subroutine call x = " & x Sub TestSub(a) Wscript.Echo "In subroutine, original value of a = " & a a = 10 Wscript.Echo "In subroutine, changed value of a = " & a End Sub

Your output will look like this:

Before subroutine call x = 7 In subroutine, original value of a = 7 In subroutine, changed value of a = 10 After subroutine call x = 10

As you can see, we assigned a value of 7 to the variable x and echoed back that value. We then called TestSub, passing it the variable x. But we’re not passing the value within that variable, we’re actually passing the location in the computer’s memory that the variable is referencing. That location in memory contains the value 7. What that means is that we passed the memory location to TestSub’s parameter, a, so a is now referencing the location in your computer’s memory that’s holding the value 7. In fact, we can see that when we first echo the value of a that value is 7.

We then change the value of a, which changes the value in that memory location. We echo the new value, then exit the subroutine. When we get back to the main part of our script, x is still pointing to that same spot in memory, but now instead of a 7 that spot has a 10 in it, so x has a value of 10.

This is called passing parameters by reference (ByRef), which is the default behavior of VBScript. You can also pass parameters by value. To do this you must specify ByVal in the subroutine:

Sub TestSub(ByVal a)

Passing by value means that the variables are no longer passing references to memory locations, they’re passing actual values. Let’s take a look at the same script, only using the ByVal keyword:

x = 7 Wscript.Echo "Before subroutine call x = " & x TestSub x Wscript.Echo "After subroutine call x = " & x Sub TestSub(ByVal a) Wscript.Echo "In subroutine, original value of a = " & a a = 10 Wscript.Echo "In subroutine, changed value of a = " & a End Sub

This time our output looks like this:

Before subroutine call x = 7 In subroutine, original value of a = 7 In subroutine, changed value of a = 10 After subroutine call x = 7

Taking a look at the output, you can see that changing the value of a within the subroutine did not change the value of x. This is because when we passed x as a parameter, we passed only the value 7, not the location in memory where that 7 is stored. So the variable named a in TestSub received the value 7 and put it in a new location in memory. At this point there are two locations in your computer’s memory that contain the value 7, one location referenced by x and one referenced by a. So when we change the value of a, it has no effect on x.

In other words, if you want to use a value in a subroutine but you don’t want the variable being passed to be changed in any way, then use the ByVal keyword.

Note: Just to add to the confusion, you could have named the variable in TestSub x and the results would be the same: you can change the value in the subroutine, but if you specified ByVal it won’t change the value in the main body of the script because it’s a different variable referencing a different location in memory, even if it does have the same name.

A function works pretty much the same as a subroutine, except that you can use the function in an equation or assignment statement because the function itself returns a value:

x = 4 y = TestFunc(x) Wscript.Echo y Function TestFunc(a) Wscript.Echo a TestFunc = a + 2 End Function

In this script we assign a value to the variable x. We then call TestFunc, passing it the value in x. (Yes you’re right, we’re really passing it the memory location of x because we didn’t specify ByVal.) Notice how we put the parameter in parentheses when we called the function. This is required when you put a function call within some sort of equation, as we did here. We’re assigning the value returned by the function to the variable y. And how did the function get a value? Like this:

TestFunc = a + 2

In the last line of the function we assign a value to the name of the function. That’s all there is to it. Here’s the output:

4

6