\ bubble.sex \ \ Written 1/17/94 by Ruben Agin for us with the MSIM sexium simulator \ See bubble.asm for this version \ Modified 3/28/96 for use with the CAM-8 Sexium emulator \ Registers: \ R0: N \ R1: i \ R2: temp,swap \ R3: data[i] \ MAL: j \ \ The following program implements bubble sort which is actually selection \ sort, but it's much more efficient, so who really cares. 0 ORG \ Start LDM h# f DATA MAH PUT LDM h# ff DATA \ Load N into R0 R1 PUT MAL PUT LDA R0 PUT LDM h# 10 DATA \ Load in starting address $1000 MAH PUT MAH PUT 13 ORG \ Loop1: LDM h# 01 DATA R1 ADD MAL PUT \ Increment i,j R1 PUT LDI \ Load data[i], i++ R3 PUT R0 GET \ N = i ? NOT R2 PUT LDM h# 01 DATA R2 ADD R1 TST SKZ \ If so, done. JMP 300 ADDR \ If not, enter loop JMP 100 ADDR 100 ORG \ Loop2: R0 GET \ N < j ? NOT R2 PUT LDM h# 01 DATA R2 ADD MAL TST SKZ JMP 13 ADDR \ If so, go to loop1 LDA \ Load data[j], j++ NOT \ data[j] < data[i] ? R2 PUT LDM h# 01 DATA R2 ADD R3 TST SKZ JMP 200 ADDR \ If so, swap them. LDI JMP 100 ADDR 200 ORG \ Swap: R3 GET \ data[j]:=data[i] STA MAL GET \ MAL temped to R3 for return R3 PUT R1 GET \ Jump to data[i] MAL PUT R2 GET NOT STA \ data[i]:=data[j] R3 GET MAL PUT LDI \ Return to data[j], j++ R2 GET NOT R3 PUT JMP 100 ADDR 300 ORG \ Done: END