Solved

Mixing C and Assembly

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Binary Bomb/GDB Questions 10 1,938
BufBomb Firecracker 4 2,181
query on MOV assembly instruction 6 583
How to Building Five Part Assembly Name for Master Page Inherit 1 475
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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