I am attempting to mimic the behavior of sizeof in C within x86-assembly i.e to print the size of data pushed onto the stack in bytes starting from before push operations to the current stack pointer (after push operations) using the write syscall.
I am attempting to achieve this by storing the memory address of the stack pointer into the edp register/base pointer before push operations. And subtracting the value of ebp - esp and storing within eax after push operations. Finally I print the eax integer value to stdout
For example:
section .text
global _start
_start:
; set the frame pointer to the beginning of the stack before-
; data is pushed.
mov ebp, esp
push byte 0x00 ; <- null terminating byte/string truncation
push 0x64636261 ; <- data
mov eax, ebp
sub eax, esp ; start of stack - end of stack = sizeof(data) stored in eax
mov edx, 4
mov ecx, eax
mov ebx, 1
mov eax, 4
int 0x80
mov eax, 1
int 0x80
compile with:
nasm -f elf32 -g test.asm && ld -melf_i386 test.o -o test
When compiling this code I receive no stdout.
The expected stdout is:
5
What am I missing or doing wrong? How do I correctly store the size of the string on the stack (in bytes) in eax and print the integer/size to stdout.