I'm trying to print the value of a counter that increments inside a while loop I made, this is just a part of a bigger function I'm working on for a project, here's how I'm increasing the value of the counter variable and trying to print it, because of the way I'm calling the printf function I believe I need to push char[] variables with what I want to print onto the stack, I've tried pushing the counter values directly to print (using "push edx" directly instead of storing the address of a char[] variable and then pushing it) and it just spits out random numbers probably the memory address of the value or something, the way the print function call is set up works properly when I print char[] variables for which I've already specified the content when I declare them before the _asm tag (for example " char text[4] = "%s\n" "), I would really appreciate your help, I can post the whole function too if needed be.
_calcGravedad:
mov edx, G // G stored in edx
inc edx //increases edx
mov G, edx //returns edx to G
//here I try to convert my int G variable (the counter) into a char[]
//so i can print it, I'm not sure of this part, it doesnt work
lea eax, G //stores memory addres of G into eax
push eax //push eax into the stack
call byte ptr _itoa_s //calls the conversion function from c
pop edx //transfers the result to edx
mov gravedad, edx //moves the result to a char[] variable
//here's the print function call
lea eax, gravedad //get address of gravedad
push eax //push it into the stack
lea eax, texto //push the print format "%s\n" onto the stack
push eax //
call DWORD ptr printf //calls the print function
pop edx //cleans the stack
pop edx //
I'm unsure why you need the address of G
rather than its value, or why you need two library calls. Why not pass your counter value straight to printf
along with a %d
format instead of the %s
format?
I don't have working masm but this should illustrate (MSVC):
#include <stdio.h>
int main(void)
{
int G = 42;
char *fmt = "%d\n";
__asm {
mov eax,G ;counter value
push eax
mov eax,fmt ;format argument
push eax
call printf
pop eax
pop eax
}
return 0;
}
Console output:
42
You might need a different mov
instruction for the format string, such as lea eax,fmt
or mov eax,offset fmt
. Note too, you don't need to qualify the library function calls as you do.