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
Question
Book Icon
Chapter 4, Problem 4.45HW

A.

Program Plan Intro

Given Assembly code:

subq $8, %rsp

movq REG, (%rsp)

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.

Unary and Binary Operations:

  • The details of unary operations includes:
    • The single operand functions as both source as well as destination.
    • It can either be a memory location or a register.
    • The instruction “incq” causes 8 byte element on stack top to be incremented.
    • The instruction “decq” causes 8 byte element on stack top to be decremented.
  • The details of binary operations includes:
    • The first operand denotes the source.
    • The second operand works as both source as well as destination.
    • The first operand can either be an immediate value, memory location or register.
    • The second operand can either be a register or a memory location.

Jump Instruction:

  • The “jump” instruction causes execution to switch to an entirely new position in program.
  • The “label” indicates jump destinations in assembly code.
  • The “je” instruction denotes “jump if equal” or “jump if zero”.
    • The comparison operation is performed.
    • If result of comparison is either equal or zero, then jump operation takes place.
  • The “ja” instruction denotes “jump if above”.
    • The comparison operation is performed.
    • If result of comparison is greater, then jump operation takes place.
  • The “pop” instruction resumes execution of jump instruction.
  • The “jmpq” instruction jumps to given address. It denotes a direct jump.

A.

Expert Solution
Check Mark

Explanation of Solution

Behavior of instruction pushq:

  • No, the given code sequence is not correctly describes the behavior of given instruction.
  • The instruction “subq $8, %rsp” subtracts 8 from value of “%rsp”.
  • If “REG” denotes “%rsp” then it computes “%rsp-8”.
  • The instruction “movq REG, (%rsp)” moves value of “REG” into location of “(%rsp)”.
  • The value “%rsp-8” is been pushed into stack instead of “%rsp”.
  • Hence, it does not describe behavior of instruction “pushq %rsp”.

B.

Program Plan Intro

Given Assembly code:

subq $8, %rsp

movq REG, (%rsp)

Processing stages:

  • The processing of an instruction has number of operations.
  • The operations are organized into particular sequence of stages.
  • It attempts to follow a uniform sequence for all instructions.
  • The description of stages are shown below:
    • Fetch:
      • It uses program counter “PC” as memory address to read instruction bytes from memory.
      • The 4-bit portions “icode” and “ifun” of specifier byte is extracted from instruction.
      • It fetches “valC” that denotes an 8-byte constant.
      • It computes “valP” that denotes value of “PC” plus length of fetched instruction.
    • Decode:
      • The register file is been read with two operands.
      • It gives values “valA” and “valB” for operands.
      • It reads registers with instruction fields “rA” and “rB”.
    • Execute:
      • In this stage the ALU either performs required operation or increments and decrements stack pointer.
      • The resulting value is termed as “valE”.
      • The condition codes are evaluated and destination register is updated based on condition.
      • It determines whether branch should be taken or not in a jump instruction.
    • Memory:
      • The data is been written to memory or read from memory in this stage.
      •  The value that is read is determined as “valM”.
    • Write back:
      • The results are been written to register file.
      • It can write up to two results.
    • PC update:
      • The program counter “PC” denotes memory address to read bytes of instruction from memory.
      • It is used to set next instruction’s address.

B.

Expert Solution
Check Mark

Explanation of Solution

Modified code:

movq REG, -8(%rsp)

subq $8, %rsp

Explanation:

  • The instruction “movq REG, -8(%rsp)” moves value of “REG” into location of “(%rsp)-8”.
  • The instruction “subq $8, %rsp” subtracts 8 from value of “%rsp”.
  • If “REG” denotes “%rsp” then it computes “%rsp”.
  • The value “%rsp” is now been pushed into stack.
  • Hence, it describes behavior of instruction “pushq %rsp”.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
In x86-64 assembly, how many registers do we have, and can we use registers like %rsp for anything we like? Group of answer choices A. We get 8664 general-purpose registers. Registers are like variables in assembly where we can store information. We can in theory use them for whatever we want, however we do need to follow conventions in order for our program to operate without errors (%rsp for example keeps track of the stack pointer).   B. We get unlimited general-purpose registers. Registers are like variables in assembly where we can store information. We can use them for whatever we want.   C. We get 16 general-purpose registers. Registers are like variables in assembly where we can store information. We can in theory use them for whatever we want, however we do need to follow conventions in order for our program to operate without errors (%rsp for example keeps track of the stack pointer).
Writing for gnu(at&t) assembly Copy the top 2 words from the stack into registers AX and CX respectively. Do this without changing the stack pointer(dont use pop) Use BP as the base register. Place the following code at thebeginning of your program:mov dx, 1234Hpush dxmov dx, 0ABCDHpush dx
Given the typical stack frame set up, what will be in edx after executing this instruction: mov edx, [ebp + 4]? Hint: It is NOT any of the selections above.

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education