Quantcast
Channel: Embedded Software (Read Only)
Viewing all articles
Browse latest Browse all 25965

Forum Post: RE: How to call assembly code from C in Linux?

$
0
0

Bear with me, I'm a command line guy, not CCS. Below is my simple C program, hello.c, that I compiled with arm-linux-gnueabihf-gcc -c hello.c just to look at the resultant object code

  1 #include <stdio.h>
  2
  3 extern int sitara();
  4
  5 main()
  6 {
  7    int rc;
  8
  9    printf("Hello World!\n");
 10    rc = sitara();
 11    printf("rc=%d from sitara()\n", rc);
 12 }

If I disassemble with arm-linux-gnueabihf-objdump -D hello.o I see

  5 Disassembly of section .text:
  6
  7 00000000 <main>:
  8    0:   b580            push    {r7, lr}
  9    2:   b082            sub     sp, #8
 10    4:   af00            add     r7, sp, #0
 11    6:   f240 0000       movw    r0, #0
 12    a:   f2c0 0000       movt    r0, #0
 13    e:   f7ff fffe       bl      0 <puts>
 14   12:   f7ff fffe       bl      0 <sitara>
 15   16:   6078            str     r0, [r7, #4]
 16   18:   f240 0000       movw    r0, #0
 17   1c:   f2c0 0000       movt    r0, #0

Notice the half-word boundaries. This suggests to me that the main() code runs in thumb mode. So I changed the .arm to .thumb in my simple sitara.S file and I can build the code and it runs without the segfault. Below is my sitara.S file

  1
  2         .thumb
  3         .text
  4
  5         .globl  sitara
  6  sitara:
  7         mov     r0, #2
  8         mov     pc, lr

I built with arm-linux-gnueabihf-gcc -o hello hello.c sitara.S

Steve K.


Viewing all articles
Browse latest Browse all 25965

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>