Assembly code is fun…it only took me 3 or so hours to complete an assignment due today. I should’ve started much, much earlier on Sunday, but that didn’t sound like any fun, so piddled around all day. And yes, I know I’ll pay for it tomorrow from lack of sleep, but I think I made up for it…I got about 10 hours of sleep on Sunday, so I think I’ll be all good. If nothing else I can sleep standing, or sitting in class…
Anyway, I had to create a program that does Bubblesort on 8-bit signed/unsigned numbers depending on which program you run.
Bubblesort means you pass the entries multiple times…like if you have the list: 7 4 5 3 9
The first run results in: 4 5 3 7 9
The second run: 4 3 5 7 9
And lastly: 3 4 5 7 9
Anyway, it’s a pain to do in assembly, but if you’d like to see the code it’s in the extended entry.
This is the unsigned version.
Anything after a semi-colon is a comment and it appears as if MT has stripped out my formatting, so you’re on your own.
You’re welcome to use my code, just be sure to credit me. Thank you.
;*
;* Title: bubblesort.s
;* — M68HC12 Assembly code: Sort the list in asecending order
;*
;* Mate: 28 March 2004
;*
;*
;* Author: Luke@evilmonkeycult.com
;*
;* Description:
;*————–
;* Bubblesort: Make passes sorting a numeric list from least to greatest
;* Use a “swap” flag to note if you have changed the order of the entries
;* during a pass.
;*
;*
;* NOTE: THIS PROGRAM DOES NOT PRESERVE REGISTERS
;*
SWITCH .data
DATLIST: FCB $2C,$84,$55,$00,$A5,$FE,$72,$84,$32,$2C
SWITCH .text
TESTPGM: LDS #$A00 ;FIRST BYTE PUSHED GOES INTO $9FF
LDAA #10
PSHA ;PUSH NUMBER OF BYTES TO SORT
LDD #DATLIST
PSHD ;PUSH STARTING ADDR OF DATA LIST
JSR UNSIGNED_SORT
LEAS 3,S ;CLEAN INPUT ARGUMENTS OFF STACK
SWI ;BY ADDING 3 TO SP. THE SWI INSTRUCITON
;GENERATES A SOFTWARE INTERRUPT THAT FORCES
;A RETURN TO THE DBUG12 MONITOR PGM.
UNSIGNED_SORT:
LDAB 4,SP ;LOAD B WITH AMOUNT OF ENTRIES
LDX #0000
ABX ;LOAD X WITH AMOUNT OF ENTRIES
LDY 2,SP ;LOAD Y WITH ADDRESS OF FIRST ENTRY
ABY
CLR Y ;CLEAR MEMORY AFTER ENTRIES FOR SWAP BIT
LDY 2,SP ;LOAD Y WITH ADDRESS OF FIRST ENTRY
DEX ;ACCOUNTING FOR ONE ALWAYS BEING COMPARED
SORT_LOOP:
LDAA Y ;LOAD A WITH LOWER ENTRY
LDAB 1,Y ;LOAD B WITH NEXT ENTRY
CBA ;COMPARE A AND B: IF A=B:Z=0, A>B:C=0, A
SWAPING:
EXG A,B ;EXCHANGE A AND B
STAA Y ;STORE THE NEW A AS THE SMALLER ENTRY
STAB 1,Y ;STORE THE NEW B AS THE LARGER ENTRY
PSHY ;ADD Y COUNTER TO STACK FOR SAFE KEEPING
LDAB 6,SP ;LOAD B WITH AMOUNT OF ENTRIES
LDY 4,SP ;LOAD Y WITH ADDRESS OF FIRST ENTRY
ABY
BSET Y,%0001 ;SET THE SWAP BIT
PULY ;RESTORE Y COUNTER
DOWNX:
INY ;INCREMENT Y FOR NEXT ENTRY SET
DEX
BNE SORT_LOOP ;RETURN UNTIL 0 TO SORT_LOOP
;TO CONTINUE WITH THE ENTRIES
LDAB 4,SP
LDY 2,SP
BRSET B,Y,$01,UNSIGNED_SORT
;CONTINUE LOOPING UNTIL SWAPPING IS FINISHED
RTS
END
March 29th, 2004 at 2:29 pm
Everyone should program in assembly once. Nobody should do it again…
March 29th, 2004 at 3:44 pm
Assembly is so much better than Java though…because I understand assembly.
April 1st, 2004 at 1:05 am
So do I, it is like chewing glass