Solved

Mixing C and Assembly

Posted on 2003-10-26
4
393 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
  • 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now