C input to SI?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
St8ic

C input to SI?

Post 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!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C input to SI?

Post 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?
St8ic

Re:C input to SI?

Post 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?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:C input to SI?

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

Re:C input to SI?

Post 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.
Schol-R-LEA

Re:C input to SI?

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