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

In addition to the processor stack, it may be convenient to use another stack in

ID: 3668127 • Letter: I

Question

In addition to the processor stack, it may be convenient to use another stack in some
programs. The second stack is usually allocated a fixed amount of space in the memory.
In this case, it is important to avoid pushing an item onto the stack when the stack has
reached its maximum size. Also, it is important to avoid attempting to pop an item off an
empty stack, which could result from a programming error. Write two short RISC-style
routines, called SAFEPUSH and SAFEPOP, for pushing onto and popping off this stack
structure, while guarding against these two possible errors. Assume that the element to be
pushed/popped is located in register R2, and that register R5 serves as the stack pointer for
this user stack. The stack is full if its topmost element is stored in location TOP, and it is
empty if the last element popped was stored in location BOTTOM. The routines should
branch to FULLERROR and EMPTYERROR, respectively, if errors occur. All elements
are of word size, and the stack grows toward lower-numbered address locations.

Please do the problem listed above except for CISC-style routines that can use Autoincrement and Autodecrement addressing modes.

Explanation / Answer

Move R2, #NUM1 Push parameters onto stack.
Subtract SP, SP, #4
Store R2, (SP)
Load R2, N
Subtract SP, SP, #4
Store R2, (SP)
Call LISTADD Call subroutine
(top of stack is at level 2).
Load R2, 4(SP) Get the result from the stack
Store R2, SUM and save it in SUM.
Add SP, SP, #8 Restore top of stack
(top of stack is at level 1).
LISTADD: Subtract SP, SP, #16 Save registers
Store R2, 12(SP)
Store R3, 8(SP)
Store R4, 4(SP)
Store R5, (SP) (top of stack is at level 3).
Load R2, 16(SP) Initialize counter to n.
Load R4, 20(SP) Initialize pointer to the list.
Clear R3 Initialize sum to 0.
LOOP: Load R5, (R4) Get the next number.
Add R3, R3, R5 Add this number to sum.
Add R4, R4, #4 Increment the pointer by 4.
Subtract R2, R2, #1 Decrement the counter.
Branch_if_[R2]>0 LOOP
Store R3, 20(SP) Put result in the stack.
Load R5, (SP) Restore registers.
Load R4, 4(SP)
Load R3, 8(SP)
Load R2, 12(SP)
Add SP, SP, #16 (top of stack is at level 2).
Return Return to calling program.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote