Page 1 of 1

C input to SI?

Posted: Thu Feb 05, 2004 8:39 am
by St8ic
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!

Re:C input to SI?

Posted: Thu Feb 05, 2004 8:43 am
by Candy
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?

Posted: Thu Feb 05, 2004 7:07 pm
by St8ic
what if it's monkey=scanf();? I just used monkey=WhatIWant as an example, and you can't have monkey=&scanf();, can you?

Re:C input to SI?

Posted: Fri Feb 06, 2004 2:59 am
by Pype.Clicker
what exactly do you want to do ? if you're just worring about having access to the *value* of 'monkey', calling

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
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 ...

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
I hope this makes things clearer for you ...

Re:C input to SI?

Posted: Tue Feb 10, 2004 10:51 pm
by St8ic
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?

Posted: Tue Feb 10, 2004 11:56 pm
by Schol-R-LEA
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.
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.

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' */
The main rationale for this design is has to do with memory allocation, specifically for strings; like most of the standard functions, scanf() does not allocate new memory on it's own, so you have to make sure that there is memory allocated for any strings you read ahead of time, and scanf() needs to have a pointer to that memory in order to save the read string into it.
HTH. C&CW.