Permalink
Browse files

initial import

  • Loading branch information...
jserv committed May 2, 2017
0 parents commit 6a35b0606b3cec95400e43e59d8ee6aa30806682
Showing with 137 additions and 0 deletions.
  1. +10 −0 Makefile
  2. +49 −0 bubble.s
  3. +45 −0 main.s
  4. +33 −0 vprintw.s
@@ -0,0 +1,10 @@
all: bubble
bubble:
arm-linux-gnueabihf-gcc -o bubble bubble.s main.s vprintw.s -static
clean:
rm -f bubble
run: bubble
@qemu-arm bubble || echo "OK!"
@@ -0,0 +1,49 @@
# inputs
# r0 - start of vector
# r1 - number of elements to sort
#
# no outputs
#
# locals
# r4 - current pointer r5 - inner counter
# r6 - keep_going flag r7 - first element
# r8 - second element
.equ element_size, 4
.globl bubble
.text
bubble:
stmfd sp!, {r4, r5, r6, r7, r8, lr} @ save variables to stack
cmp r1, #1 @ number of elements must be > 1
ble EndFunc @ stop if nothing to do
sub r5, r1, #1 @ need n-1 comparisons
mov r4, r0 @ initialize current pointer
mov r6, #0 @ this register set when we swap
LoopStart:
ldr r7, [r4], #element_size @ load one element
ldr r8, [r4] @ and next one
cmp r7, r8 @ compare them
ble NoSwap @ branch if second greater
mov r6, #1 @ set keep_going flag
??? r4, r4, #element_size @ reset pointer to first element
swp r8, r8, [r4] @ exchange value in r8 and address in r4
str r8, [r4, #element_size]! @ store new r8 to incremented address
NoSwap:
subs r5, r5, #1 @ decrement counter
??? LoopStart @ and restart loop if more needed
EndInner:
cmp r6, #0 @ check keep_going flag
beq EndFunc @ and leave if not set
mov r6, #0 @ clear keep_going flag
mov r4, r0 @ reset pointer
sub r5, r1, #1 @ reset counter
b LoopStart @ start another iteration
EndFunc:
??? sp!, {r4, r5, r6, r7, r8, pc} @ restore state from stack and leave function
45 main.s
@@ -0,0 +1,45 @@
.globl main
.equ items, 12
.equ datum_size,4
.section .rodata
before:
.asciz "Before sorting, values are : %d"
after:
.asciz "After sorting, values are : %d"
comma:
.asciz ", %d"
new_line:
.asciz "\n"
ok:
.asciz "ok\n"
.section .data
values:
.word 10, -77, 23, 6, 28, 31, 46, 63, 134, 97, 21, 53
evalues:
.word 1, 2, 3
.text
main:
ldr r0, =values
mov r1, #items
ldr r2, =before
ldr r3, =comma
bl _vprintw
ldr r0, =new_line
bl printf
ldr r0, =values
mov r1, #items
bl bubble
ldr r0, =values
mov r1, #items
ldr r2, =after
ldr r3, =comma
bl _vprintw
ldr r0, =new_line
bl printf
mov r7, #1 @ set r7 to 1 - the syscall for exit
swi 0 @ then invoke the syscall from linux
@@ -0,0 +1,33 @@
# print vectors of words
#
# inputs
# r0 - start of vector
# r1 - number of elements to print
# r2 - pointer to start of string used to print first element
# r3 - pointer to start of string used to print subsequent elements
#
# no outputs
.globl _vprintw
.equ datum_size,4
_vprintw:
stmfd sp!, {r4, r5, r6, r7, lr} @ save registers on the stack
cmp r1, #0 @ exit if no elements
ble last
mov r4, r0 @ copy the parameters to locals
mov r5, r1
mov r6, r2
mov r7, r3
ldr r1, [r4], #datum_size @ load first vector element to r0 and bump pointer
mov r0, r6 @ address of first string to r0
bl printf @ and print itt
subs r5, r5, #1 @ decrement counter
beq last @ and fall out if zero
vprintw_loop:
ldr r1, [r4], #datum_size @ load next vector item to r0 and bump pointer
mov r0, r7 @ address of subsequent string to r0
bl printf @ and print it
subs r5, r5, #1 @ decrement counter
bne vprintw_loop @ and loop if non-zero
last:
ldmfd sp!, {r4, r5, r6, r7, pc} @ restore registers from stack and return

0 comments on commit 6a35b06

Please sign in to comment.