Solved

Mixing C and Assembly

Posted on 2003-10-26
4
386 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this step by step tutorial with screenshots, we will show you HOW TO: Enable SSH Remote Access on a VMware vSphere Hypervisor 6.5 (ESXi 6.5). This is important if you need to enable SSH remote access for additional troubleshooting of the ESXi hos…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

11 Experts available now in Live!

Get 1:1 Help Now