Created
October 20, 2018 15:32
-
-
Save MelulekiDube/4c864fedf2f36f72c27a078635b581af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* we are going to use register r0 and r3 for indexes*/ | |
/* we will then use register r4 and r5 as temporal registers*/ | |
.global main | |
.func main | |
main: | |
MOV R0, #0 @ initialze index variable | |
mov r3, #0 | |
ldr r3, =a | |
.loadArray: | |
ldr r2, [fp, #-8] | |
ldr r3, [fp, #-20] | |
cmp r2, r3 | |
bge loadDone | |
ldr r3, [fp, #-8] | |
lsl r3, r3, #2 | |
ldr r2, [fp, #-16] | |
add r3, r2, r3 | |
ldr r2, [fp, #-8] | |
rsb r2, r2, #10 | |
str r2, [r3] | |
ldr r3, [fp, #-8] | |
add r3, r3, #1 | |
str r3, [fp, #-8] | |
b .loadArray | |
loadDone: | |
MOV r0, #0 | |
MOV r1, #0 | |
MOV r2, #0 | |
MOV r3, #0 | |
outerloop: | |
CMP R0, #10 @ check to see if we are done iterating | |
BEQ sortdone @ exit loop if done | |
LDR R1, =a @ get address of a | |
MOV R2, R0, #2 @ multiply index*4 to get array offset | |
ADD R2, R1, R2 @ R2 now has the element address | |
MOV R6, R2 @ R6 is the index_of_min | |
MOV R3, R0 | |
ADD R3, R3, #1 | |
innerloop: | |
/*determine if we can still loop through*/ | |
CMP R3, #10 | |
BEQ swap | |
/*logic*/ | |
LSL R5, R3, #2 @ this is the j if it was to be done with a high level language | |
ADD R5, R1, R5 @ R5 now has the address the element[j] | |
CMP R5, R6 | |
BLE IF | |
IF: | |
MOV R6, R5 | |
/*end of logic*/ | |
/*incrementing*/ | |
ADD R3, R3, #1 | |
B innerloop | |
swap: | |
CMP R6, R2 | |
BNE if_swap | |
if_swap: | |
MOV R8, R6 | |
MOV R6, R2 | |
MOV R2 R8 | |
ADD R0, R0, #1 @ increment index | |
B outerloop @ branch to next loop iteration | |
sortdone: | |
MOV R0, #0 @ initialze index variable | |
readloop: | |
CMP R0, #10 @ check to see if we are done iterating | |
BEQ readdone @ exit loop if done | |
LDR R1, =a @ get address of a | |
LSL R2, R0, #2 @ multiply index*4 to get array offset | |
ADD R2, R1, R2 @ R2 now has the element address | |
LDR R1, [R2] @ read the array at address | |
PUSH {R0} @ backup register before printf | |
PUSH {R1} @ backup register before printf | |
PUSH {R2} @ backup register before printf | |
MOV R2, R1 @ move array value to R2 for printf | |
MOV R1, R0 @ move array index to R1 for printf | |
BL _printf @ branch to print procedure with return | |
POP {R2} @ restore register | |
POP {R1} @ restore register | |
POP {R0} @ restore register | |
ADD R0, R0, #1 @ increment index | |
B readloop @ branch to next loop iteration | |
readdone: | |
B _exit @ exit if done | |
_exit: | |
MOV R7, #4 @ write syscall, 4 | |
MOV R0, #1 @ output stream to monitor, 1 | |
MOV R2, #21 @ print string length | |
LDR R1, =exit_str @ string at label exit_str: | |
SWI 0 @ execute syscall | |
MOV R7, #1 @ terminate syscall, 1 | |
SWI 0 @ execute syscall | |
_printf: | |
PUSH {LR} @ store the return address | |
LDR R0, =printf_str @ R0 contains formatted string address | |
BL printf @ call printf | |
POP {PC} @ restore the stack pointer and return | |
.data | |
.balign 4 @we are aligning the array to 4 byte memory location./ thefore the next memory location has to be a memory divided by 4 | |
a: .skip 40 @ a is a label of an array skip is saying is saying skip n number of memory locations so our array is n/4 where 4 is the number of bytes per elemet | |
printf_str: .asciz "a[%d] = %d\n" | |
exit_str: .ascii "Terminating program.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment