CS 410 assignment 2_1 C++ to Assembly With Loops Activity

docx

School

Southern New Hampshire University *

*We aren’t endorsed by this school

Course

410

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

3

Uploaded by BailiffSpider1312

Report
CS 410 C++ to Assembly with Loops Activity Template Step 1: Explain the functionality of the C++ code. The program requests a user to input an integer value, then calculates the factorial and prints it out. C++ Code Functionality C++ Line of Code Explanation of Functionality #include<iostream> Includes the header file name iostream using namespace std; Uses a standard namespace int main() Creates a function that returns an integer int num, i; int product =1; Declares the variable for the program cout<<"Enter a number:\n"<< endl; Prints out instructions for the user cin>>num; Collects the input from the user for(i=num;i>0; i--) product = product * i; Multiplies the product times each number from 1 to the number input provided by the user or “num” Calculates the factorial cout<<"The factorial for " << num << "is: \n"<< product; Prints out the factorial return 1; Returns true and exits the program 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() Main: { .cfi_startproc int num, i; int product =1; pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $32, %rsp movq %fs:40, %rax movq %rax, -8(%rbp) xorl %eax, %eax
movl$1, -12(%rbp) leaq .LC0(%rip), %rsi leaq _ZSt4cout(%rip), %rdi cout<<"Enter a number:\n"<< endl; .LC0: .string "Enter a number:\n" cin>>num; for(i=num;i>0; i--) product = product * i; movl -12(%rbp), %eax movl %eax, %esi movq %rdx, %rdi call _ZNSolsEi@PLT movl $1, %eax movq -8(%rbp), %rcx xorq %fs:40, %rcx cout<<"The factorial for " << num << "is: \n"<< product; .LC1: .string "The factorial for " .LC2: .string "is: \n" .text return 1; leave .cfi_def_cfa 7, 8 ret .cfi_endproc
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 Main: Beginning of the main function .cfi_startproc Used at the beginning of each function .LC0: .string "Enter a number:\n" .LC1: .string "The factorial for " .LC2: .string "is: \n" .text These are the string variables to display messages to the user. movq %rax, -8(%rbp) Set up variable num 8 bits above rdp and in rax leaq .LC0(%rip), %rsi Prints out “Enter a number” leaq -20(%rbp), %rax movq %rax, %rsi leaq _ZSt3cin(%rip), %rdi call _ZNSirsERi@PLT Stores the users input(integer) inside num, rax is now assigned to whatever value 20 and above rdp is. movl -20(%rbp), %eax movl %eax, -16(%rbp) .L3: cmpl $0, -16(%rbp) jle .L2 movl -12(%rbp), %eax imull -16(%rbp), %eax movl %eax, -12(%rbp) subl $1, -16(%rbp) jmp .L3 Num is stored into I, the for loop starts and imull is called to multiply the value, which is stored in eax, then we are jumped to .L3 leaq .LC1(%rip), %rsi leaq .LC2(%rip), %rsi This prints out "The factorial for " and the factorial that was calculated .cfi_endproc sed at the end of a function where it closes the entry previously opened by .cfi_startproc
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