Knowee
Questions
Features
Study Tools

Why must registers be backed up at the beginning of a subroutine, and restored before returning?Question 5Select one:a.Conventions dictate that all registers are moved onto the stack.b.Registers might spontaneously change value, therefore backing them up is advisable to prevent against data loss.c.A subroutine expects parameters to be stored on the stack.d.Saving/restoring registers creates the illusion that each subroutine has its own independent set of registers, despite the CPU only having a single set that's shared across the entire program.

Question

Why must registers be backed up at the beginning of a subroutine, and restored before returning?Question 5Select one:a.Conventions dictate that all registers are moved onto the stack.b.Registers might spontaneously change value, therefore backing them up is advisable to prevent against data loss.c.A subroutine expects parameters to be stored on the stack.d.Saving/restoring registers creates the illusion that each subroutine has its own independent set of registers, despite the CPU only having a single set that's shared across the entire program.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is d. Saving/restoring registers creates the illusion that each subroutine has its own independent set of registers, despite the CPU only having a single set that's shared across the entire program.

Here's why:

When a subroutine is called, it may need to use some of the CPU's registers to perform its task. However, the calling code may also be using those registers. If the subroutine were to change the values in the registers without saving them first, then when control returns to the calling code, it would find that the register values have been altered, which could cause problems.

By saving the necessary registers at the start of the subroutine (usually by pushing their values onto the stack), the subroutine can then use those registers freely. Before the subroutine finishes, it restores the original values (usually by popping them off the stack). This way, when control returns to the calling code, it's as if the subroutine never touched the registers.

This mechanism allows each subroutine to behave as if it has its own private set of registers, which simplifies the programming model. In reality, the CPU has a single set of registers that are shared across all code.

This problem has been solved

Similar Questions

Could subroutines use a dedicated section of memory (allocated in .data or .bss) for backing up and restoring register values, instead of using the stack?Question 1Select one:a.Not possible.b.Yes, but it is impossible to use this method and also use the stack to pass parameters to any functions called.c.Yes, this would work fine but it requires more work.d.Yes, but this would only work for a small number of registers because .data and .bss are smaller than the stack segment.

What do WRAMP subroutines use the system stack for?Question 14Select one:a.Saving/restoring register values, passing parameters to other subroutines, and storing all local variables.b.Passing parameters to other subroutines, saving/restoring registers, and returning values to the caller.c.Saving/restoring register values, passing parameters to other subroutines, and storing any local variables that don't fit into registers.d.A general area of memory for storing data.

Which of the following statements is false?Question 2Select one:a.The register $ra does not need to be backed up and restored if the subroutine does not call any other subroutine.b.A subroutine does not need to back up and restore the register $1 in any circumstances.c.The CPU will modify the value stored in $ra when "jal" is executed.d.Each subroutine has its own independent set of registers, much like local variables in C or Java.

In what situations must $1 remain unchanged by a subroutine?Question 13Select one:a.Only if the subroutine does not have a return value.b.Only if the subroutine calls other subroutines.c.Always. Conventions dictate that all registers are not modified by a subroutine.d.Never. Any subroutine is free to modify $1.

In terms of the physical hardware, what is special about register $1?Question 15Select one:a.It is automatically saved by the CPU when the "jal" instruction is executed.b.It is automatically set to the return value when "jr $ra" is executedc.Nothing. Subroutines use it as the return value purely by convention.d.It is write-only.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.