Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mixing C and Assembly

Posted on 2003-10-26
4
Medium Priority
?
432 Views
Last Modified: 2008-03-17
Hi all

Pardon me for my ignorance as I am new to assembly programming. Would like to seek help in this problem of mine.

I have a function which needs to be called in C and implemented in assembly. In C, the function is called "char_put(&c)" where a pointer is passed in.

1) How do I access this pointer in assembly?

2) How do I store data into the memory location pointed to by the
pointer? (Suppose to be a 8bit data)

3) When completed, the procedure needs to return a "1" if data is
store or "0" if otherwise. How do I return a value to C from
assembly?

Thank you so much in advance. Any advice is appreciated.

Regards
Wilson
0
Comment
Question by:Wtwh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:mtmike
ID: 9622262
In the standard C calling convention, all variables are passed on the stack and the callee can return a value in eax.

This is how the stack looks in the callee:

...
argument3
argument2
argument1
return address

For example (nasm)

char_put:
  mov edx,[esp + 4] ; load first argument from stack (a string pointer)
  movzx eax,byte [edx] ; load and zero extend first character from the string
  mov byte [edx],0 ; truncate the string
  ret ; return from function, result is in eax

Note that the callee may clobber eax, ecx and edx.

Here's the C equivalent of the above assembly function:

int char_put(char *s)
{
  int first;

  first = (int) s[0];
  s[0] = 0;
  return first;
}

You can also write the wanted function in C, let the compiler generate the assembly and then modify the generated function.
0
 

Author Comment

by:Wtwh
ID: 9622322
Thank you for the response. It was most useful.

I have 2 more queries.

1) The parameter passed in from the C function is a pointer which is supposed to be modified by the assembly procedure. How do I change the value of the data pointed to by the pointer? ie How do I access the memory location when the argument passed in is the pointer?

2) Which compiler can I use which can generate the assembly code from C function?

Apologise for my ignorance again as I'm really new to all these.

Regards
Wilson
0
 
LVL 5

Accepted Solution

by:
mtmike earned 200 total points
ID: 9623765
You load the argument (a pointer) from the stack into a register and then you can dereference that register. The example including its C equivalent:

mov edx,[esp + 4] ; edx = s
movzx eax,byte [edx] ; eax = (int) *s;
mov byte [edx],0 ; *s = 0;

If you're completely new to assembly, it's easiest to learn the basics of assembly first and then try to hook it to C code. The latter is quite easy actually. You only have to respect the calling convention and then assembly your sources into an object file that can be linked as usual.

Every compiler that I know of can generate assembly. In fact, most consist of a separate compiler, assembler and linker. For example gcc has the -S switch to output the assembly of a C/C++ source. MSVC has a similar switch.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question