Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am required to change C code to Assembly. I understand how to change the code but am having trouble explaining what the code is

I am required to change C code to Assembly. I understand how to change the code but am having trouble explaining what the code is converted to when reading it in assembly. Below is the code in C

#include #include int main() { int num, i; int product=1; printf("Enter a number: "); scanf("%d", &num); for(i=num;i>0; i--) product = product * i; printf("The factorial for %d is: %d ", num, product); return 1; }

-- Below is the code changed to assembly --

1. .file "assignment2_1.c" 2. .section .rodata 3. .LC0: 4. .string "Enter a number:" 5. .LC1: 6. .string "%d" 7. .LC2: 8. .string "The factorial for %d is: %d " 9. .text 10. .globl main 11. .type main, @function 12. main: 13. .LFB2: 14. .cfi_startproc 15. pushq %rbp 16. .cfi_def_cfa_offset 16 17. .cfi_offset 6, -16 18. movq %rsp, %rbp 19. .cfi_def_cfa_register 6 20. subq $16, %rsp 21. movl $1, -4(%rbp) 22. movl $.LC0, %edi 23. call puts 24. leaq -12(%rbp), %rax 25. movq %rax, %rsi 26. movl $.LC1, %edi 27. movl $0, %eax 28. call __isoc99_scanf 29. movl -12(%rbp), %eax 30. movl %eax, -8(%rbp) 31. jmp .L2 32. .L3: 33. movl -4(%rbp), %eax 34. imull -8(%rbp), %eax 35. movl %eax, -4(%rbp) 36. subl $1, -8(%rbp) 37. .L2: 38. cmpl $0, -8(%rbp) 39. jg .L3 40. movl -12(%rbp), %eax 41. movl -4(%rbp), %edx 42. movl %eax, %esi 43. movl $.LC2, %edi 44. movl $0, %eax 45. call printf 46. movl $1, %eax 47. leave 48. .cfi_def_cfa 7, 8 49. ret 50. .cfi_endproc 51. .LFE2: 52. .size main, .-main 53. .ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4" 54. .section .note.GNU-stack,"",@progbits

Please help by explaining the lines of code in assembly and how they correspond to the C code. I have numbered the lines to make it easier to explain what line is doing what.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is heritability?

Answered: 1 week ago