Can somebody please walk through this MIPS code with me?

FrostyStraw

enter image description here

enter image description here

My first question is

  1. when adjusting stack space, why was the number 8 chosen? I'm assuming it means 8 bytes, but how was it determined that 8 bytes would be needed to adjust? I know we are supposed to allocate enough space for all the arguments (in this case, one argument) and I'm guessing also for the return address, but how do we know how many bytes each one needs?

  2. I'm trying to read through the code to understand what happens, but I get confused at

beq $t0, $zero, L1.

I know from here we branch to where it says L1, where n is decremented by 1, but then I'm confused. Does

jal fact

mean we immediately return to where it says fact at the top, but this time with the argument being n = 1 instead of n = 2? [assuming n = 2 was our original argument] At what point do we reach the code where it says to multiply $a0 and $v0?

I am honestly having a really hard time wrapping my brain around all of this. If I've understood it, everytime the factorial function is called, a new "stack frame" is added to the stack...each frame allocates 8 bytes into the stack? and each new frame contains a return address to return to the procedure that called it..If I'm understanding that correctly, I'm having difficulty seeing it in code.

deviantfan

enter image description here

Answer 1 is in your code´s comments:
This "8" depends on your code, it is the "n" in the picture.

8 byte because you need place for two values. Each integer value has 4 byte...
The values are one parameter and the return address.
(The caller and the called function have two separate return addresses,
but in the register is only place for one.
So the caller saves his address on the stack temporarily)

About the beq: Correct. If $t0 and $zero are equal (ie. $t0 has the value 0) then jump to L1.

And about the jal...
Well, to put it in one or two words: It´s a function call, no jump.

If you have no function calls or jumps, your code is processed top-down, each instructon.
Imagine you have something in the computer pointing here you are right now.
So, if the jal is to be processed, it points to the jal instruction.
What the happens there:
You get a another instruction pointer to the first instruction of fact,
hile the first one remains at jal.
Then, the second pointer will go top-down the function,
while the first one remains where it was.
Until the second pointer reaches his end: jr.
There, the second pointer is removed again,
and the first one will continue where it stopped before.
...
The "magic" about recursion is, that the second pointer could spawn a third
and wait for him and so on...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Please walk me through this code from ruby monk

From Dev

Could somebody please help me with this code?

From Dev

Walk me through the Linux log files (please)

From Dev

Can somebody please briefly explain to me what's happening with the IntStream?

From Dev

I am trying to search user by name, and load some data to Recycleview. But the code is not working. Can somebody help me please

From Dev

Can somebody run me through a clean install of 13.10?

From Dev

Can somebody run me through a clean install of 13.10?

From Dev

Can somebody please explain WaitForSeconds()?

From Dev

Can somebody please help me to avoid internal server error | htaccess | apache2ctl | backtrack

From Dev

Can someone walk me throu the angular code

From Dev

Can somebody explain these C code?

From Dev

Can somebody explain these lines in this code?

From Dev

Can you walk me through how to download and install Windows?

From Dev

Can someone please explain me this code?

From Dev

Can somebody tell me why my "thumbnails" aren't in the row format I am attempting in code link?

From Dev

Can somebody explain this javascript statement to me?

From Dev

Can somebody explain me the working of this program with stacks?

From Dev

Can somebody tell me what is wrong with this picture?

From Dev

Can somebody explain this piece of clojure code?

From Dev

Can somebody explain the last method in this code?

From Dev

What is the role of if statements in this code can somebody explain?

From Dev

Hello, can somebody help me or any suggstion code how can I start in conditional for getting the range of specific range number

From Dev

Please explain me this code?

From Dev

Can somebody tell me how can this regular expression match anything?

From Dev

Can someone please help me fix this Java code?

From Dev

Can someone please help me with the code I am writing? (Java)

From Dev

why we use this code please can any one tell me

From Dev

Can someone please explain to me how this code works?

From Dev

Can someone walk me through serving gzipped files from Cloudfront via S3 origin?

Related Related

  1. 1

    Please walk me through this code from ruby monk

  2. 2

    Could somebody please help me with this code?

  3. 3

    Walk me through the Linux log files (please)

  4. 4

    Can somebody please briefly explain to me what's happening with the IntStream?

  5. 5

    I am trying to search user by name, and load some data to Recycleview. But the code is not working. Can somebody help me please

  6. 6

    Can somebody run me through a clean install of 13.10?

  7. 7

    Can somebody run me through a clean install of 13.10?

  8. 8

    Can somebody please explain WaitForSeconds()?

  9. 9

    Can somebody please help me to avoid internal server error | htaccess | apache2ctl | backtrack

  10. 10

    Can someone walk me throu the angular code

  11. 11

    Can somebody explain these C code?

  12. 12

    Can somebody explain these lines in this code?

  13. 13

    Can you walk me through how to download and install Windows?

  14. 14

    Can someone please explain me this code?

  15. 15

    Can somebody tell me why my "thumbnails" aren't in the row format I am attempting in code link?

  16. 16

    Can somebody explain this javascript statement to me?

  17. 17

    Can somebody explain me the working of this program with stacks?

  18. 18

    Can somebody tell me what is wrong with this picture?

  19. 19

    Can somebody explain this piece of clojure code?

  20. 20

    Can somebody explain the last method in this code?

  21. 21

    What is the role of if statements in this code can somebody explain?

  22. 22

    Hello, can somebody help me or any suggstion code how can I start in conditional for getting the range of specific range number

  23. 23

    Please explain me this code?

  24. 24

    Can somebody tell me how can this regular expression match anything?

  25. 25

    Can someone please help me fix this Java code?

  26. 26

    Can someone please help me with the code I am writing? (Java)

  27. 27

    why we use this code please can any one tell me

  28. 28

    Can someone please explain to me how this code works?

  29. 29

    Can someone walk me through serving gzipped files from Cloudfront via S3 origin?

HotTag

Archive