How do I use MacOS's assembler to assemble to THUMB

100 views Asked by At

I need to compile a small assembly file (.S) to THUMB, but MacOS's as tool generates ARM machine code:

$ cat > code.S
.text
L0:
    push {lr}
    bl L1
L1:
    pop {r0}
    blx r0
    pop {pc}
$ as -arch armv7 code.S
$ otool -t -v a.out
a.out:
(__TEXT,__text) section
L0:
00000000    e92d4000    stmdb   sp!, {lr}
00000004    ebffffff    bl  0x8
L1:
00000008    e8bd0001    ldm sp!, {r0}
0000000c    e12fff30    blx r0
00000010    e8bd8000    ldm sp!, {pc}

As you can see it generates ARM machine code. How do I make it generate THUMB?

1

There are 1 answers

0
Dany Zatuchna On

OK, thanks to Notlikethat I found what I was looking for in the thread he linked. I'll just sum it up here.

The solution was to add the assembler directive .thumb_func before every label I wished to "flag" as THUMB. The result:

$ cat > code.S
.text
.thumb_func
L0:
    push {lr}
    bl L1
.thumb_func
L1:
    pop {r0}
    blx r0
    pop {pc}
$ as -arch armv7 code.S
$ otool -t -v a.out
a.out:
(__TEXT,__text) section
tramp10:
00000000        b500    push    {lr}
00000002    f000f800    bl      0x6
tramp11:
00000006        bc01    pop     {r0}
00000008        4780    blx     r0
0000000a        bd00    pop     {pc}

Thanks for all the help!