Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 4.1, Problem 4.4PP
Explanation of Solution
Given C code:
long rsum(long *start, long count)
{
if(count <= 0)
return 0;
return *start + rsum(start+1, count-1);
}
Data movement instructions:
- The different instructions are been grouped as “instruction classes”.
- The instructions in a class performs same operation but with different sizes of operand.
- The “Mov” class denotes data movement instructions that copy data from a source location to a destination.
- The class has 4 instructions that includes:
- movb:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 1 byte data size.
- movw:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 2 bytes data size.
- movl:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 4 bytes data size.
- movq:
- It copies data from a source location to a destination.
- It denotes an instruction that operates on 8 bytes data size.
- movb:
Corresponding x86-64 code:
long rsum(long *start, long count)
start in %rdi, count in %rsi
rsum:
movl $0, %eax
testq %rsi, %rsi
jle .L9
pushq %rbx
movq (%rdi),%rbx
subq $1, %rsi
addq $8, %rdi
call rsum
addq %rbx, %rax
popq %rbx
.L9:
rep; ret
Explanation:
- The instruction “movl $0, %eax” initializes value in register “%eax” to 0.
- The instruction “testq %rsi, %rsi” checks for count to be zero.
- The instruction “jle .L9” jumps to label “.L9” if first value is less than or equal to second.
- The instruction “pushq %rbx” saves callee-saved register.
- The instruction “movq (%rdi),%rbx” moves value at location of register “%rdi” to register “%rbx”.
- The instruction “subq $1, %rsi” decrements value of “count” stored in register “%rsi”...
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Q3#A /list two ways to rewrite the following code with Code Optimization Methods (Redundant instruction
elimination)?
Int add ten(int X)
{
Int Y, Z,
Y= 10;
Z=X+Y;
Return z; }
Answer the given question with a proper explanation and step-by-step solution.
C++ 11.12 LAB: Fibonacci sequence (recursion)
The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the Fibonacci() function, which takes in an index, n, and returns the nth value in the sequence. Any negative index values should return -1.
Ex: If the input is:
7
the output is:
Fibonacci(7) is 13
Note: Use recursion and DO NOT use any loops.
main.cpp
#include <iostream>using namespace std;
int Fibonacci(int n) {/* Type your code here. */ }
int main() {int startNum;cin >> startNum;cout << "Fibonacci(" << startNum << ") is " << Fibonacci(startNum) << endl;return 0;}
#include
using namespace std;
int main()
{
int type;
double Vth, Vg, Vd, Vs;
cin>>type>>Vth>>Vg>>Vd>>Vs;
return 0;
}
Chapter 4 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Ch. 4.1 - Prob. 4.1PPCh. 4.1 - Prob. 4.2PPCh. 4.1 - Prob. 4.3PPCh. 4.1 - Prob. 4.4PPCh. 4.1 - Prob. 4.5PPCh. 4.1 - Prob. 4.6PPCh. 4.1 - Prob. 4.7PPCh. 4.1 - Prob. 4.8PPCh. 4.2 - Practice Problem 4.9 (solution page 484) Write an...Ch. 4.2 - Prob. 4.10PP
Ch. 4.2 - Prob. 4.11PPCh. 4.2 - Prob. 4.12PPCh. 4.3 - Prob. 4.13PPCh. 4.3 - Prob. 4.14PPCh. 4.3 - Prob. 4.15PPCh. 4.3 - Prob. 4.16PPCh. 4.3 - Prob. 4.17PPCh. 4.3 - Prob. 4.18PPCh. 4.3 - Prob. 4.19PPCh. 4.3 - Prob. 4.20PPCh. 4.3 - Prob. 4.21PPCh. 4.3 - Prob. 4.22PPCh. 4.3 - Prob. 4.23PPCh. 4.3 - Prob. 4.24PPCh. 4.3 - Prob. 4.25PPCh. 4.3 - Prob. 4.26PPCh. 4.3 - Prob. 4.27PPCh. 4.4 - Prob. 4.28PPCh. 4.4 - Prob. 4.29PPCh. 4.5 - Prob. 4.30PPCh. 4.5 - Prob. 4.31PPCh. 4.5 - Prob. 4.32PPCh. 4.5 - Prob. 4.33PPCh. 4.5 - Prob. 4.34PPCh. 4.5 - Prob. 4.35PPCh. 4.5 - Prob. 4.36PPCh. 4.5 - Prob. 4.37PPCh. 4.5 - Prob. 4.38PPCh. 4.5 - Prob. 4.39PPCh. 4.5 - Prob. 4.40PPCh. 4.5 - Prob. 4.41PPCh. 4.5 - Prob. 4.42PPCh. 4.5 - Prob. 4.43PPCh. 4.5 - Prob. 4.44PPCh. 4 - Prob. 4.45HWCh. 4 - Prob. 4.46HWCh. 4 - Prob. 4.47HWCh. 4 - Prob. 4.48HWCh. 4 - Modify the code you wrote for Problem 4.47 to...Ch. 4 - In Section 3.6.8, we saw that a common way to...Ch. 4 - Prob. 4.51HWCh. 4 - The file seq-full.hcl contains the HCL description...Ch. 4 - Prob. 4.53HWCh. 4 - The file pie=full. hcl contains a copy of the PIPE...Ch. 4 - Prob. 4.55HWCh. 4 - Prob. 4.56HWCh. 4 - Prob. 4.57HWCh. 4 - Our pipelined design is a bit unrealistic in that...Ch. 4 - Prob. 4.59HW
Knowledge Booster
Similar questions
- #include using namespace std; int main() { int type; double Vth, Vg, Vd, Vs; cin>>type>>Vth>>Vg>>Vd>>Vs; return 0; }arrow_forwardLanguage: C Write a C program that allocates memory using the malloc function for an array of size specified by the user at runtime. Assign pseudo-random double values to the array elements, then sort the array using the qsort function. Using the examples from the lecture, define a function that compares numbers of type double. Pass the function address to the qsort function. Use the free function to free up memory. Problem 1 and Problem 2.arrow_forward▸ A is an array of 100 words and variables g and hare registers x20 and x21. ▸ Assume the starting (base) address of A is in x22. Use x9 for the temporary register. I > Compile the C statement. The number 8 is also known as the offset. Offset to be added is 32 as each word is 4 bytes. g= h + A[8]: Load word 1w x5, 40(x6) x5- Memory[x6 + 40] Word from memory to registerarrow_forward
- By C++arrow_forwardProblem (5) Consider the code in main() below. Write code (in C++) to close as many memory leaks as possible. If there is still some memory leaking, describe why you cannot take care of it. int** pp; int* p1, p2; int x = 2; p2=new int; pp=new int*; *pp = p2; = new int; %3D p2 **pp = p1 *p2; *pp; p2 = new int;arrow_forwardSystems Programming Purpose: The purpose of this assignment is to practice loop instructions and array iterations in M6800 assembly language programming. Write an assembly language program which implements the following C code that is a find minimum problem solution implementation: int A[10] = {8, 14, 6, 16, 5, 5, 10, 9, 4, 11}; int i = 0; int min = 255; // This is the largest number 8-bit accumulators can hold. while (i < 10) { if (A[i] < min) min = A[i]; } Your solution should be able to handle all possible array elements and orderings for any value an accumulator can hold. You should treat variable į as XR in assembler (index register) and store variable min in the address 60H. 0010: 00 00 00 00 00 00 00 00 0O 00 00 00 00 00 00 00 8628: 00 060 00 00 00 600 00 00 00 08 08 00 6 00 00 00 0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0050: 00 G0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 006 0: 00 00 00 00 00 00 00 00 00 08 00…arrow_forward
- i need the answer quicklyarrow_forwardQuestion 2 Using the incomplete programming code given, complete the code using dynamic programming with memory function, to reproduce the results in the following Table 1. (C++) #include<iostream>using namespace std; // max knapsack capacity // *** WRITE YOUR CODE HERE ***// num of items // *** WRITE YOUR CODE HERE ***// weight of each item // *** WRITE YOUR CODE HERE ***// value of each item // *** WRITE YOUR CODE HERE ***// variable for dynamic programming matrix // *** WRITE YOUR CODE HERE *** //==========================================// Dynamic programming function: recursive// ========================================= // ALGORITHM F(i,j) // int value // if F[i,j] is not filled yet (-1): // (start with j = W, i = n) // if capacity j < current item's weight w[i]: // value = recall F(i-1, j) // else: // we can include current item,…arrow_forwardhi need help code this in c plus plus langue a give a thumbs uparrow_forward
- i need ans very very fast in 20 min and thank you | ᴅʏʙᴀʟᴀ ?✨ | microprocessor 8085؛arrow_forward4(c): Design an "unconventional" priority encoder as follows: It has 3 inputs D0, D1 and D2 and outputs A1, A0 and Valid. The input combination [D1,D0] = [11] is guaranteed to never happen, that is, both D1 and DO cannot be 1 at the same time. If D2 is 1, then the output code [A1A0]=[10]. Else it is either [01] or [00] depending on which of D1 or DO is logic 1. If any input is 1, Valid=1, else Valid=0. Give expressions for A1, A0 and Valid in terms of D0, D1 and D2. A1 = AO = Valid = You may DI Do yoo/01 use any lo 00 101 method you know to get your answer. Ai Ao Please draw any k-maps D2 00 01 10 used for partial credit (see below). It must be Valid. clear how you got your answer, i.e. show all "rough" work.arrow_forwardQuestion:- 1 Recreate the following C program in x86-64 and aarch64 assembly manually: #include <stdio.h> typedef struct _v2d {long x;long y;} v2d_t; static long dot(v2d_t a, v2d_t b){return a.x * b.x + a.y * b.y;} static long dot_ptr(v2d_t *a, v2d_t *b){return a->x * b->x + a->y * b->y;} int main(void){v2d_t a, b; scanf("%ld %ld %ld %ld", &a.x, &a.y, &b.x, &b.y); long res1 = dot(a, b);printf("%ld\n", res1); long res2 = dot_ptr(&a, &b);printf("%ld\n", res2); return 0;}arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr