Q1. Draw a diagram of the stack prior to execution of line 32 – there should be 3 stack frames – show the beginning and end of each frame as well as the current positions of EBP and ESP. You need to draw a diagram of the stack prior to execution of line 32 by looking at the following Q1 code.
Q1 Code
func1:
push ebp
mov ebp, esp
sub esp, 24
mov eax, DWORD PTR [ebp+8]
lea edx, [eax+eax]
mov eax, DWORD PTR [ebp+8]
add edx, eax
mov eax, DWORD PTR [ebp+12]
add eax, edx
mov DWORD PTR [ebp-12], eax
sub esp, 8
push 3
push DWORD PTR [ebp-12]
call func2
add esp, 16
leave
ret
.size func1, .-func1
.globl func2
.type func2, @function
func2:
push ebp
mov ebp, esp
mov eax, DWORD PTR [ebp+8]
sub eax, DWORD PTR [ebp+12]
pop ebp
ret
.size func2, .-func2
.globl main
.type main, @function
main:
lea ecx, [esp+4]
and esp, -16
push DWORD PTR [ecx-4]
push ebp
mov ebp, esp
push ecx
sub esp, 20
mov DWORD PTR [ebp-12], 0
sub esp, 8
push 6
push 4
call func1
add esp, 16
mov DWORD PTR [ebp-12], eax
mov eax, 0
mov ecx, DWORD PTR [ebp-4]
leave
lea esp, [ecx-4]
ret
.size main, .-main
.ident “GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2”
.section …,@progbits
Q2. In the following Q2 code, line 33 can be replaced with a (arguably) simpler instruction – what is the instruction that could replace it? Does this new instruction take up more or less room in the executable?
Q2 Code
main:
push ebp
mov ebp, esp
sub esp, 16
mov DWORD PTR [ebp-4], 4
mov DWORD PTR [ebp-16], 0
mov DWORD PTR [ebp-8], 0
mov eax, DWORD PTR [ebp-4]
cmp eax, 1
je .L3
cmp eax, 4
je .L4
jmp .L9
.L3:
mov DWORD PTR [ebp-16], 1
jmp .L5
.L4:
mov DWORD PTR [ebp-16], 2
jmp .L5
.L9:
mov DWORD PTR [ebp-16], 3
.L5:
mov DWORD PTR [ebp-12], 0
jmp .L6
.L7:
mov eax, DWORD PTR [ebp-12]
add DWORD PTR [ebp-8], eax
add DWORD PTR [ebp-12], 1
.L6:
mov eax, DWORD PTR [ebp-12]
cmp eax, DWORD PTR [ebp-16]
jl .L7
mov eax, 0
leave
ret
.size main, .-main
.ident “GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2″
.section .note.GNU-stack,””,@progbits