Skip to content

Instantly share code, notes, and snippets.

@debuti
Last active August 21, 2018 08:29
Show Gist options
  • Save debuti/48eb1d4ed9714e80b3aff33beba4d8e0 to your computer and use it in GitHub Desktop.
Save debuti/48eb1d4ed9714e80b3aff33beba4d8e0 to your computer and use it in GitHub Desktop.
ARM C to ASM call
callasm.bin: file format binary
Disassembly of section .data:
00000000 <.data>:
0: e92d4800 push {fp, lr}
4: e28db004 add fp, sp, #4
8: e59f0004 ldr r0, [pc, #4] ; 0x14
c: eb000001 bl 0x18
10: eafffffe b 0x10
14: 00008054 andeq r8, r0, r4, asr r0
18: e52db004 push {fp} ; (str fp, [sp, #-4]!)
1c: e28db000 add fp, sp, #0
20: e24dd00c sub sp, sp, #12
24: e59f4024 ldr r4, [pc, #36] ; 0x50
28: e5905000 ldr r5, [r0]
2c: e5956000 ldr r6, [r5]
30: e5846000 str r6, [r4]
34: e2855001 add r5, r5, #1
38: e5956000 ldr r6, [r5]
3c: e3560000 cmp r6, #0
40: 1afffffa bne 0x30
44: e28bd000 add sp, fp, #0
48: e49db004 pop {fp} ; (ldr fp, [sp], #4)
4c: e12fff1e bx lr
50: 101f1000 andsne r1, pc, r0
54: 6c6c6143 stfvse f6, [ip], #-268 ; 0xfffffef4
58: 20676e69 rsbcs r6, r7, r9, ror #28
5c: 6d6f7266 sfmvs f7, 2, [pc, #-408]! ; 0xfffffecc
60: 000a4320 andeq r4, sl, r0, lsr #6
-e
Hexdump
00000000 00 48 2d e9 04 b0 8d e2 04 00 9f e5 01 00 00 eb |.H-.............|
00000010 fe ff ff ea 54 80 00 00 04 b0 2d e5 00 b0 8d e2 |....T.....-.....|
00000020 0c d0 4d e2 24 40 9f e5 00 50 90 e5 00 60 95 e5 |..M.$@...P...`..|
00000030 00 60 84 e5 01 50 85 e2 00 60 95 e5 00 00 56 e3 |.`...P...`....V.|
00000040 fa ff ff 1a 00 d0 8b e2 04 b0 9d e4 1e ff 2f e1 |............../.|
00000050 00 10 1f 10 43 61 6c 6c 69 6e 67 20 66 72 6f 6d |....Calling from|
00000060 20 43 0a 00 | C..|
00000064
extern void myprint(char* a);
void _start() {
myprint("Calling from C\n");
while(1){}
}
CROSS_COMPILE ?= arm-unknown-eabi
AOPS = --warn --fatal-warnings
COPS = -Wall -Werror -O0 -nostdlib -nostartfiles -ffreestanding
callasm.bin: print.s callasm.c
$(CROSS_COMPILE)-as $(AOPS) print.s -o print.o
$(CROSS_COMPILE)-cc -c $(COPS) callasm.c -o callasm.o
# Caution! The order of the object files is critical
$(CROSS_COMPILE)-ld callasm.o print.o -o callasm.elf
$(CROSS_COMPILE)-objdump -D callasm.elf -marm > callasm.elf.list
$(CROSS_COMPILE)-objcopy -S -O binary callasm.elf callasm.bin
$(CROSS_COMPILE)-objdump -D -d callasm.bin -b binary -marm > callasm.bin.list
echo -e "\n\nHexdump\n" >> callasm.bin.list
hexdump -C callasm.bin >> callasm.bin.list
runfromc: callasm.bin
qemu-system-arm -M versatilepb -m 128M -nographic -kernel callasm.bin
clean:
-rm print.o callasm.o callasm.elf callasm.bin callasm*list
$ make clean && make runfromc
rm print.o callasm.o callasm.elf callasm.bin callasm*list
arm-unknown-eabi-as --warn --fatal-warnings print.s -o print.o
arm-unknown-eabi-cc -c -Wall -Werror -O0 -nostdlib -nostartfiles -ffreestanding callasm.c -o callasm.o
# Caution! The order of the object files is critical
arm-unknown-eabi-ld callasm.o print.o -o callasm.elf
arm-unknown-eabi-objdump -D callasm.elf -marm > callasm.elf.list
arm-unknown-eabi-objcopy -S -O binary callasm.elf callasm.bin
arm-unknown-eabi-objdump -D -d callasm.bin -b binary -marm > callasm.bin.list
echo -e "\n\nHexdump\n" >> callasm.bin.list
hexdump -C callasm.bin >> callasm.bin.list
qemu-system-arm -M versatilepb -m 128M -nographic -kernel callasm.bin
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
���� ������QEMU: Terminated
.global myprint
@ Procedure to print a NULL terminated string through the UART
myprint:
push {r11} @Start of the prologue. Saving Frame Pointer onto the stack
add r11, sp, #0 @Setting up the bottom of the stack frame
sub sp, sp, #12 @End of the prologue. Allocating some buffer (12 bytes) on the stack
ldr r4, =0x101f1000 @This address is UART0 in Versatile/PB
ldr r5, [r0] @Load the input string, its in r0 as is the 1st parameter
ldr r6, [r5] @Load the content of the address pointed by r5 on r6
_printchr:
str r6, [r4] @Print throught the UART
add r5, r5, #1 @Point to the next character
ldr r6, [r5] @Load the character to r6
cmp r6, #0 @Check if its the last one
bne _printchr
add sp, r11, #0 @Start of the epilogue. Readjusting the Stack Pointer
pop {r11} @restoring frame pointer
bx lr @End of the epilogue. Jumping back to main via LR register
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment