I have this code written on NASM that takes two arguments when used in c: int* ptr
and int val
. The purpose is to set the memory that ptr
points to to val
and then return ptr
.
section .text
global _mptr
_mptr:
mov ebx, [esp + 4]
mov ecx, [esp + 8]
mov [ebx], ecx
mov eax, [esp + 4]
ret
This is the C code:
#include <stdio.h>
extern int* _mptr(int* ptr, int val);
int main(void)
{
int i = 0;
int* ptr = &i;
ptr = _mptr(ptr, 225);
//I want to set i to 225 using _mptr.
printf("%d\n", i);
}
I compile the program in this way:
nasm -f elf asmCode.asm
gcc -Wall -m32 program.c asmCode.o
on Ubuntu OS.
When I execute it with ./a.out
the program fails.
Which is the right way to access the memory that some C-pointer points to in assembly?