I am attempting to calculate the size of pushed data onto the stack. When attempting this I receive a segmentation fault. My objective is to point ebp into esp before pushing any data onto the stack. After I push data onto the stack I am measuring the size of said data in bytes ebp - esp and storing in ebx and printing to stdout using printf.
For example:
; compile with:
; nasm -f elf test.asm && gcc -m32 -o test test.o
section .text
global main
extern printf
main:
; set the frame pointer to the beginning of the stack before-
; data is pushed.
push ebp
mov ebp, esp
push ebx
push 0x00 ; <- null terminating byte/string truncation
push 0x64636261 ; <- data
mov ebx, ebp
sub ebx, esp ; start of stack - end of stack = sizeof(data) stored in ebx
push ebx
push format_str
call printf
add esp, 8
pop ebp
pop ebx
ret
section .data
format_str db "%s", 2, 0
When compiling this code I receive the output:
Segmentation fault (core dumped)
Expected output:
5
%d...mov esp, ebp.