Question

Write a function that implements another stack function, peek. Peek returns the value of the first...

Write a function that implements another stack function, peek. Peek returns the value of the first element on the stack without removing the element from the stack. Peek should also do underflow error checking. (Why is overflow error checking unnecessary?)

IN LC-3 ASSEMBLY

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

GIVEN THAT:

THE IMPLEMENTS ANOTHER STACK FUNCTION ,PEEK RETURNS THE VALUE :

USING LC-3 :

1. MEMORY ADDRESSES INCREMENT FROM TOP TO BOTTOM AS SHOWN BELOW:

X3FF0

X3FF1

X3FF2

X3FF3

X3FF4

2. In order to allocate the Memory address in LC3 we use .BLKW instruction .

3. Let us say we have allocated 5 address spaces to the stack

4. Stack .BLKW 5           ; as shown in the above addresses

Let us say we use R0 to store pushed and popped element

                              R5 to store the top of the stack address . In the above case R5 has X3FF0

Now look at the POP function

POP    ST R2, Save2 ;     save, needed for every pop operation

            ST R1, Save1 ;      save, needed for every pop operation

           LD R1, nBASE ;     nBASE contains - X3FF4   which is useful to check if the stack is empty

             ADD R2, R5, R1 ; compare Stack top to stack base if they are same exit stack is empty

            BRz fail_exit ;      branching statement stack is empty

            LDR R0, R5, #0 ;   Store the R5 value to R0

            ADD R5, R5, #1 ; Now increment R5 then top will be next element (element is popped to R0)

            BRnzp success_exit Branching statement for success.

In the same way above for seek operation we don’t need to increment the top of the stack address

It is enough to store the element in R5 to R0, as shown below.

5. SEEK ST R2, Save2 ;     save, needed for every seek operation

            ST R1, Save1 ;      save, needed for every seek operation

LD R1, nBASE ;     nBASE contains - X3FF4   which is useful to check if the stack is empty

ADD R2, R5, R1 ; compare Stack top to stack base if they are same exit stack is empty

BRz fail_exit ;      branching statement stack is empty

LDR R0, R5, #0 ;   Store the R5 value to R0

BRnzp success_exit Branching statement for success.

WE JUST NEED TO REMOVEING THE ADD R5,R5,#1

Add a comment
Know the answer?
Add Answer to:
Write a function that implements another stack function, peek. Peek returns the value of the first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • using java //HINT: Each function can be completed using a single statement. public class LinkListStack<T> implements...

    using java //HINT: Each function can be completed using a single statement. public class LinkListStack<T> implements StackADT<T> Unordered LinkedList<T> stack = new UnorderedLinked List: public void push(T element) //TODO-Write Code Here } public T popo //TODO - Write Code Here 3 public T peek) [ //TODO - Write Code Here ] public boolean isEmpty //TODO - Write Code Here 3 public int size) I/TODO - Write Code Here

  • You have to use a h file for class definitions and two (as specified below) cpp...

    You have to use a h file for class definitions and two (as specified below) cpp file for the C++ implementation. You have to generate the list from scratch and check for overflow I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. Please submit enough screenshots showing compiling and execution under Linux. Write a main program...

  • Call stack question! Long one... The call stack is part of main memory that is reserved...

    Call stack question! Long one... The call stack is part of main memory that is reserved for function calling. Like all r memory it is finite, so can be exhausted resulting in a stack overflow. Recursive functions allocate space on the stack for each recursive call: if there are many such recursive calls a stack overflow can result. The questions that follow ask you to investigate recursive functions and stack overflows. Note that when running programs in the Linux terminal...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack)...

    **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """...

  • Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and...

    Problem 1: Write a function add64 that adds two input unsigned 64 bit integers x and y and returns the unsigned 64 bit integer sum z, i.e. zx+y. In your main function, you should assume that x, y, and z will be stored in the following 6 registers as follows: x: upper 32 bits in $t1 y: upper 32 bits in St3 z: upper 32 bits in St5 lower 32 bits in $to lower 32 bits in $t2 lower 32...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT