CS 410 C++ to Assembly Activity Template

docx

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

410

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

3

Uploaded by colossaltitan

Report
CS 410 C++ to Assembly Activity Template Step 1: Explain the functionality of the C++ code. C++ Code Functionality C++ Line of Code Explanation of Functionality #include<iostream> Includes the iostream header file into your program. Enabling you to print and read from the standard input/output. using namespace std; Enables you to call functions from iostream without prepending it with std:: int main() { Declares the start of main function which returns an exit code as an integer int width=10; Assigns a value of 10 to the integer variable named width int height=5; Assigns a value of 5 to the integer variable named height int area; Declares an integer variable named area area = width * height; Assigns the value of width multiplied by height to the area variable cout<<endl<< area; Prints a newline followed by the value of area to the standard output return 0; Returns an exit code of 0 signaling success of the main function } Closes the end of the function definition Step 2: Convert the C++ file into assembly code. Step 3: Align each line of C++ code with the corresponding blocks of assembly code. C++ to Assembly Alignment C++ Line of Code Blocks of Assembly Code int main() { .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $16, %rsp int width=10; movl $10, -12(%rbp) int height=5; movl $5, -8(%rbp) int area; movl -12(%rbp), %eax area = width * height; imull -8(%rbp), %eax
movl %eax, -4(%rbp) cout<<endl<< area; movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax movq %rax, %rsi leaq _ZSt4cout(%rip), %rdi call _ZNSolsEPFRSoS_E@PLT movq %rax, %rdx movl -4(%rbp), %eax movl %eax, %esi movq %rdx, %rdi call _ZNSolsEi@PLT return 0; movl $0, %eax } leave .cfi_def_cfa 7, 8 ret
Step 4: Explain how the blocks of assembly code perform the same tasks as the C++ code. Assembly Functionality Blocks of Assembly Code Explanation of Functionality .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $16, %rsp Allocates 16 bytes for the stack frame by setting the value of the stack pointer to the base pointer then subtracting 16 from it. This gives room for the local variables. movl $10, -12(%rbp) Moves the value of 10 to 12 bytes from the base pointer (width) movl $5, -8(%rbp) Moves the value of 10 to 8 bytes from the base pointer (height) movl -12(%rbp), %eax Moves the value located 12 bytes from the base pointer (width) to the eax register imull -8(%rbp), %eax movl %eax, -4(%rbp) Multiplies the value at the eax register (width) with the value located at 8 bytes from the base pointer (height) Moves the value of the multiplication to 4 bytes from the base pointer (area) movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax movq %rax, %rsi leaq _ZSt4cout(%rip), %rdi call _ZNSolsEPFRSoS_E@PLT movq %rax, %rdx movl -4(%rbp), %eax movl %eax, %esi movq %rdx, %rdi call _ZNSolsEi@PLT Moves the address of std::endl to the rax register (%rax) Moves the address of %rax to %rsi Loads the effective address of std::cout to %rdi Calls a function that takes %rdi and %rsi as the 1 st and 2 nd arguments Moves the result of the function call which is located in %rax into %rdx Moves the value at 4 bytes from the base pointer (area) to the 32 bit eax register Moves the value at the eax register to the esi register. Moves the location of %rdx into %rdi Calls a function located at _ZNSolsEi@PLT (<<) movl $0, %eax Moves the value of 0 to the eax register leave .cfi_def_cfa 7, 8 ret Leaves the function
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help