Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
use assembly code in c code

Subject: use assembly code in c code

From: Robbin

Date: 21 Oct, 2010 09:34:04

Message: 1 of 4

hi everyone,

I'm trying to figure out if its possible to use straight assembly code in C? i have C-mex files which calles external c functions. these c functions have a sort of memory management and to protect this, i need semaphores. i found the assembly instruction CMPXCHG/cmp to do this for me. but how can i implement this in the external c functions, so that matlab knows that its assembly when its compiling?

Thanks,

Robbin

Subject: use assembly code in c code

From: Rune Allnor

Date: 21 Oct, 2010 11:14:28

Message: 2 of 4

On Oct 21, 11:34 am, "Robbin " <robbini...@hotmail.com> wrote:
> hi everyone,
>
> I'm trying to figure out if its possible to use straight assembly code in C?

Yes, it is. Check the docs of your C compiler.

> i have C-mex files which calles external c functions. these c functions have a sort of memory management and to protect this, i need semaphores. i found the assembly instruction CMPXCHG/cmp to do this for me. but how can i implement this in the external c functions, so that matlab knows that its assembly when its compiling?

*Matlab* has no information about such issues, nor does it
need to know. This is an issue with the C compiler. Check the
docs.

A word of warning, though: With C you need to know what you are
doing, or you might end up in severe trouble. With assembler
you *really* need to know not only what you are doing, but what
the computer does and how it does it.

Since I have to tell you these things - stay away from assembler.
You are just not up to it.

Rune

Subject: use assembly code in c code

From: Roger Stafford

Date: 21 Oct, 2010 16:04:04

Message: 3 of 4

"Robbin " <robbinio57@hotmail.com> wrote in message <i9p1ec$kl4$1@fred.mathworks.com>...
> hi everyone,
>
> I'm trying to figure out if its possible to use straight assembly code in C? i have C-mex files which calles external c functions. these c functions have a sort of memory management and to protect this, i need semaphores. i found the assembly instruction CMPXCHG/cmp to do this for me. but how can i implement this in the external c functions, so that matlab knows that its assembly when its compiling?
>
> Thanks,
>
> Robbin
- - - - - - - - - -
  The fundamental fact to realize about assembly language coding is that it has to be designed for a particular computer instruction set. Different computer types have different instruction repertoires and therefore require different assembly language coding. The purpose of higher language compilers such as C is to carry out a translation from a common language written by users who don't have to know the particular details of the computer being used. The same higher language can serve to compute equivalent algorithms on different computers, but it is up to the compiler to know how to make this translation for a particular computer type. Therefore each kind of computer has its own version of any given compiler.

  When you speak of using "straight assembly code", the question arises as to who devises this code? The code has to serve a particular kind of machine and the person writing it has to know a great deal about that computer.

  As you can perhaps guess, I am an old-time assembly language "hacker" and have written many programs in assembly language over the years, and in each case I had to understand all the myriad of little details about how each instruction on the machine worked. Assembly language writing is not a simple undertaking. I think this is what Rune is warning you about.

Roger Stafford

Subject: use assembly code in c code

From: Mike Thomas

Date: 21 Oct, 2010 18:15:07

Message: 4 of 4

"Robbin " <robbinio57@hotmail.com> wrote in message <i9p1ec$kl4$1@fred.mathworks.com>...
> hi everyone,
>
> I'm trying to figure out if its possible to use straight assembly code in C? i have C-mex files which calles external c functions. these c functions have a sort of memory management and to protect this, i need semaphores. i found the assembly instruction CMPXCHG/cmp to do this for me. but how can i implement this in the external c functions, so that matlab knows that its assembly when its compiling?
>
> Thanks,
>
> Robbin

Hello Robbin,

We do many different forms of code optimizations day in and day out, mostly in ANSI-C. I would like to echo what I have already seen in here, namely, that assembly is much more cumbersome to work with, and very specific to the machine type. Code running on one machine perfectly won't even compile on a slightly different machine.

If you think about it, C is pretty much a "portable assembler" language. It is very difficult to write an assembly version of some C code and have it execute faster, mostly because compiler writers have become very clever over the years, and they literally incorporate "every trick in the book" to crank out very fast executing code (unrolling for loops, turning long lists of case statements into binary searches, etc.)

Unless you are 100% certain that you need to implement an assembly solution for this, I'd stick to good ole, lean & mean C.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us