Skip to content

Instantly share code, notes, and snippets.

@MarcoPolo
Created September 25, 2012 04:31
Show Gist options
  • Save MarcoPolo/3779999 to your computer and use it in GitHub Desktop.
Save MarcoPolo/3779999 to your computer and use it in GitHub Desktop.
Bubble sort in freaking asm
;****************************************************************************************
; file = ex0.asm
; Quick examples of Assembler directives & 28F335 DSP code
; Dr. Karl Gugel, May/2009
;
; To be assembled using Code Composer Studio which requires a linker command
; file to tell CCR where to place code & data into DSP SRAM.
; The command file used = 28335_RAM_lnk.com
; Important Code locations:
; .text RAML1 (internal DSP memory) starting address = 09000 Hex, 4K Words
; .data RAML2 (internal DSP memory) starting address = 0A000 Hex, 4K Words
;
; Special Note:
; Assembler directives are used to place data and variables into memory.
; They are not F335 instructions and thus are not executed at "run-time".
; When this program is loaded into memory, the data (created above by the
; assembler) is also copied down into memory. This is called "load-time".
;
.global _c_int00 ;This assembler directive allows _c_int00 to be a
;global variable. This tells the linker where your
;program (.text code) begins and where to boot from.
;
; Additional References:
; Assembler Directives: spru513c.pdf
; Memory Map/Hardware Related: sprs439e.pdf (also called 320f28335.pdf)
; CPU Registers & Assembly Code: spru430.pdf (version E)
;****************************************************************************************
;***************************** Program Constants ****************************************
; Creating constants using the .set assembler directive. This should be at the top of your
; program. This is like a define statement in C.
num1 .set 0h ;assembly-time constant (hex number)
num2 .set 11110000b ;assembly-time constant (binary no.)
num3 .set 65535 ;assembly-time constant (decimal no.)
count .set 3 ;number of characters to add in EEL4744 ('E'+'E'+'L')
data_sect .set 0xa000 ;constant that is actually the starting addr of .data section
bss_sect .set 0xb000 ;constant that is actually the starting addr of .bss section
orig_addr .set 0xa00a
orig_len .set 12
sorted_addr .set 0xb00c
;****************************************************************************************
;******************* DATA ALLOCATION SECTION - Variables/Data ***************************
; Data can go before or after your program code but should not be placed in the middle
; nof a program for clarity reasons.
.data ;data section, see the command linker file, this puts the
;following data defined below in a block of internal SRAM
;starting at 0xA000.
spaces .space 160
;.score_addr
;score_vector .word 0
score_vector .word 100
.word 10
.word 15
.word 130
.word 50
.word 15
.word 35
.word 65
.word 8
.word 95
.word 200
.word 5
.word 0xFF ;Padding
;.BSS SECTION is used to reserve space in SRAM for run-time results.
; See the command linker file, the starting address is 0xB000
.bss results,3 ;reserves three words at label 'results' in the .bss section
.bss sum,1 ;reserves one word at label 'sum' in the .bss section
.bss max_addr, 1
.bss min_addr, 1
;.global directive lets you to see the assigned addresses in map file.
.global num1,num2,num3,num3,num4,counter,char,val1,val2,val3,results,sum,score_addr
;****************************************************************************************
;******************** Brief Introduction to CPU Model ***********************************
; CPU Registers:
; ACC Accumulator (32 bits) comprised of AH (upper 16 bits) and AL (lower 16 bits)
; XAR0 Auxiliary Register0 (32 bits) comprised of AR0H (upper 16 bits) and AR0 (lower 16 bits)
; XAR1 Auxiliary Register1 (32 bits) comprised of AR1H (upper 16 bits) and AR1 (lower 16 bits)
; XAR2 Auxiliary Register2 (32 bits) comprised of AR2H (upper 16 bits) and AR2 (lower 16 bits)
; XAR3 Auxiliary Register3 (32 bits) comprised of AR3H (upper 16 bits) and AR3 (lower 16 bits)
; XAR4 Auxiliary Register4 (32 bits) comprised of AR4H (upper 16 bits) and AR4 (lower 16 bits)
; XAR5 Auxiliary Register5 (32 bits) comprised of AR5H (upper 16 bits) and AR5 (lower 16 bits)
; XAR6 Auxiliary Register6 (32 bits) comprised of AR6H (upper 16 bits) and AR6 (lower 16 bits)
; XAR7 Auxiliary Register7 (32 bits) comprised of AR7H (upper 16 bits) and AR6 (lower 16 bits)
; XT Multiplicand Register (32 bits) comprised of T (upper 16 bits) and TL (lower 16 bits)
; P Product Register (32 bits) comprised PH (upper 16 bits) and PL (lower 16 bits)
; PC Program Counter (22 bits)
; SP Stack Pointer (16 bits)
; DP Data Page Register (16 bits)
; ST1,ST0 Status Registers (flags)
;****************************************************************************************
;****************** F335 Program Examples ***********************
.text ;Program section, see the command linker file, program code
;should be placed in the text section which starts at 0x9000
_c_int00: ;This label tells the linker where the entry (starting) point for
;the first instruction in your program.
MOV AR0, #orig_addr
MOV AR1, #orig_len
MOV AR2, #sorted_addr
PUSH #orig_addr
PUSH #orig_len
PUSH #sorted_addr
LC SORT_BITCHES ;INITIALIZE LED (OUTPUT, MUX = 00)
WAIT B WAIT, UNC ;WAIT FOR INTS
SORT_BITCHES:
POP AR0 ;First POP is 0000 because that the first part of the addr 00009000B (where the LC cane from) we'll just remember it is 0000
POP AR0 ;STACKPOINTER
POP AR1 ;Sorted Address
POP AR2 ;original Length
POP AR3 ;original address
STARTBUBBLES:
MOV AR4, AR2 ;copy the original length
MOV AR5, AR3; set starting point
SWAPMEMAYBE:
MOV AL, *AR5
INC AR5
MOV AH, *AR5
DEC AR5
SUB AH, AL
B SWAP, LT ; If we need to swap we will and then we will go to the top of the START bubble branch
DEC AR4 ;decrease the counter and check if we are done with it
B DONE, EQ
INC AR5 ; Read the next value and go on
B SWAPMEMAYBE, UNC
DONE:
LC COPYARRAY ;moves the sorted array to the sorted vector position corrupts regs 1-3 like it gives a fuck
PUSH AR0 ;Restore the stack pointer
PUSH #0000
LRET
COPYARRAY:
;copy values over
MOV AL, *AR3
MOV *AR1, AL
INC AR3
INC AR1
;decrease the count
DEC AR2
B COPYARRAY, NEQ
LRET
SWAP:
MOV AL, *AR5
INC AR5
MOV AH, *AR5
MOV *AR5, AL
DEC AR5
MOV *AR5, AH
B STARTBUBBLES, UNC
;ENDPROGRAM
endMAN b endMAN, UNC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment