Computer Systems: A Programmer's Perspective (3rd Edition)
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
Expert Solution & Answer
Book Icon
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.

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

Blurred answer
Students 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)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr