This video really helped me understand why registers are saved to the stack. My Computer Architecture class glazed over it a little bit. Thanks a ton. Probably the best MIPS series on UA-cam.
i f*****g understood it. Our education system is just full of people who like to teach things in an complex way that are actually simple. I just want you to know that you are loved
Congratulations Amell! I am trying to learn assembly in university and you are helping me a lot. I hadn' t understood a lot, but you helped me to learn it! GOOD JOB!
Thanks a lot for these videos. Really helpful in understanding the concepts of MIPS!! Clear and concise. These are the best resources to learn assembly coding.
Amazing tutorials, Quasar Distant! Your way of teaching is so good that i passed all my college exams of MIPS with a such a ease. Thank you for taking your time to make these tutorials!
Thank you. This helps me a lot for the Computer Architecture. I got a lot of confused just listening in the lecture class. After I watch your vid and download Mars to try for myself I understand a lot about it. Thank you, again.
Hey, did you know Johns Hopkins is using your videos in their school of Engineering? You should contact them and ask them to give you credit. They use this video in their Computer Organization class--downloaded it into their Blackboard system for private viewing. The class has, like, 7 sections. Hundreds of students are watching. You should get credit man!!
in which situations would we choose to use the register $s0 over a t register? What are the practical circumstances in which we need to deploy this strategy?
Thank you so much man, I’m watching this way too late into my semester but it really cleared up a lot of questions I had, although I had a question which is, what would happen if we don’t restore the stack in this situation?
I have a question, How come you did: sw $s0, 0($sp) in increaseMyRegister instead of: sw $a0, 0($sp)? Isn't the value that you send an argument, so you should be using storing the argument register instead of whats on $s0? Also, great video tutorial, thank you so much!
also can i allocate bytes not only integer right? for example if i want my function to do something with an array of bytes, and the number of bytes are e.g ten so i would give 10 positions in the stack?
What do you mean by positive numbers adding to the stack and negative numbers taking from it ?! Isn't every integer, regardless of its sign, is stored as 32 bits in memory, doesn't that mean that it always consumes 4 bytes ?
It actually doesn't matter that if we use s or t registers. I tried EXACTLY the same thing with t register and it went the same way. So what is the difference between s and t registers?
I saved the value on the stack because MIPS procedure calling conventions specify that $s registers are callee-saved registers, so their values must be restored when the function/procedure that used them returns. If I had used $t registers instead of $s registers, then I wouldn't have to save the value inside the procedure because $t registers are caller-saved registers.
Something I have difficult to understand if im not allocating memory (place) in stack I can still get same answer, so why I have to do it? If it's rules to do it, it doesn't make sense . And i tired to change from S registers to T register, i will get same answer. BTW THANK YOU AGAIN FOR BEST VIDEOS.
You can think of $s registers as local variables: when the function modifies an $s register, it is local to that function, so when the function returns, the value in the caller will be preserved. The stack allows us to save those old values in memory, so they can be restored at the end of the function. This behavior is useful for recursion, too.
Why do we need to save the value in $s0 to stack at all? Why not save the value in $s0 to $t0 and work with the $t0 register, modifying it, so that we don't need to change the $s0 register?
+Tiffastic Nguyen There are two main reasons for doing that: 1) MIPS programmers follow the convention that $t registers can be overwritten by any callee procedure, and that $s registers will always be restored to the value they had in the caller procedure. Thus, by following those conventions, we can avoid subtle logic errors in our code, and we also follow good programming practices that help other programmers working on our code avoid subtle errors. 2) Saving and restoring the $s registers in the stack allows programmers to utilize a very powerful programming technique called recursion. If you'd like to know more about recursion, please, watch my videos on it.
so if I want to save 3 or more data elements under a single name like I take a customers name and then I save their information e.g address, number, email under their name and when I want to access them separately. This is the way to do it? In c++ this is done through either 3d arrays or classes specifically pointer objects or object array. If you can tell me how to do this sir, it would be appreciated. Im new to mips and ive done mips coding till 1d array and c++ till tree data structure.
+K Badsha $fp is the frame pointer, it's used to point the last location of $sp before it enters a function so it's easy to return to that location after modifying the function, if that makes sense :)
Shouldn't the caller save the values in $s register to the stack since the caller knows it's using the $s register? Rather than the callee doing the saving?
+Tiffastic Nguyen no. because you don't want the calling scope to have the responsibility of knowing what registers the function scope will be changing. it's a lot easier for the function to say, "i know i'm changing the values of registers x, y, and z. I better save those on the stack so i can restore them."
That's a good question. I did it that way in the tutorials because li is a pseudo-instruction that will be translated into real MIPS instructions by the assembler. It is perfectly fine to use pseudo-instructions (even recommended), but in some computer architecture courses, professors prefer to show you how it is done only using real instructions, so students can obtain a deeper understanding of the MIPS architecture.
+neettim In order to display an integer to the screen, the system expects the value to be placed in register $a0. So I had to use the move pseudo-instruction to put that value there. Essentially, you tell the system what system call you intend to use. li $v0, 1 is for the system call to print integer. Then you pass the arguments to the corresponding registers (in this case $a0). Finally, you use the system instruction to invoke the system call.
Quasar Distant thanks for the explanation. I love your tutorials, they're really helpful! So I just had a lecture today and my professor used load word to put the contents into register $a0. instead of move. ex. Printing a number from the stack. li $v0,1 lw $a0,0($sp) syscall. Do you know why I had to use lw instead of move? If you need more context behind the code, just let me know!
+Hash Sam There are 32 registers (0-31). This is because this version of MIPS is the 32 bit version, meaning that there are 32 registers which can each hold 32 bits.
Hi, is MIPS able to save in stack for persistence purposes? I mean, is it possible to save some number (or a string) and, after reloading the program with simulator, retrieve it from the memory? Is it possible or it is a limit of MIPS? thanks
supersadogoat Yes, you can use system calls to create, open, write, and read files. Check out this link from: stackoverflow.com/questions/25953681/create-and-write-to-file-on-mips
Strangely enough I can write one file, read from file but not in the same program. I mean, those two functions work well when separated in two different MIPS applications. Currently working on it but, may I ask you if you wonder why? stackoverflow.com/questions/30477204/mips-write-and-read-a-file
can I put my professor in the stack until I watch your videos and understand the material and pretend I understand all from his lecture? Thank from my deep heart!!!
In summary: Convention in MIPS regarding $t0, ..., $t7 Temporary registers and $s0, ..., $s7 Saved registers If you are using a t register inside a function, you don't have to save it to the stack, the function can modify the value in the register, and then that modification can be reflected in the caller But if you are using an s register, by convention, the function should not have permission to modify the value of the s register in a way that the modification is reflected in the caller If the function modifies an s register, I will be during the function execution, but the value of the register before and after the function call should stay the same s registers are called saved, and t registers are callee saved The function that we call (the callee) can modify the t registers, but it is not allowed to modify the values in the s regisers in a way that is reflected in the caller
Making a quick-sort algorithm for an assignment. Im like "damn, how do you pass parameters into a recursive call and not mess up the variables for each frame in the call stack?" Well... this is how. My professor did not explain thoroughly how to do this. Thank you very much!
why have to use stack while you can use another $t register acts as temporary register to hold old value to come back later? I think the example used in this video is bad
.data newLine:.asciiz" " .text main: addi $s0,$zero,10 jal incrementing li $v0,4 la $a0,newLine syscall # Printing the values li $v0,1 add $a0,$zero,$s0 syscall # Last lines of li $v0,10 syscall incrementing: # Storing the value into stackpointer addi $sp,$sp,-4 # This is for alloacting the sw $s0,0($sp) # Storing the value into first addi $s0,$s0,30 # Printing new value in function li $v0,1 move $a0,$s0 syscall lw $sp,0($sp) addi $sp,$sp,4 jr $ra Iam getting output 40,40 can any one clarify this
This video really helped me understand why registers are saved to the stack. My Computer Architecture class glazed over it a little bit. Thanks a ton. Probably the best MIPS series on UA-cam.
+The Glass Piñata Incident You're welcome. Happy programming!
when is amell ever not excited?
+phatboi858 lol Good question...
I wonder if he is still excited today
when he got retired
You are a savior dude. I don't know what I would do on my assignments and exams without your videos
my instructors can't explain this topic in 50 minutes, he did in 2 minutes. thank you Amell!!
You're welcome, Berk. Happy coding!
Thank you so much for these MIPS videos. Clear and concise and extremely helpful.
Thank you. Happy coding!
I literally don't know what I would do without these videos thank you so much!
So many light bulb moments while watching this. Thanks!
i f*****g understood it. Our education system is just full of people who like to teach things in an complex way that are actually simple. I just want you to know that you are loved
Why a video 9 years old is better than a whole course I took, omg
I can't really understand my teacher when she teaches and these videos helped me so much in my class. Thank you so much man.
Congratulations Amell! I am trying to learn assembly in university and you are helping me a lot. I hadn' t understood a lot, but you helped me to learn it! GOOD JOB!
Thank you so much, Thomas. I'm glad to know my videos have been helpful to you. Happy assembly programming!
Thanks a lot for these videos. Really helpful in understanding the concepts of MIPS!! Clear and concise. These are the best resources to learn assembly coding.
Thank you. Happy coding!
Amazing tutorials, Quasar Distant! Your way of teaching is so good that i passed all my college exams of MIPS with a such a ease. Thank you for taking your time to make these tutorials!
I'm glad to know that. You're welcome!
thank you sir. So helpful. My professor sucks at explaining this
You're welcome, Kevin.
This was exactly what I needed. Thanks a lot for taking the time to explain the concepts. It finally clicked and all makes sense.
You're welcome, Bridget! Happy coding!
YESSSSS!!!
had to learn this at a non-presence univerity from home - your videos help a lot! thanks :)
You're welcome. I'm happy to know that. Happy coding!
Thanks bro.i didn't expect your tutorials would be so helpful.
Your content is way more amazing than any other available on UA-cam.
Keep it up bro
You are very welcome!
This is still helpful in 2020, thanks a lot
You're welcome. Happy coding!
Love the profile sis
@@jesselam5867 more of a bro, but coding gang les go
Thank you. This helps me a lot for the Computer Architecture.
I got a lot of confused just listening in the lecture class. After I watch your vid and download Mars to try for myself I understand a lot about it. Thank you, again.
You're always welcome. I'm glad to know that my videos helped you in understanding. Happy coding!
great explanation when you revise the code at the end. love the enthusiasm my guy :)
Thank you!
V O I L A !
O
I
L
A
!
Thank You for these videos, they really help a lot with my coding assignments in MIPS
Man your videos are excellent, and your enthusiasm is contagious! Thanks for this material.
Thank you. Happy coding!
I really love your toturial, very clear and easy to learn.
Thank you very much!
I'm glad to know that. You're welcome!
Man, yo series is kickass....Damn professor...i wish we had such 10-min sessions than 2 45min sessions
Happy coding!
Wow great explanation. Was really struggling with the stack concept. Thanks a lot.
+israel r8 You're welcome, Israel. Happy programming!
Great videos dude I wish i had seen this channel while i was taking computer architecture tho
Thank you.
this is more clutch than lebron's block on iguadala in 2016 thankyou i got exam in a couple days
You are very welcome!
Thank you for explaining so clearly! These videos really help me!
Great tutorials :)
I really appreciate it.
+kwantom1996 You're welcome. I'm glad you found this helpful. Happy coding!
This is helping me in my Assembly class so much thank you thank you thank you!!!
You're welcome! Happy coding!
thank u, Amell, these videos help me a lot !!!
You're welcome!
This video is underrated
Amel you are such an awesome person!
Thank you, Austin! Happy coding!
Hey, did you know Johns Hopkins is using your videos in their school of Engineering? You should contact them and ask them to give you credit. They use this video in their Computer Organization class--downloaded it into their Blackboard system for private viewing. The class has, like, 7 sections. Hundreds of students are watching. You should get credit man!!
Thank you for letting me know. Happy coding!
in which situations would we choose to use the register $s0 over a t register? What are the practical circumstances in which we need to deploy this strategy?
Thank you so much man, I’m watching this way too late into my semester but it really cleared up a lot of questions I had, although I had a question which is, what would happen if we don’t restore the stack in this situation?
you are better than my professor...
Thank you!
I would have failed but for these videos thanks Amell!
You're quite welcome!
I have a question,
How come you did: sw $s0, 0($sp) in increaseMyRegister instead of: sw $a0, 0($sp)?
Isn't the value that you send an argument, so you should be using storing the argument register instead of whats on $s0?
Also, great video tutorial, thank you so much!
correction: t registers are CALLER saved while s registers are CALLEE saved
Thanks Kermit, this really helps with my homework.
You're welcome. Happy coding!
Thats good.. it help me alot in my studies....
I'm glad to know that, Engr. Aftab. Happy coding!
What a man
So this is like passing by value in C right?
Restoring the space in the stack is just like free allocated memory in the stack.
thank you very much my sir you are the best it's very helpful
You're welcome! Happy coding!
Great video thnx!
so if i use temp registers, i don't need sp and use dynamic memory?
Anyway I could get in contact with you? I have some questions about this video, thank you.
Thank you for this 👍
You are very welcome, Carla!
also can i allocate bytes not only integer right? for example if i want my function to do something with an array of bytes, and the number of bytes are e.g ten so i would give 10 positions in the stack?
Thanks you are the best!
Remarkable work.
@@Spyro-kt8gy Thank you!
a really huge thanks to you :) so usefull videos, grazie mille!
Prego, Sophie! I'm very glad you find my videos useful. Happy coding!
my man.
if i were to see you in person i would give you a dap
+Krashevil Yeah... We gotta be cool!
What do you mean by positive numbers adding to the stack and negative numbers taking from it ?!
Isn't every integer, regardless of its sign, is stored as 32 bits in memory, doesn't that mean that it always consumes 4 bytes ?
Please tell me how to save two Doubles to a Stack ?
CREATE Amazing, thanks my friend.
You're welcome. Happy coding!
why does it still work if the lines where i addi place out of/in the sp are taken out? (thanks for your tuto btw, rly helps me learning assembly)
Can you make a tutorial on how to draw a square on the bitmap tool?
i didn t understand what did you meen by colly safe !
wow, thank you so much
You are very welcome!
Will saving and pulling from the stack greatly increase the run time of a program? would it be faster to use an array?
It actually doesn't matter that if we use s or t registers. I tried EXACTLY the same thing with t register and it went the same way. So what is the difference between s and t registers?
Thank you but for create one array in the stack?
I’m having trouble understanding your accent when he says t registers are “collee” saved. What is he trying to say here? Thanks
He means "that which is called." The caller calls the callee.
Few years later and youre still saving cs first semesters
hey Amell, I have question, what happen if we dont have this line #addi $sp, $sp, 4 why do we have to do that? thank for your great vids.
To restore the stack pointer to its original place
can you also implement the newLine in the function right;
Did you saved your "old value" in stack pointer cause you wont to "destroy the value"? Thank you again for videos @6.36
I saved the value on the stack because MIPS procedure calling conventions specify that $s registers are callee-saved registers, so their values must be restored when the function/procedure that used them returns. If I had used $t registers instead of $s registers, then I wouldn't have to save the value inside the procedure because $t registers are caller-saved registers.
Something I have difficult to understand if im not allocating memory (place) in stack I can still get same answer, so why I have to do it? If it's rules to do it, it doesn't make sense . And i tired to change from S registers to T register, i will get same answer.
BTW THANK YOU AGAIN FOR BEST VIDEOS.
You can think of $s registers as local variables: when the function modifies an $s register, it is local to that function, so when the function returns, the value in the caller will be preserved. The stack allows us to save those old values in memory, so they can be restored at the end of the function. This behavior is useful for recursion, too.
Why do we need to save the value in $s0 to stack at all? Why not save the value in $s0 to $t0 and work with the $t0 register, modifying it, so that we don't need to change the $s0 register?
+Tiffastic Nguyen
There are two main reasons for doing that:
1) MIPS programmers follow the convention that $t registers can be overwritten by any callee procedure, and that $s registers will always be restored to the value they had in the caller procedure. Thus, by following those conventions, we can avoid subtle logic errors in our code, and we also follow good programming practices that help other programmers working on our code avoid subtle errors.
2) Saving and restoring the $s registers in the stack allows programmers to utilize a very powerful programming technique called recursion. If you'd like to know more about recursion, please, watch my videos on it.
+Quasar Distant Thank you! I understand it better now.
+Tiffastic Nguyen You're welcome. Happy programming, and keep in touch! ;)
so if I want to save 3 or more data elements under a single name like I take a customers name and then I save their information e.g address, number, email under their name and when I want to access them separately. This is the way to do it? In c++ this is done through either 3d arrays or classes specifically pointer objects or object array. If you can tell me how to do this sir, it would be appreciated. Im new to mips and ive done mips coding till 1d array and c++ till tree data structure.
stacks in c++ like single or double linked list can do the problem as well so i though maybe the same concept applies here?
Hey, great tutorial but my teacher uses $fp for his functions. Could you explain to me why we need that and how we'll use it?
+K Badsha $fp is the frame pointer, it's used to point the last location of $sp before it enters a function so it's easy to return to that location after modifying the function, if that makes sense :)
Shouldn't the caller save the values in $s register to the stack since the caller knows it's using the $s register? Rather than the callee doing the saving?
+Tiffastic Nguyen no. because you don't want the calling scope to have the responsibility of knowing what registers the function scope will be changing. it's a lot easier for the function to say, "i know i'm changing the values of registers x, y, and z. I better save those on the stack so i can restore them."
why do you allways use addi $register,$zero,number
instead of li $register,number?
That's a good question. I did it that way in the tutorials because li is a pseudo-instruction that will be translated into real MIPS instructions by the assembler. It is perfectly fine to use pseudo-instructions (even recommended), but in some computer architecture courses, professors prefer to show you how it is done only using real instructions, so students can obtain a deeper understanding of the MIPS architecture.
using li $register, number is better than addi instruction. Try using addi with a big ( like 10mln ) number.
Thank you for the videos
You're welcome. I will make more and better videos soon. Get ready for recursion...
Can someone explain to me what the 'move' command does and why it's used? Why do we need to move the value from $s0 to $a0?
+neettim In order to display an integer to the screen, the system expects the value to be placed in register $a0. So I had to use the move pseudo-instruction to put that value there. Essentially, you tell the system what system call you intend to use. li $v0, 1 is for the system call to print integer. Then you pass the arguments to the corresponding registers (in this case $a0). Finally, you use the system instruction to invoke the system call.
Quasar Distant thanks for the explanation. I love your tutorials, they're really helpful! So I just had a lecture today and my professor used load word to put the contents into register $a0. instead of move.
ex. Printing a number from the stack.
li $v0,1
lw $a0,0($sp)
syscall.
Do you know why I had to use lw instead of move? If you need more context behind the code, just let me know!
i don't know how to get a char from a given string
i have to make lowercase chars uppercase
how many registers on MIPs simulator MARS (not counting coprocessor registers), what are those?
+Hash Sam There are 32 registers (0-31). This is because this version of MIPS is the 32 bit version, meaning that there are 32 registers which can each hold 32 bits.
Hi, is MIPS able to save in stack for persistence purposes? I mean, is it possible to save some number (or a string) and, after reloading the program with simulator, retrieve it from the memory? Is it possible or it is a limit of MIPS?
thanks
supersadogoat
Yes, you can use system calls to create, open, write, and read files.
Check out this link from:
stackoverflow.com/questions/25953681/create-and-write-to-file-on-mips
wow! thanks! this thing was giving me headaches.... :)
You're welcome!
Strangely enough I can write one file, read from file but not in the same program. I mean, those two functions work well when separated in two different MIPS applications. Currently working on it but, may I ask you if you wonder why?
stackoverflow.com/questions/30477204/mips-write-and-read-a-file
can I put my professor in the stack until I watch your videos and understand the material and pretend I understand all from his lecture?
Thank from my deep heart!!!
You're welcome. Happy coding!
Hey could you please help me with one of my project based on this. I am new to assembly language
Thanks
I'm very busy with school and work right now. So I won't be able to help you at the moment.
Eu te amo!
I LOVE AMELL
Thank you. Happy coding!
thank you
You are welcome!
thanks from 2024 Amell!
You're welcome!
In summary:
Convention in MIPS regarding $t0, ..., $t7 Temporary registers and $s0, ..., $s7 Saved registers
If you are using a t register inside a function, you don't have to save it to the stack, the function can modify the value in the register, and then that modification can be reflected in the caller
But if you are using an s register, by convention, the function should not have permission to modify the value of the s register in a way that the modification is reflected in the caller
If the function modifies an s register, I will be during the function execution, but the value of the register before and after the function call should stay the same
s registers are called saved, and t registers are callee saved
The function that we call (the callee) can modify the t registers, but it is not allowed to modify the values in the s regisers in a way that is reflected in the caller
is it odd that I've digested more MIPS today than i did up to this point in the semester than at this fancy smancy 4 year university.
Fucking love you brother. Good shit.
Making a quick-sort algorithm for an assignment. Im like "damn, how do you pass parameters into a recursive call and not mess up the variables for each frame in the call stack?" Well... this is how. My professor did not explain thoroughly how to do this. Thank you very much!
You're welcome. Happy coding, Daniel!
from -4 to 4 there is 8 bits i don't inderstand why we did li $s0, (0)sp, 4 and not li $s0, (0)sp, 0
why have to use stack while you can use another $t register acts as temporary register to hold old value to come back later?
I think the example used in this video is bad
KING
2021 :D
you sound like muhhamad ali
DOLLAR SIGNNN :D
2024
Sadly zero understanding in this video
.data
newLine:.asciiz"
"
.text
main:
addi $s0,$zero,10
jal incrementing
li $v0,4
la $a0,newLine
syscall
# Printing the values
li $v0,1
add $a0,$zero,$s0
syscall
# Last lines of
li $v0,10
syscall
incrementing:
# Storing the value into stackpointer
addi $sp,$sp,-4 # This is for alloacting the
sw $s0,0($sp) # Storing the value into first
addi $s0,$s0,30
# Printing new value in function
li $v0,1
move $a0,$s0
syscall
lw $sp,0($sp)
addi $sp,$sp,4
jr $ra
Iam getting output 40,40
can any one clarify this
Amell > College professor