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
  • (Data Structure) Using only emptyCheck(), peek(), push(a), and pop() operations, write a function Middle(Stack S) that...

    (Data Structure) Using only emptyCheck(), peek(), push(a), and pop() operations, write a function Middle(Stack S) that returns the value from stack S that is in the middle, (i.e., location [size / 2]). You may create additional stacks as needed, but S must be restored by the end of the routine.

  • C++ Please Write a function to copy one stack to another stack and leave the first...

    C++ Please Write a function to copy one stack to another stack and leave the first stack unchanged. Only use the stack member functions: push pop top empty Your program should: create a stack prompt the user for a list of names and add them to the stack create a second stack call a function to copy the contents of the first stack to the second stack (leaving the first stack unchanged) empty the contents of the first stack (displaying...

  • ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and...

    ) Consider Java's Stack class, and its five standard stack operations: push, pop, peek, isEmpty, and clear. Complete the two unfinished methods. Do not modify any other parts of the class.               // Looks at the top two elements of the stack, and removes and returns the larger        // of the two elements from the stack, returning the other element to the stack.        // For example, if the stack, from the top, is 8 10 7 2...

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

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

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

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

  • **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