printfs syscall in ARM assembly

You can assemble and link it with

arm-linux-gnueabi-gcc --static hello.S -o hello
.data
LC0:
	.ascii  "%d\n"
.text
.align  2

.globl main
main:
	stmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, lr}	@ save the registers we use to the stack
	movs	r4, $0
loop:
	ldr     r3, L3
	movs	r0, r3
	movs	r1, r4
	bl	printf

	adds	r4, r4,$1
	cmp	r4, $100
	bne	loop

exit:
	ldmfd	sp!, {r0, r1, r2, r3, r4, r5, r6, pc}	@ restore registers before exit
	movs	r7, $1                  @ set r7 to 1 - the syscall for exit
	swi	0                       @ then invoke the syscall from linux

L3:
	.word	LC0
	.size	main, .-main

Leave a Reply

Your email address will not be published. Required fields are marked *