Jump to content

Need help from expert. Simple program sorting arrays from low to highest.

Recommended Posts

For some reason I can't figure out how to do this properly, I been thinking about this for at least a week, I'd really appreciate any help I could get. I need to sort the array1 and put it in array1s, the same thing with array2 and putting the sorted array in array2s.

Here's what I have:


           .text                           ; Assemble into program memory
            .retain                         ; Override ELF conditional linking
                                            ; and retain current section
            .retainrefs                     ; Additionally retain any sections
                                            ; that have references to current

                                         ; section
ARY1 .set  0x0200
ARY1S .set 0x0210
ARY2 .set 0x0220
ARY2S .set 0x0230

RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
StopWDT     mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

ClEAR clr r4
clr r5
clr r6

Sort1 mov.w #AR1,R4
mov.b @R4+,R5
mov.w #AR1S,R6
call #Sort

Sort2 mov.w #AR2,R4
mov.b @R4+,R5
mov.w #AR2,R6
call #Sort

Sort clr r7
clr r8
clr r9
clr r10
clr r11
clr r12
clr r13
clr r14
clr r15

clr r10
mov.b R5,R11
dec R11

mov.b @R4+,R7
mov.b @R4,R8
cmp.b R7,R8
jz No_Swap
jge No_Swap

Swap mov.b R7,R9
mov.b R8,R7
mov.b R9,R8
inc R10
mov.b R8,0(R4)
mov.b R7,-1(R4)

No_Swap dec R11
tst R11
jnz Swap
cmp.b #0,R10
jnz Swap

Mainloop jmp Mainloop

ARRAY1SET mov.b   #10,0(R4)
mov.b #20,1(R4)
mov.b #89,2(R4)
mov.b #-5,3(R4)
mov.b #13,4(R4)
mov.b #63,5(R4)
mov.b #-1,6(R4)
mov.b #88,7(R4)
mov.b #2,8(R4)
mov.b #-88,9(R4)
mov.b #1,10(R4)

ARRAY2SET mov.b #10,0(R4)
mov.b #90,1(R4)
mov.b #-10,2(R4)
mov.b #-45,3(R4)
mov.b #25,4(R4)
mov.b #-46,5(R4)
mov.b #-8,6(R4)
mov.b #99,7(R4)
mov.b #20,8(R4)
mov.b #0,9(R4)
mov.b #-64,10(R4)
Help please.
-Daniel R
Edited by bluehash
Please use code tags next time to improve code readability.
Link to post
Share on other sites

Ok... wow...


First: the teacher will come out


Use the code tag (the <> at the top of the window) to preserve formatting for code. It also helps preserve sanity....


What algorithm are you trying to implement (insertion sort? selection sort? Bubble (shudder) sort?)


Also, comments help a lot to indicate what you are trying to do, and help YOU to see problems as you write the code




THat said, Your loops (the jnz at the end of the inner and the outer) are both jumping to the Swap label, and you are never doing the comparison after the first time. This is the core problem.


THis is, in part why I ask what algorithm you are trying to implement, since the index handling and comparison/exchange schedule are not the same with different algorithms, so I can not help you fix the problem without more info.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...