For some functions I need to translate a C variable into SI for the ASM function. e.g.
// C part
monkey=WhatIWant
dothis(monkey);
// ASM part
dothis: mov si,input
doallthiscrap
ret
but, in accordance to Murphy's Law, it sees "monkey" as "monkey", not "WhatIWant". I've read alot of mixing C and assembly docs, but I can't get it right.
Thanks!
C input to SI?
Re:C input to SI?
St8ic wrote: For some functions I need to translate a C variable into SI for the ASM function. e.g.
// C part
monkey=WhatIWant
dothis(monkey);
// ASM part
dothis: mov si,input
doallthiscrap
ret
but, in accordance to Murphy's Law, it sees "monkey" as "monkey", not "WhatIWant". I've read alot of mixing C and assembly docs, but I can't get it right.
Thanks!
Code: Select all
// C part
monkey=&WhatIWant
dothis(monkey);
// ASM part
dothis: mov eax, [esp+4]
mov si, [eax]
doallthiscrap
ret
for passing by reference, you might want to keep eax for storing the result there. What types are WhatIWant and monkey btw?
Re:C input to SI?
what if it's monkey=scanf();? I just used monkey=WhatIWant as an example, and you can't have monkey=&scanf();, can you?
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:C input to SI?
what exactly do you want to do ? if you're just worring about having access to the *value* of 'monkey', calling
is sufficient.
now, if you want to be able to *modify* the content of monkey in your ASM function, what you need to give to your function is the *address* of monkey ...
I hope this makes things clearer for you ...
Code: Select all
int monkey=5;
the_asm_function(monkey);
the_asm_function:
push ebp
mov ebp,esp
mov esi,[ebp+8] ;; you now have the value of monkey in esi
;; i.e. esi == 5
...
pop ebp
ret
now, if you want to be able to *modify* the content of monkey in your ASM function, what you need to give to your function is the *address* of monkey ...
Code: Select all
int monkey=5;
the_asm_function(&monkey);
// here monkey is 7
the_asm_function:
push ebp
mov ebp, esp
mov esi,[ebp+8] ;; now esi points to monkey
add [esi],2 ;; alter the variable
pop ebp
ret
Re:C input to SI?
This code works if I do a "int monkey=5", but not a "monkey=scanf();". Would there be a difference? I already tested my code with "the_asm_function(5);", so I know that part works fine.
Re:C input to SI?
Because that's not how scanf() works. Too bad, too, since it might make more sense if it did, but that's niether here nor there.St8ic wrote: This code works if I do a "int monkey=5", but not a "monkey=scanf();". Would there be a difference? I already tested my code with "the_asm_function(5);", so I know that part works fine.
Scanf() is a formatted scan, the inverse operation to printf() (sort of). It takes a format string, followed by an indeterminate number of pointers to variables; scanf() parses the format string to determine the number and type of items to read in, and stores them in the locations pointed to by the matching pointers. The return value of scanf() is the number of values read successfully, not the read value itself. So, for the read you want, the call would be:
Code: Select all
int monkey;
scanf("%d", &monkey); /* '&' means 'return a pointer to' */
HTH. C&CW.