MIPS Tutorial 17 Saving Registers to the Stack

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 174

  • @theglasspinataincident7405
    @theglasspinataincident7405 9 років тому +114

    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.

    • @amellperalta
      @amellperalta  9 років тому +8

      +The Glass Piñata Incident You're welcome. Happy programming!

  • @phatboi858
    @phatboi858 9 років тому +232

    when is amell ever not excited?

  • @aggelikiboulieri6969
    @aggelikiboulieri6969 3 роки тому +14

    You are a savior dude. I don't know what I would do on my assignments and exams without your videos

  • @berkyildiz95
    @berkyildiz95 6 років тому +8

    my instructors can't explain this topic in 50 minutes, he did in 2 minutes. thank you Amell!!

    • @amellperalta
      @amellperalta  6 років тому +1

      You're welcome, Berk. Happy coding!

  • @Davio88
    @Davio88 8 років тому +28

    Thank you so much for these MIPS videos. Clear and concise and extremely helpful.

  • @Brlitzkreig
    @Brlitzkreig 2 роки тому +3

    I literally don't know what I would do without these videos thank you so much!

  • @iroharin1134
    @iroharin1134 10 років тому +42

    So many light bulb moments while watching this. Thanks!

  • @MrPflanzenLp
    @MrPflanzenLp Рік тому +1

    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

  • @TherealShacharTs
    @TherealShacharTs 4 місяці тому +2

    Why a video 9 years old is better than a whole course I took, omg

  • @karljoshuamiranda4386
    @karljoshuamiranda4386 2 роки тому +1

    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.

  • @thomasboufikos7485
    @thomasboufikos7485 6 років тому +3

    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!

    • @amellperalta
      @amellperalta  6 років тому

      Thank you so much, Thomas. I'm glad to know my videos have been helpful to you. Happy assembly programming!

  • @abhijeetsaraha9144
    @abhijeetsaraha9144 8 років тому +3

    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.

  • @PopaTiberiuAdrian
    @PopaTiberiuAdrian 8 років тому +2

    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!

    • @amellperalta
      @amellperalta  8 років тому +2

      I'm glad to know that. You're welcome!

  • @kevinmakesvideos
    @kevinmakesvideos 8 років тому +33

    thank you sir. So helpful. My professor sucks at explaining this

  • @Weirder_the_better
    @Weirder_the_better 6 років тому +4

    This was exactly what I needed. Thanks a lot for taking the time to explain the concepts. It finally clicked and all makes sense.

  • @zeg0noidpils26
    @zeg0noidpils26 7 років тому +4

    had to learn this at a non-presence univerity from home - your videos help a lot! thanks :)

    • @amellperalta
      @amellperalta  7 років тому +2

      You're welcome. I'm happy to know that. Happy coding!

  • @GauravKumar-wl6fc
    @GauravKumar-wl6fc 2 роки тому +1

    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

  • @dr_nyt4041
    @dr_nyt4041 4 роки тому +15

    This is still helpful in 2020, thanks a lot

    • @amellperalta
      @amellperalta  4 роки тому +2

      You're welcome. Happy coding!

    • @jesselam5867
      @jesselam5867 3 роки тому

      Love the profile sis

    • @dr_nyt4041
      @dr_nyt4041 3 роки тому +1

      @@jesselam5867 more of a bro, but coding gang les go

  • @HoaxGift
    @HoaxGift 6 років тому +1

    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.

    • @amellperalta
      @amellperalta  6 років тому +1

      You're always welcome. I'm glad to know that my videos helped you in understanding. Happy coding!

  • @sob5520
    @sob5520 6 років тому +4

    great explanation when you revise the code at the end. love the enthusiasm my guy :)

  • @mahadhassan3212
    @mahadhassan3212 6 років тому +50

    V O I L A !
    O
    I
    L
    A
    !

  • @bossman4112
    @bossman4112 3 роки тому +1

    Thank You for these videos, they really help a lot with my coding assignments in MIPS

  • @thisaintmyrealname1
    @thisaintmyrealname1 6 років тому

    Man your videos are excellent, and your enthusiasm is contagious! Thanks for this material.

  • @nguyenminhtam2302
    @nguyenminhtam2302 6 років тому +1

    I really love your toturial, very clear and easy to learn.
    Thank you very much!

    • @amellperalta
      @amellperalta  6 років тому

      I'm glad to know that. You're welcome!

  • @malehakim248
    @malehakim248 7 років тому +1

    Man, yo series is kickass....Damn professor...i wish we had such 10-min sessions than 2 45min sessions

  • @israelruiz8243
    @israelruiz8243 9 років тому +4

    Wow great explanation. Was really struggling with the stack concept. Thanks a lot.

    • @amellperalta
      @amellperalta  9 років тому

      +israel r8 You're welcome, Israel. Happy programming!

  • @BrianFaure1
    @BrianFaure1 8 років тому +6

    Great videos dude I wish i had seen this channel while i was taking computer architecture tho

  • @bmk2561
    @bmk2561 4 місяці тому +1

    this is more clutch than lebron's block on iguadala in 2016 thankyou i got exam in a couple days

  • @Clarin3t1
    @Clarin3t1 2 роки тому

    Thank you for explaining so clearly! These videos really help me!

  • @kwantom1996
    @kwantom1996 9 років тому +7

    Great tutorials :)
    I really appreciate it.

    • @amellperalta
      @amellperalta  9 років тому

      +kwantom1996 You're welcome. I'm glad you found this helpful. Happy coding!

  • @stormfang5941
    @stormfang5941 5 років тому

    This is helping me in my Assembly class so much thank you thank you thank you!!!

  • @yumao4348
    @yumao4348 4 роки тому +1

    thank u, Amell, these videos help me a lot !!!

  • @georgell57gt9
    @georgell57gt9 2 роки тому +1

    This video is underrated

  • @user-me9ru8qh6e
    @user-me9ru8qh6e 5 років тому +1

    Amel you are such an awesome person!

    • @amellperalta
      @amellperalta  5 років тому +1

      Thank you, Austin! Happy coding!

  • @pagoneelswitch8662
    @pagoneelswitch8662 2 роки тому +1

    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!!

    • @amellperalta
      @amellperalta  2 роки тому +1

      Thank you for letting me know. Happy coding!

  • @xavierlinn5013
    @xavierlinn5013 7 років тому +3

    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?

  • @Arshinred
    @Arshinred 7 місяців тому

    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?

  • @narvik7587
    @narvik7587 3 роки тому +2

    you are better than my professor...

  • @bipulkumar6637
    @bipulkumar6637 3 роки тому +1

    I would have failed but for these videos thanks Amell!

  • @Taseen95
    @Taseen95 4 роки тому +1

    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!

  • @letho8323
    @letho8323 2 роки тому +2

    correction: t registers are CALLER saved while s registers are CALLEE saved

  • @embomb701
    @embomb701 5 років тому +1

    Thanks Kermit, this really helps with my homework.

  • @engr.aftabufaq8633
    @engr.aftabufaq8633 6 років тому +1

    Thats good.. it help me alot in my studies....

    • @amellperalta
      @amellperalta  6 років тому +1

      I'm glad to know that, Engr. Aftab. Happy coding!

  • @SamuelTerra22
    @SamuelTerra22 5 років тому +4

    What a man

  • @cornelkenef3596
    @cornelkenef3596 9 років тому +1

    So this is like passing by value in C right?

  • @mygardencat7755
    @mygardencat7755 6 років тому +2

    Restoring the space in the stack is just like free allocated memory in the stack.

  • @medilyesoudhini7411
    @medilyesoudhini7411 6 років тому +1

    thank you very much my sir you are the best it's very helpful

  • @hei4227
    @hei4227 3 роки тому +1

    Great video thnx!

  • @AmCanTech
    @AmCanTech 2 роки тому

    so if i use temp registers, i don't need sp and use dynamic memory?

  • @leauxmeli1422
    @leauxmeli1422 4 роки тому

    Anyway I could get in contact with you? I have some questions about this video, thank you.

  • @carlasanchez4349
    @carlasanchez4349 2 роки тому +1

    Thank you for this 👍

  • @sollekram
    @sollekram 6 років тому

    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?

  • @y.x3476
    @y.x3476 2 роки тому

    Thanks you are the best!

  • @Spyro-kt8gy
    @Spyro-kt8gy 4 місяці тому +1

    Remarkable work.

  • @sophiedaif7585
    @sophiedaif7585 7 років тому +1

    a really huge thanks to you :) so usefull videos, grazie mille!

    • @amellperalta
      @amellperalta  7 років тому

      Prego, Sophie! I'm very glad you find my videos useful. Happy coding!

  • @Krashevil
    @Krashevil 9 років тому +3

    my man.
    if i were to see you in person i would give you a dap

    • @amellperalta
      @amellperalta  9 років тому +1

      +Krashevil Yeah... We gotta be cool!

  • @GangnamStyle60
    @GangnamStyle60 6 років тому

    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 ?

  • @prateekmodi5207
    @prateekmodi5207 7 років тому +1

    Please tell me how to save two Doubles to a Stack ?

  • @bratergames9410
    @bratergames9410 5 років тому +1

    CREATE Amazing, thanks my friend.

  • @corentink3887
    @corentink3887 5 років тому

    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)

  • @tamaracousineau2329
    @tamaracousineau2329 4 роки тому

    Can you make a tutorial on how to draw a square on the bitmap tool?

  • @mohamedabi6415
    @mohamedabi6415 3 роки тому

    i didn t understand what did you meen by colly safe !

  • @ahmetkarakartal9563
    @ahmetkarakartal9563 2 роки тому +1

    wow, thank you so much

  • @aaron___6014
    @aaron___6014 7 років тому

    Will saving and pulling from the stack greatly increase the run time of a program? would it be faster to use an array?

  • @vishalmahavar
    @vishalmahavar 3 роки тому

    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?

  • @Spartanoja
    @Spartanoja 9 років тому

    Thank you but for create one array in the stack?

  • @nithinkumarhasswag
    @nithinkumarhasswag 4 роки тому +1

    I’m having trouble understanding your accent when he says t registers are “collee” saved. What is he trying to say here? Thanks

    • @RodrigoMorante
      @RodrigoMorante 4 роки тому

      He means "that which is called." The caller calls the callee.

  • @bambusleitung4422
    @bambusleitung4422 3 роки тому

    Few years later and youre still saving cs first semesters

  • @lovejava7729
    @lovejava7729 8 років тому

    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.

    • @harshshah1502
      @harshshah1502 7 років тому

      To restore the stack pointer to its original place

  • @sollekram
    @sollekram 6 років тому

    can you also implement the newLine in the function right;

  • @Arghandevol
    @Arghandevol 7 років тому

    Did you saved your "old value" in stack pointer cause you wont to "destroy the value"? Thank you again for videos @6.36

    • @amellperalta
      @amellperalta  7 років тому +2

      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.

    • @Arghandevol
      @Arghandevol 7 років тому

      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.

    • @amellperalta
      @amellperalta  7 років тому +2

      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.

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 9 років тому +1

    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?

    • @amellperalta
      @amellperalta  9 років тому +3

      +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.

    • @ThuyNguyen-bu9ge
      @ThuyNguyen-bu9ge 9 років тому

      +Quasar Distant Thank you! I understand it better now.

    • @amellperalta
      @amellperalta  9 років тому

      +Tiffastic Nguyen You're welcome. Happy programming, and keep in touch! ;)

  • @roomian
    @roomian 4 роки тому

    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.

    • @roomian
      @roomian 4 роки тому

      stacks in c++ like single or double linked list can do the problem as well so i though maybe the same concept applies here?

  • @qaipak1
    @qaipak1 8 років тому

    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?

    • @govi218mu
      @govi218mu 8 років тому

      +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 :)

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 9 років тому

    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?

    • @albusv
      @albusv 9 років тому

      +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."

  • @josepedrodeferreira2655
    @josepedrodeferreira2655 8 років тому +2

    why do you allways use addi $register,$zero,number
    instead of li $register,number?

    • @amellperalta
      @amellperalta  8 років тому +4

      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.

    • @gcantoni
      @gcantoni 6 років тому

      using li $register, number is better than addi instruction. Try using addi with a big ( like 10mln ) number.

  • @aarb17
    @aarb17 10 років тому

    Thank you for the videos

    • @amellperalta
      @amellperalta  10 років тому +1

      You're welcome. I will make more and better videos soon. Get ready for recursion...

  • @neettim
    @neettim 8 років тому

    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?

    • @amellperalta
      @amellperalta  8 років тому +1

      +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.

    • @neettim
      @neettim 8 років тому

      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!

  • @primemaniadrive
    @primemaniadrive 3 роки тому

    i don't know how to get a char from a given string
    i have to make lowercase chars uppercase

  • @gulammujtabahashmi6045
    @gulammujtabahashmi6045 9 років тому

    how many registers on MIPs simulator MARS (not counting coprocessor registers), what are those?

    • @theglasspinataincident7405
      @theglasspinataincident7405 9 років тому

      +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.

  • @supersadogoat
    @supersadogoat 9 років тому

    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

    • @amellperalta
      @amellperalta  9 років тому

      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

    • @supersadogoat
      @supersadogoat 9 років тому

      wow! thanks! this thing was giving me headaches.... :)

    • @amellperalta
      @amellperalta  9 років тому

      You're welcome!

    • @supersadogoat
      @supersadogoat 9 років тому

      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

  • @abebe7017
    @abebe7017 5 років тому +1

    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!!!

  • @Gameoverash
    @Gameoverash 9 років тому

    Hey could you please help me with one of my project based on this. I am new to assembly language
    Thanks

    • @amellperalta
      @amellperalta  9 років тому +1

      I'm very busy with school and work right now. So I won't be able to help you at the moment.

  • @SuperCacazinho
    @SuperCacazinho 3 роки тому

    Eu te amo!

  • @chel-bell
    @chel-bell 5 років тому +1

    I LOVE AMELL

  • @dolevdo
    @dolevdo 2 роки тому +1

    thank you

  • @wavyxox
    @wavyxox 10 місяців тому +1

    thanks from 2024 Amell!

  • @elco7956
    @elco7956 7 місяців тому

    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

  • @adamcoleman6541
    @adamcoleman6541 6 років тому

    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.

  • @danielgannage8109
    @danielgannage8109 6 років тому

    Fucking love you brother. Good shit.

    • @danielgannage8109
      @danielgannage8109 6 років тому +1

      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!

    • @amellperalta
      @amellperalta  6 років тому

      You're welcome. Happy coding, Daniel!

  • @yaya-ji7nu
    @yaya-ji7nu 2 роки тому

    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

  • @catorlife
    @catorlife 2 місяці тому

    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

  • @ahmedhadiji4566
    @ahmedhadiji4566 4 роки тому

    KING

  • @ahmedramybouguettoucha8775
    @ahmedramybouguettoucha8775 3 роки тому +2

    2021 :D

  • @deepakjain4481
    @deepakjain4481 Місяць тому +1

    you sound like muhhamad ali

  • @zeyaddessouki890
    @zeyaddessouki890 5 років тому

    DOLLAR SIGNNN :D

  • @ismailboudra-d5w
    @ismailboudra-d5w 8 місяців тому +2

    2024

  • @shivanshukumar9862
    @shivanshukumar9862 Рік тому +1

    Sadly zero understanding in this video

  • @adarshkv4616
    @adarshkv4616 4 роки тому

    .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

  • @conbil16
    @conbil16 3 роки тому +2

    Amell > College professor