0

pleasse i need someone to assit me with my assighnment.

q1. Examine the code below and Draw the stack frame after analysing the assembly code when function1, function2 and function3 are called by the main program for a 32-bit system. Figure 2 shows a sample stack frame of a function.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int function1(int x, int y, int z)
{
    int result_func1;
    result_func1 = x + y + z;
    return result_func1;
}

int function2(int x, int y, char* input_string)
{
    int result_func2;
    char buffer[20];
    strcpy(buffer, input_string);
    printf("your input string %s is copied in the buffer \n", input_string);
    result_func2= x - y;
    return result_func2;
}

void function3(int result1, int result2)
{
    printf("The result of function 1 is %d\n", result1);
    printf("The result of function 1 is %d\n", result1);
}

void function4(void)
{
    printf("The function never gets called is \n");
    exit(-1);
}
int main(int argc, char* argv[])
{
    int result1;
    int result2;
    result1 = function1(5, 10, 15);
    result2 = function2(20, 8, argv[1]);
    function3(result1, result1);
}

assembly code of the after the analysis:

 0x00005555555552c0 <+0>:   endbr64 
       0x00005555555552c4 <+4>: push   %rbp
       0x00005555555552c5 <+5>: mov    %rsp,%rbp
       0x00005555555552c8 <+8>: sub    $0x20,%rsp
       0x00005555555552cc <+12>:    mov    %edi,-0x14(%rbp)
       0x00005555555552cf <+15>:    mov    %rsi,-0x20(%rbp)
       0x00005555555552d3 <+19>:    mov    $0xf,%edx
       0x00005555555552d8 <+24>:    mov    $0xa,%esi
       0x00005555555552dd <+29>:    mov    $0x5,%edi
       0x00005555555552e2 <+34>:    callq  0x5555555551c9 <function1>
       0x00005555555552e7 <+39>:    mov    %eax,-0x8(%rbp)
       0x00005555555552ea <+42>:    mov    -0x20(%rbp),%rax
       0x00005555555552ee <+46>:    add    $0x8,%rax
       0x00005555555552f2 <+50>:    mov    (%rax),%rax
       0x00005555555552f5 <+53>:    mov    %rax,%rdx
       0x00005555555552f8 <+56>:    mov    $0x8,%esi
       0x00005555555552fd <+61>:    mov    $0x14,%edi
       0x0000555555555302 <+66>:    callq  0x5555555551ef <function2>
       0x0000555555555307 <+71>:    mov    %eax,-0x4(%rbp)
       0x000055555555530a <+74>:    mov    -0x8(%rbp),%edx
       0x000055555555530d <+77>:    mov    -0x8(%rbp),%eax
       0x0000555555555310 <+80>:    mov    %edx,%esi
       0x0000555555555312 <+82>:    mov    %eax,%edi
       0x0000555555555314 <+84>:    callq  0x555555555261 <function3>
       0x0000555555555319 <+89>:    mov    $0x0,%eax
       0x000055555555531e <+94>:    leaveq 
       0x000055555555531f <+95>:    retq   

modidied(actual 32bit assembly code)

function1:
        push    ebp
        mov     ebp, esp
        mov     eax, DWORD PTR [ebp+12]
        add     eax, DWORD PTR [ebp+8]
        add     eax, DWORD PTR [ebp+16]
        pop     ebp
        ret
.LC0:
        .string "your input string %s is copied in the buffer \n"
function2:
        push    ebp
        mov     ebp, esp
        push    ebx
        lea     eax, [ebp-28]
        sub     esp, 44
        mov     ebx, DWORD PTR [ebp+16]
        push    ebx
        push    eax
        call    strcpy
        pop     eax
        pop     edx
        push    ebx
        push    OFFSET FLAT:.LC0
        call    printf
        mov     eax, DWORD PTR [ebp+8]
        mov     ebx, DWORD PTR [ebp-4]
        sub     eax, DWORD PTR [ebp+12]
        leave
        ret
.LC1:
        .string "The result of function 1 is %d\n"
function3:
        push    ebp
        mov     ebp, esp
        push    ebx
        sub     esp, 12
        mov     ebx, DWORD PTR [ebp+8]
        push    ebx
        push    OFFSET FLAT:.LC1
        call    printf
        mov     DWORD PTR [ebp+12], ebx
        add     esp, 16
        mov     ebx, DWORD PTR [ebp-4]
        mov     DWORD PTR [ebp+8], OFFSET FLAT:.LC1
        leave
        jmp     printf
.LC2:
        .string "The function never gets called is "
function4:
        push    ebp
        mov     ebp, esp
        sub     esp, 20
        push    OFFSET FLAT:.LC2
        call    puts
        mov     DWORD PTR [esp], -1
        call    exit
main:
        lea     ecx, [esp+4]
        and     esp, -16
        push    DWORD PTR [ecx-4]
        push    ebp
        mov     ebp, esp
        push    ecx
        sub     esp, 8
        mov     eax, DWORD PTR [ecx+4]
        push    DWORD PTR [eax+4]
        push    8
        push    20
        call    function2
        pop     edx
        pop     ecx
        push    30
        push    30
        call    function3
        mov     ecx, DWORD PTR [ebp-4]
        xor     eax, eax
        leave
        lea     esp, [ecx-4]
        ret

stack frame example of function

please i need someone who can assist me with who to start

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    Are you sure that's the correct assembly? The text says _"32-bit system"_ but that is clearly 64 bit code. Anyway, where did you get stuck? – Jester Jan 08 '22 at 00:14
  • hey jester, i probably made a mistake since i'm still finding it difficult to understand it. i got stuck at how to draw the stack frame. i really don't understand it. – Wale Olokunola Jan 08 '22 at 00:22
  • First verify whether that's the asm code you should be using. Then go through the instructions and whenever you see one that changes the stack apply what it does to your diagram. When you reach a point the question asks about, copy the diagram. E.g. `push %rbp` will be your first instruction that does something to the stack so draw "base pointer" into a stack slot. Then `sub $0x20,%rsp` will allocate 32 bytes, so draw that. `mov %edi,-0x14(%rbp)` will write `edi` into the allocated space at offset `-20`, so write `argc` in there. – Jester Jan 08 '22 at 00:36
  • 2
    I would rather use a bit more optimized code. It will show you that ABI does not always use stack for everything. https://godbolt.org/z/nx6Easo6G – 0___________ Jan 08 '22 at 01:05
  • thank you very much @Jester .......kindly please help me look through again i have added the 32bit assembly code – Wale Olokunola Jan 08 '22 at 01:25

0 Answers0