Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Subroutine arguments are passed on the stack, and can be accessed within the bod

ID: 3679095 • Letter: S

Question

Subroutine arguments are passed on the stack, and can be accessed within the body of the subroutine starting at address EBP+8. At the start of each subroutine: Save EBP on the stack Copy the current value of the stack pointer (ESP) to EBP Create space within the stack for each local variable by subtracting the appropriate value from ESP. For example, if your function uses four integer local variables, each of which contains four bytes, subtract 16 from ESP. Local variables can then be accessed starting at the address EBP-4. A subroutine's return value is typically stored in EAX. See Lectures 14 and 16-18 for more details on subroutines, the x 86 architecture, and the conversion from high-level concepts to low-level assembly.

Explanation / Answer

The X86 assembly routine for a given subroutine function:

f(int, int, int):

        pushq   %rbp

        movq    %rsp, %rbp

        pushq   %rbx

        movl    %edi, -28(%rbp)

        movl    %esi, -32(%rbp)

        movl    %edx, -36(%rbp)

        movl    -32(%rbp), %eax

        movl    -28(%rbp), %edx

        leal    (%rdx,%rax), %eax

        movl    %eax, -12(%rbp)

        movl    -36(%rbp), %eax

        movl    -12(%rbp), %edx

        leal    (%rdx,%rax), %ecx

        movl    -36(%rbp), %eax

        movl    -12(%rbp), %edx

        movl    %edx, %ebx

        subl    %eax, %ebx

        movl    %ebx, %eax

        imull   %ecx, %eax

        popq    %rbx

        leave

        ret

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote