Write an LEGv8 that finds weight and potential energy associated with a body fal
ID: 3816488 • Letter: W
Question
Write an LEGv8 that finds weight and potential energy associated with a body falling from a height (h) and mass (m). Assume g = 9.8 m/s2 and is stored in the memory. The address of the memory location is stored in X19. The input and output values are single precision floating-point values. Also, assume the input mass (m) is in S0, height (h) is in S1. Store the computed weight in S2 and computed potential energy in S3. Please comment the assembly code. Here is the C program:
#include <stdio.h>
int main()
{
const float gravity = 9.8;
float pEnergy, height, mass, weight;
printf("Please enter the height in meters: ");
scanf("%f", &height); //allow the user to input the height
printf("Please enter the mass in kg: ");
scanf("%f", &mass); //allow the user to input the mass
pEnergy = height * mass * gravity; //find potential energy
weight = mass * gravity;
printf(" Weight: %.1f ", weight);
printf("Potential Energy: %.1f ", pEnergy);
return 0;
}
Explanation / Answer
Find the assembly code for the above problem statement
****************************
#include<stdio.h>
int main()
{
float pEnergy, height, mass, weight;
const float gravity = 9.8;
//perform main operations
printf("Please enter the height in meters: ");
printf("Please enter the mass in kg: ");
scanf("%f","%f",&height,&&mass);
__asm__ ( "imull %%ebx, %%eax;" : "=pEnergy" (mul) : "a" (height) , "b" (mass), "c"(gravity));
__asm__ ( "imull %%ebx, %%eax;" : "=weight" (mul) : "b" (mass), "c"(gravity));
printf("%f * %f = %f ",pEnergy);
printf(" Weight: %.1f ", weight);
}
****************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.