Question

In this assignment, you are going to write a program that will calculate x*x by using shift operations. Details are as follow

Please solved IAR workbench in C language I have a limited time less than 10 hours

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

#include <stdio.h>

int main() {
   //code below is accompanied by appropriate comments to explain all the operations performed in detail

//variables required in the program
int x=0, realValue=0, tempValue=0, result=0;


//p is the pointer used to store the values at particular addresses as asked in the question
int *p = 0x20000008UL;
  
//x is used to store the number given as input by the user
printf("Please enter the value of x ( between 1 and 100)");
scanf("%d", &x);
  
//As x is to be processed, realValue stores the actual value of x
realValue=x;


//count1 is used to denote at which but we are currently at
int count1 = 0;


//count2 is used to keep a count of the ON bits in the input number
int count2 = 0;
  
//this loop is used to travel all the bits of x one by one
while(x>0){
if(x&1 == 1){
count2++;
//tempValue is used to store the value of realValue while the realValue is processed
tempValue = realValue;
realValue = realValue<<count1;
*p=realValue;
realValue = tempValue;
p++;
}
x = x>>1;
count1++;
}
  
//this loop is used to add the numbers which are stored by the above loop, in order to calculate the square of //the given number
p = 0x20000008UL;
while(count2>0){
result+=(*p);
p++;
count2--;
}
  
//final is the pointer towards the address in which the final number is stored
int *final = 0x20000000UL;
*final=result;
  
   //this statement is used to print the final result
   printf("The square of the number given as input is: %d", *final);
  
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
In this assignment, you are going to write a program that will calculate x*x by using shift opera...
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
  • Write a program using the LOOP instruction to sum all the even numbers from 20H to...

    Write a program using the LOOP instruction to sum all the even numbers from 20H to 80H. Write a program using the ADC (or add with carry) instruction to add these two 48 bit numbers. Assume the first number is store at an address starting 400H and that the second number is stored at an address starting at address 500H. Store the results in memory starting at address 600H. (Note that each number consists of three 16 bit words). Show...

  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • 1. (6 Marks) Using the single bus architecture provided below (as seen in class also), write the ...

    1. (6 Marks) Using the single bus architecture provided below (as seen in class also), write the sequence of micro-code instructions for doing the following tasks (as given by points a), b) and c)). You do not have to worry about timing in this question. For memory reads/writes, assume the operation finishes in 1 clock cyclıe (no timing issues). For each micro-code you provide, comment on what you are accomplishing The following table gives ALU functions operating on numbers in...

  • Please answer the list questions above with explanation. Thank you LOAD-STORE PROGRAM EXAMPLE Write an Assembly...

    Please answer the list questions above with explanation. Thank you LOAD-STORE PROGRAM EXAMPLE Write an Assembly program to add two 8-bit numbers. C A+B lds r16, A lds rl7, B : 1. Load variables add E16, :172 Do something 2. Do something sts C, r16 : 3. Store answer Identify the operation, source operand, destination operand in the first Data Transfer insiruction. Identify the source/destination operand in the Arithmetic and Logic (ALU) instruction. .What addressing mode is used by the...

  • Write a C++ program In this assignment you will complete the definition of two functions that...

    Write a C++ program In this assignment you will complete the definition of two functions that implement the repeated squaring algorithm described in the Stamp textbook on pages 98-99. Note that your implementation should not use the pow() function, the powl() functions, or any other built-in exponentiation functions. program4-driver.cpp - This file contains a completed main() function that serves as the driver to parse the command line, pass the values to the powerModN() function, and print the result. Make no...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • How do I write a C program called binary that takes a single command line argument,...

    How do I write a C program called binary that takes a single command line argument, and integer, in decimal, and prints out the binary representation of the number with 64 bits. The argument must be stored as a long. Use atol to convert the command line argument. Include a function char *binary(long n, char *b) { ... } that stores the binary representation in the string b with '0's and '1's. It is the responsibility of the calling program...

  • Consider a hypothetical computer with an instruction set of only two n-but instructions. The firs...

    Consider a hypothetical computer with an instruction set of only two n-but instructions. The first bit specifies the opcode, and the remaining bits specify one of the 2-1 n-bit words of main memory. The two instructions are as follows: SUBS X: Subtract the contents of location X from the accumulator, and store the result in location X and the accumulator JUMP X: Place address X in Program Counter A word in memory may contain either an instruction or a binary...

  • This assignment is designed to introduce you to using memory, input/output syscalls and if statements a....

    This assignment is designed to introduce you to using memory, input/output syscalls and if statements a. Open the text editor and type in the following. Then save your work as “assign3.asm”. Note that there are 3 words of memory indicated that start at the address 0x10010000. Do not use labels in the data segment. .data .word 0 .word 0 .word 0 .globl main .text main: b. Add the following zero terminated ascii strings (.asciiz) to the data segment right above...

  • Please help with program this. Thank you so much in advance! Create a Java program which...

    Please help with program this. Thank you so much in advance! Create a Java program which implements a simple stack machine. The machine has 6 instructions Push operand Puts a value on the stack. The operand is either a floating point literal or one of 10 memory locations designated MO M9 Pop operand Pops the value on the top of the stack and moves it to the memory location MO-M9 Add Pops the top two values off the stack, performs...

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