Question

In: Computer Science

Task 3      Write a program that pushes the first 10 natural numbers to the stack,...

Task 3

     Write a program that pushes the first 10 natural numbers to the stack, then pops those numbers. Use the debugger to view how the stack changes after each instruction. Once all the data has been pushed to the stack, take a screenshot of the stack in memory.

Task 4

    Write a subroutine called “Area” that calculates the area of a rectangle. Use accumulator A and B to pass the length and the width to the function. Use accumulator D to return the result.

HINT:

  • load the width and height into accumulators A and B.
  • Call the subroutine.
  • Provide a flowchart, a snapshot of CPU registers before and after executing the subroutine.

    Now re-write the program from Task 3, but this time use the stack to pass the function arguments and the return value.

HINT:

  • load the width into accumulator A, then push A to the stack
  • load the height into accumulator B, then push B to the stack
  • Allocate one word on the stack for the return value.
  • Call the subroutine.
  • After you return from the function, deallocate all variables from the stack.
  • Provide a drawing of the stack frame and a snapshot of of the stack before, during and after executing the subroutine.

Write in Assembly for HCS12

Solutions

Expert Solution

#include <bits/stdc++.h> 
using namespace std; 
  
// A structure to represent a stack  
class Stack  
{  
    public: 
    int top;  
    unsigned capacity;  
    char* array;  
};  
  
// function to create a stack of given  
// capacity. It initializes size of stack as 0  
Stack* createStack(unsigned capacity)  
{  
    Stack* stack = new Stack(); 
    stack->capacity = capacity;  
    stack->top = -1;  
    stack->array = new char[(stack->capacity * sizeof(char))];  
    return stack;  
}  
  
// Stack is full when top is equal to the last index  
int isFull(Stack* stack)  
{ return stack->top == stack->capacity - 1; }  
  
// Stack is empty when top is equal to -1  
int isEmpty(Stack* stack)  
{ return stack->top == -1; }  
  
// Function to add an item to stack.  
// It increases top by 1  
void push(Stack* stack, char item)  
{  
    if (isFull(stack))  
        return;  
    stack->array[++stack->top] = item;  
}  
  
// Function to remove an item from stack.  
// It decreases top by 1  
char pop(Stack* stack)  
{  
    if (isEmpty(stack))  
        return -1;  
    return stack->array[stack->top--];  
}  
  
// A stack based function to reverse a string  
void reverse(char str[])  
{  
    // Create a stack of capacity  
    //equal to length of string  
    int n = strlen(str);  
    Stack* stack = createStack(n);  
  
    // Push all characters of string to stack  
    int i;  
    for (i = 0; i < n; i++)  
        push(stack, str[i]);  
  
    // Pop all characters of string and  
    // put them back to str  
    for (i = 0; i < n; i++)  
        str[i] = pop(stack);  
}  
  
// Driver code  
int main()  
{  
    char str[] = "GeeksQuiz";  
  
    reverse(str);  
    cout << "Reversed string is " << str;  
  
    return 0;  
} 

Related Solutions

Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the...
Question1; Write a c++ program that prints all natural numbers between 10(included) to 100(included) by the while loop. question2: Write a C++ program that prints all numbers between 10 to 1000 that are divisible by 5
Write a testing program (not sort.c from task 2) that contains a stack buffer overflow vulnerability....
Write a testing program (not sort.c from task 2) that contains a stack buffer overflow vulnerability. Show what the stack layout looks like and explain how to exploit it. In particular, please include in your diagram: (1) The order of parameters (if applicable), return address, saved registers (if applicable), and local variable(s), (2) their sizes in bytes, (3) size of the overflowing buffer to reach return address, and (4) the overflow direction in the stack (5) What locations within the...
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Write a C++ program that prompt the user to enter 10 numbers andstores the numbers...
Write a C++ program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
Task 1 Write a program that adds the three numbers stored in data registers at 0x20,...
Task 1 Write a program that adds the three numbers stored in data registers at 0x20, 0x30, and 0x40 and places the sum in data register at 0x50 task 4 Modify the program in Task1, so the program will run in infinite loop by using these following functions: GOTO function BRA function CALL function Simulate your program in PIC18 IDE Simulator and attach a screenshot of your simulation while the program is running.
Write Visual Basic program that seperates all natural numbers up to 863 value with a comma...
Write Visual Basic program that seperates all natural numbers up to 863 value with a comma side by side and show on the screen Please write readable.Thankss
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT