This is assembly language code. I deleted all the assembler directives. Please d
ID: 3746744 • Letter: T
Question
This is assembly language code. I deleted all the assembler directives. Please describe the result of each instruction in the code below please.
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $5, -12(%rbp)
movl $3, -8(%rbp)
movl -12(%rbp), %edx
movl -8(%rbp), %eax
addl %edx, %eax
movl %eax, %edx
shrl $31, %edx
addl %edx, %eax
sarl %eax
movl %eax, -4(%rbp)
leaq .LC0(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx
movl -4(%rbp), %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZNSolsEi@PLT
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT
movl $0, %eax
leave
ret
_Z41__static_initialization_and_destruction_0ii:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L5
cmpl $65535, -8(%rbp)
jne .L5
leaq _ZStL8__ioinit(%rip), %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
leaq _ZStL8__ioinit(%rip), %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
movq %rax, %rdi
call __cxa_atexit@PLT
nop
leave
ret
_GLOBAL__sub_I_main:
pushq %rbp
movq %rsp, %rbp
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
ret
the c++ code that this assembly language was generated by is listed below:
#include
#define Y 3
int main()
{
int x = 5;
int y = Y;
int ave = (x + y) / 2;
std::cout << "Average is " << ave << std::endl;
return 0;
}
Explanation / Answer
main:
pushq %rbp # we push the old base pointer onto the stack to save it for later. Then we copy the value of the base pointer
movq %rsp, %rbp # %rbp points to the base of the main's stack frame
subq $16, %rsp # unsued stack in the stack structure
movl $5, -12(%rbp) # move 5 into rbp // local variables //int x
movl $3, -8(%rbp) # move 3 into rbp // local variables // Y
movl -12(%rbp), %edx # move the variable value into another variable
movl -8(%rbp), %eax # move the variable value into another variable
addl %edx, %eax # adding a variable
movl %eax, %edx # move the variable value into another variable
shrl $31, %edx
addl %edx, %eax # update the stack structure with the return address
sarl %eax
movl %eax, -4(%rbp) # moving the value from one register to another register
leaq .LC0(%rip), %rsi # links to the header file to call the cout function
leaq _ZSt4cout(%rip), %rdi # calls the function cout
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movq %rax, %rdx #the rest 4 lines to get the value given in the cout
movl -4(%rbp), %eax
movl %eax, %esi
movq %rdx, %rdi
call _ZNSolsEi@PLT # calling it from the stack
movq %rax, %rdx
movq _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@GOTPCREL(%rip), %rax
movq %rax, %rsi
movq %rdx, %rdi
call _ZNSolsEPFRSoS_E@PLT #calling the variable avg
movl $0, %eax
leave # leaving the function
ret # return address stored in the stack
_Z41__static_initialization_and_destruction_0ii:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L5 # rest all to initilize the iostream in the file
cmpl $65535, -8(%rbp)
jne .L5
leaq _ZStL8__ioinit(%rip), %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
leaq _ZStL8__ioinit(%rip), %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
movq %rax, %rdi
call __cxa_atexit@PLT
nop
leave #leaves the function
ret # function is set to return
_GLOBAL__sub_I_main: # main function starts from here
pushq %rbp # pusing the value in the stack as base address
movq %rsp, %rbp # referencing it in the base address in the stack
movl $65535, %esi # variable loading in the stack
movl $1, %edi # return is set to 1
call _Z41__static_initialization_and_destruction_0ii # calling the printf function
popq %rbp # printing the ave in the program
ret # returning 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.