CS 410 assignment 4-2 Binary to C^M^M 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

17

Uploaded by BailiffSpider1312

Report
CS 410 Binary to C++ Activity Template File One Step 2: Explain the functionality of the blocks of assembly code.
Blocks of Assembly Code Explanation of Functionality push %rbp %rsp,%rbp sub $0x10,%rsp movl $0x1,-0x8(%rbp) cmpl $0x9,-0x8(%rbp) jg 0xa3 <main+163> movl $0x1,-0xc(%rbp) cmpl $0x9,-0xc(%rbp) jg 0x9a <main+154> mov -0x8(%rbp),%eax imul -0xc(%rbp),%eax mov %eax,-0x4(%rbp) mov -0x8(%rbp),%eax mov %eax,%esi lea 0x0(%rip),%rdi # 0x3c <main+60> callq 0x41 <main+65> lea 0x0(%rip),%rsi # 0x48 <main+72> this uses for loops, the first for loop checks if the number is equal to 9 or less, if it is then moves the value into -4(%rbp). If it doesn’t, it returns and the program ends. mov %rax,%rdi callq 0x50 <main+80> mov %rax,%rdx mov -0xc(%rbp),%eax mov %eax,%esi mov %rdx,%rdi callq 0x60 <main+96> lea 0x0(%rip),%rsi # 0x67 <main+103> mov %rax,%rdi callq 0x6f <main+111> mov %rax,%rdx mov -0x4(%rbp),%eax mov %eax,%esi mov %rdx,%rdi callq 0x7f <main+127> mov %rax,%rdx mov 0x0(%rip),%rax # 0x89 <main+137> mov %rax,%rsi mov %rdx,%rdi callq 0x94 <main+148> This checks for number less or equal to 9, then the value of -4(%rbp) is moved to eax. Before the -8(%rbp) value is multiplied by eax. Then there are multiple move statements which move the values around and prepare them to be printed by the callq. addl $0x1,-0xc(%rbp) jmp 0x20 <main+32> addl $0x1,-0x8(%rbp) jmpq 0xf <main+15> mov $0x0,%eax leaveq 0x00000000000000a9 <+169>: retq After the value is printed, the variable is incremented by 1 before looping back.
Step 4: Convert the assembly code to C++ code. Step 5: Explain how the C++ code performs the same tasks as the blocks of assembly code. Blocks of Assembly Code C++ Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x10,%rsp #include<iostream> using namespace std; int main() { Starts program, uses standard namespace, declares main function movl $0x1,-0x8(%rbp) cmpl $0x9,-0x8(%rbp) jg 0x9ad <main+163> addl $0x1,-0x8(%rbp) jmpq 0x919 <main+15> int number, i, a, x; for (a = 1; a <= 9; a++) { Declaring integer variables and prompting user input, creating a for loop until integer is greater than 9. movl $0x1,-0xc(%rbp) cmpl $0x9,-0xc(%rbp) jg 0x9a4 <main+154> addl $0x1,-0xc(%rbp) jmp 0x92a <main+32> for (i = 1; i <= 9; i++){ Creates for loop for integer "i" until it is greater than 9. movl $0x1,-0xc(%rbp) cmpl $0x9,-0xc(%rbp) jg 0x9a4 <main+154> addl $0x1,-0xc(%rbp) jmp 0x92a <main+32> x = a * i; Multiplies a and i assigns the product to x. mov -0x8(%rbp),%eax mov %eax,%esi lea 0x2006da(%rip),%rdi # 0x201020 callq 0x7e0 lea 0x153(%rip),%rsi mov %rax,%rdi callq 0x7b0 mov %rax,%rdx mov -0xc(%rbp),%eax mov %eax,%esi mov %rdx,%rdi callq 0x7e0 lea 0x138(%rip),%rsi mov %rax,%rdi callq 0x7b0 leaveq retq cout << a << " * " << i << " = " << x << endl; } } return 0; } Prints out the final product and returns 0 File Two
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
Step 2: Explain the functionality of the blocks of assembly code.
Blocks of Assembly Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x30,%rsp mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax lea 0x0(%rip),%rsi # 0x1e <main+30> lea 0x0(%rip),%rdi # 0x25 <main+37> callq 0x2a <main+42> mov %rax,%rdx mov 0x0(%rip),%rax # 0x34 <main+52> mov %rax,%rsi mov %rdx,%rdi callq 0x3f <main+63> lea -0x14(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x4d <main+77> callq 0x52 <main+82> mov -0x14(%rbp),%edx mov -0x14(%rbp),%eax imul %eax,%edx mov -0x14(%rbp),%eax imul %edx,%eax mov %eax,-0x14(%rbp) mov -0x14(%rbp),%eax cvtsi2sd %eax,%xmm0 movsd 0x0(%rip),%xmm1 # 0x73 <main+115> mulsd %xmm1,%xmm0 movsd %xmm0,-0x10(%rbp) lea 0x0(%rip),%rsi # 0x83 <main+131> lea 0x0(%rip),%rdi # 0x8a <main+138> callq 0x8f <main+143> mov %rax,%rdx mov -0x10(%rbp),%rax mov %rax,-0x28(%rbp) movsd -0x28(%rbp),%xmm0 mov %rdx,%rdi callq 0xa7 <main+167> mov $0x0,%eax mov -0x8(%rbp),%rcx xor %fs:0x28,%rcx je 0xc0 <main+192> callq 0xc0 <main+192> leaveq retq The program calculates volume based on input radius, initializes variables, calls the CalculateV function, prints the volume and solution, moves variables, returns 0 and exits.
Step 4: Convert the assembly code to C++ code. Step 5: Explain how the C++ code performs the same tasks as the blocks of assembly code.
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
Blocks of Assembly Code C++ Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x30,%rsp #include <iostream> using namespace std; float CalculateV(float pi, int num) { Program starts with standard user namespace and float function to calculate volume mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax lea 0x0(%rip),%rsi # 0x1e <main+30> lea 0x0(%rip),%rdi # 0x25 <main+37> callq 0x2a <main+42> mov %rax,%rdx mov 0x0(%rip),%rax # 0x34 <main+52> mov %rax,%rsi mov %rdx,%rdi callq 0x3f <main+63> lea -0x14(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x4d <main+77> callq 0x52 <main+82> mov -0x14(%rbp),%edx mov -0x14(%rbp),%eax imul %eax,%edx mov -0x14(%rbp),%eax imul %edx,%eax mov %eax,-0x14(%rbp) mov -0x14(%rbp),%eax cvtsi2sd %eax,%xmm0 movsd 0x0(%rip),%xmm1 # 0x73 Return(num*num*num*pi); } int main() { float pi = 3.14; int num; This is the main function for the integer, pi and num are both assigned. mulsd %xmm1,%xmm0 movsd %xmm0,-0x10(%rbp) lea 0x0(%rip),%rsi # 0x83 <main+131> lea 0x0(%rip),%rdi # 0x8a <main+138> callq 0x8f <main+143> cout << “Enter Radius: “ << endl; cin >> num; This prints out enter radius and collects the users input and assigns it to num. mov %rax,%rdx mov -0x10(%rbp),%rax cout << “The volume is: /n“ << CalculateV(pi,num) << endl; Once the the volume is calculated, this prints out the
File Three Step 2: Explain the functionality of the blocks of assembly code.
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
Blocks of Assembly Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x20,%rsp mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax movl $0x1,-0xc(%rbp) lea 0x0(%rip),%rsi # 0x25 <main+37> lea 0x0(%rip),%rdi # 0x2c <main+44> callq 0x31 <main+49> mov %rax,%rdx mov 0x0(%rip),%rax # 0x3b <main+59> mov %rax,%rsi mov %rdx,%rdi callq 0x46 <main+70> lea -0x18(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x54 <main+84> callq 0x59 <main+89> mov -0x18(%rbp),%eax sub $0x1,%eax mov %eax,-0xc(%rbp) movl $0x1,-0x10(%rbp) mov -0x18(%rbp),%eax cmp %eax,-0x10(%rbp) jg 0xe3 <main+227> movl $0x1,-0x14(%rbp) This program prints a diamond shape based on the number of rows are entered by the user, this is where the variables are initialized for the user. mov -0x14(%rbp),%eax cmp -0xc(%rbp),%eax jg 0x99 <main+153> lea 0x0(%rip),%rsi # 0x87 <main+135> lea 0x0(%rip),%rdi # 0x8e <main+142> callq 0x93 <main+147> addl $0x1,-0x14(%rbp) jmp 0x78 <main+120> subl $0x1,-0xc(%rbp) movl $0x1,-0x14(%rbp) mov -0x10(%rbp),%eax add %eax,%eax sub $0x1,%eax cmp %eax,-0x14(%rbp) jg 0xca <main+202> lea 0x0(%rip),%rsi # 0xb8 <main+184> lea 0x0(%rip),%rdi # 0xbf <main+191> callq 0xc4 <main+196> addl $0x1,-0x14(%rbp) jmp 0xa4 0x10(%rbp) The program checks if i is less than or equal to the user input number and jumps to the 'decrement' loops if not.
Step 4: Convert the assembly code to C++ code. Step 5: Explain how the C++ code performs the same tasks as the blocks of assembly code.
Blocks of Assembly Code C++ Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x20,%rsp mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax movl $0x1,-0xc(%rbp) lea 0x0(%rip),%rsi # 0x25 <main+37> lea 0x0(%rip),%rdi # 0x2c <main+44> callq 0x31 <main+49> mov %rax,%rdx mov 0x0(%rip),%rax # 0x3b <main+59> mov %rax,%rsi mov %rdx,%rdi callq 0x46 <main+70> lea -0x18(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x54 <main+84> callq 0x59 <main+89> #include<iostream> using namespace std; int main() { int n, i, j; cout << "Enter number of rows\ n” << endl; cin >> n; This program prints a diamond shape based on the number of rows are entered by the user, this is where the variables are initialized for the user. mov -0x18(%rbp),%eax sub $0x1,%eax mov %eax,-0xc(%rbp) movl $0x1,-0x10(%rbp) mov -0x18(%rbp),%eax cmp %eax,-0x10(%rbp) jg 0xe3 <main+227> movl $0x1,-0x14(%rbp) mov -0x14(%rbp),%eax cmp -0xc(%rbp),%eax jg 0x99 <main+153> lea 0x0(%rip),%rsi # 0x87 <main+135> lea 0x0(%rip),%rdi # 0x8e <main+142> callq 0x93 <main+147> addl $0x1,-0x14(%rbp) jmp 0x78 <main+120> subl $0x1,-0xc(%rbp) movl $0x1,-0x14(%rbp) mov -0x10(%rbp),%eax add %eax,%eax sub $0x1,%eax for(j=1; j<=n; j++) { for(i=1; i<=n-j; i++) { cout << " " << endl; } for(i=1; i<=2*j-1; i++) { cout << "*" << endl; cout << "\n" << endl; } } The program checks if i is less than or equal to the user input number and jumps to the 'decrement' loops if not. Adds %eax, subtracts %eax, and compares 0xc(%rbp) to %ex. Then prints out the asterisk(depending on the number the user enters).
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
File Four Step 2: Explain the functionality of the blocks of assembly code.
Blocks of Assembly Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x30,%rsp mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax movq $0x0,-0x20(%rbp) movq $0x1,-0x18(%rbp) lea 0x0(%rip),%rsi # 0x2e <main+46> lea 0x0(%rip),%rdi # 0x35 <main+53> callq 0x3a <main+58> mov %rax,%rdx mov 0x0(%rip),%rax # 0x44 <main+68> mov %rax,%rsi mov %rdx,%rdi callq 0x4f <main+79> lea -0x28(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x5d <main+93> callq 0x62 <main+98> mov -0x28(%rbp),%rax test %rax,%rax je 0xf2 <main+242> mov -0x28(%rbp),%rcx movabs $0x6666666666666667,%rdx mov %rcx,%rax imul %rdx This Moves %rsp onto %rbp, subtracts %rsp by $0x20, initialize values, callq for cout("Enter the binary number: "), moves %rax into %rsi, %edi, and %eax. sar $0x2,%rdx mov %rcx,%rax sar $0x3f,%rax sub %rax,%rdx mov %rdx,%rax mov %rax,-0x10(%rbp) mov -0x10(%rbp),%rdx mov %rdx,%rax shl $0x2,%rax add %rdx,%rax add %rax,%rax sub %rax,%rcx mov %rcx,%rax mov %rax,-0x10(%rbp) mov -0x10(%rbp),%rax imul -0x18(%rbp),%rax add %rax,-0x20(%rbp) shlq -0x18(%rbp) The most important details are the callq instruction for scanning for user input, the jmp statement for later in the assembly, the multiplication instructions, and the subtracting of the value at %rax by %rdx. These instructions involve scanning for user input, a while loop, moving the value at -20(%rbp) into %rcx, and multiplication.
Step 4: Convert the assembly code to C++ code. Step 5: Explain how the C++ code performs the same tasks as the blocks of assembly code.
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
Blocks of Assembly Code C++ Code Explanation of Functionality push %rbp mov %rsp,%rbp sub $0x30,%rsp #include <iostream> using namespace std; int main() { Starts program, uses standard namespace, declares main function. mov %fs:0x28,%rax mov %rax,-0x8(%rbp) xor %eax,%eax movq $0x0,-0x20(%rbp) movq $0x1,-0x18(%rbp) lea 0x0(%rip),%rsi # 0x2e <main+46> lea 0x0(%rip),%rdi # 0x35 <main+53> callq 0x3a <main+58> mov %rax,%rdx mov 0x0(%rip),%rax # 0x44 <main+68> mov %rax,%rsi mov %rdx,%rdi callq 0x4f <main+79> lea -0x28(%rbp),%rax mov %rax,%rsi lea 0x0(%rip),%rdi # 0x5d <main+93> callq 0x62 <main+98> long long num; int decimalNum, i, rem; cout << "Enter the binary number:\n"; cin >> num; This is where the variables are initialized, and it gets the users input and saves it as a binary value mov -0x28(%rbp),%rax test %rax,%rax je 0xf2 <main+242> mov -0x28(%rbp),%rcx movabs $0x6666666666666667,%rdx mov %rcx,%rax imul %rdx sar $0x2,%rdx mov %rcx,%rax sar $0x3f,%rax sub %rax,%rdx mov %rdx,%rax mov %rax,-0x10(%rbp) mov -0x10(%rbp),%rdx mov %rdx,%rax shl $0x2,%rax add %rdx,%rax add %rax,%rax sub %rax,%rcx mov %rcx,%rax mov %rax,-0x10(%rbp) decimalNum = 0; i = 0; while (num != 0) { rem = num % 10; num /= 10; decimalNum += rem * pow(2, i); ++i; } This is were the while loop is activated and calculates the hex value of the binary number