Solved

Mixing C and Assembly

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

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

Suggested Solutions

Title # Comments Views Activity
Random "Missing Operator In Expression" error 3 977
C types 14 305
How to Building Five Part Assembly Name for Master Page Inherit 5 565
SSIS - Mixed Mode Assembly Error 4 42
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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