# Using assembler routines in both JS and C++?

**URL:** https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155
**Category:** micro:bit
**Created:** [May 20, 2020, 2:14pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155 "2020-05-20T14:14:56Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 20, 2020, 2:14pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/1 "2020-05-20T14:14:56Z")

</div>

I understand how to call an assember routine from Javascript because there is a shim in a TS file that accompanies the assembler, such as the following, where one can also rename the function from` myAssemberCode()` to `widget()` for example.

```
namespace myextension {
    //% shim=myAssemberCode
    export function widget(args...) {
        return
    }

```

That’s straightforward. Assembly code can be used for time-critical Microbit pin output, for example.

But if my extension has a .cpp file rather than a .ts - or perhaps it even has both - I can’t see how to let C++ call the routine. What do I do, please?

---

<div class="post-metadata">

### Author: ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)
#### Post date: [May 20, 2020, 11:38pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/2 "2020-05-20T23:38:29Z")

</div>

You use the same `shim` annotation to call a C/C++ function. It may look like `NAMESPACE::NAME` if you use a namespace.

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 9:42am UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/3 "2020-05-21T09:42:52Z")

</div>

Thank you. I’ll try again in a minute. I thought I had already tried that with a //% shim=… in the cpp file but it didn’t work. It’s possible I might have made a typo in the shim that I simply failed to see. I’ve found previously that a consequence of things such as `throwStickForMyDog()` that you can miss the fact you’ve got a lowercase “f” by mistake despite staring at it for ten minutes.

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 12:04pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/4 "2020-05-21T12:04:37Z")

</div>

Blimey, this just isn’t working and I have no idea why not.

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 2:11pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/5 "2020-05-21T14:11:32Z")

</div>

Is it possible @peli you could help me on this, please? Nothing works and I haven’t the foggiest idea what I should be doing.

How do I call an assembly function in C++ code? It appears that the shims do not achieve this. I’ve tried every conceivable combination.

[https://github.com/MTKilpatrick/pxt-nokialcd-asm](https://github.com/MTKilpatrick/pxt-nokialcd-asm)

I’ve got this extension with a TS file, a CPP file and an ASM file. There is one namespace for the extension. I’ve put the relevant functions right at the top of the namespace declarations in the TS and CPP files for clarity. I have a byte-send and a buffer-send function in the ASM file.

The important code is lines 68 and 69 of the nokialcd.ts file. One is commented out, the other not. Both should do exactly the same thing: they call an assembler function. One is via TS, the other supposedly via a C wrapper. But the C version doesn’t work.

I cannot see how I can get the assembler function to be called within the C code. How do you do this, please?

---

<div class="post-metadata">

### Author: ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)
#### Post date: [May 21, 2020, 3:53pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/6 "2020-05-21T15:53:55Z")

</div>

The shim notation only work for linking TypeScript to C++ functions. They do not have any effect for C++ / ASM interaction.

We do not pass ASM files to the ARM compiler so you will need inline the assembly source into your C++ source file.

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 4:03pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/7 "2020-05-21T16:03:42Z")

</div>

Aaah, I see. Are there some directives to precede and/or follow the block of assembler code when it’s in the CPP file?

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 4:06pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/8 "2020-05-21T16:06:12Z")

</div>

Such as things explained here? :-

> <https://github.com/MicrosoftDocs/cpp-docs/blob/master/docs/assembler/arm/arm-assembler-directives.md>

---

<div class="post-metadata">

### Author: ![peli](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/peli/32/12_2.png) [@peli](https://forum.makecode.com/u/peli)
#### Post date: [May 21, 2020, 4:20pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/9 "2020-05-21T16:20:19Z")

</div>

I’m not familiar with the details of inline asm in C++. You’ll have to console the ARM/GCC docs.

---

<div class="post-metadata">

### Author: ![mkilpatr](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@mkilpatr](https://forum.makecode.com/u/mkilpatr)
#### Post date: [May 21, 2020, 7:08pm UTC](https://forum.makecode.com/t/using-assembler-routines-in-both-js-and-c/2155/10 "2020-05-21T19:08:32Z")

</div>

It’s rather disappointing that it’s not easy just to link to the assembler function in C as it is with TS. The documentaion for inline assembler makes it look rather restrictive.
