Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a small routine [ARM]

Status
Not open for further replies.

JonnyS183

Programmer
May 11, 2007
1
GB
Hi I have to write a small subroutine which sorts a name alphabetically. Now until today I've never used ARM and have only written a tiny bit of x86 assembler code, so as you can imagine its been a long day!
Anyway I coded an example in C and then I thought I'd translate it but after 4 hours of debugging I can't see why! Can anyone scan through and point out where I have gone wrong?
The routine is below:
sortstring
MOV r0, r1 ; save the contents of r1
MOV r2,#12 ; the size of the string
MOV r3,#1 ; the counter for the outer loop
MOV r8,#0 ; variable that holds if a swap has occured
mainloop
CMP r3, r2 ; see if the loop counter matches the string size
BGE endsort ; jump to the end of the subroutine
MOV r9, #0 ; set the inner loop counter
MOV r10, #12
SUB r10, r10, #r3
sortloop
CMP r9, r10 ; see if r9 and r10 match
BGE endloop ; if r9 >= r10 then exit this loop
LDRB r4,[r1] ; load a byte (or character) into r4
LDRB r5,[r1,#1] ; load the byte next to r4 into r5
CMP r4,r5
BLE skipsort ; if r4 > r5
MOV r6, r4 ; move r4 into a temporary register
MOV r4, r5 ; put r5 into r4
MOV r5, r6 ; put r4 (stored in r6) into r5
STRB r4,[r1] ; write the updated charaters to srcstring
STRB r5,[r1,#1]
MOV r8, #1 ; indicatea a swap has occured
skipsort
ADD r1, r1, #1 ; increment the address that r1 points to
ADD r9,r9,#1 ; increment the inner loop counter
B sortloop ; jump back to the top
endloop
CMP r8,#0
BEQ endsort ; if no swap occured finish the program
ADD r3,r3,#1 ; increase the outer-loop counter
B mainloop ; jump to the main loop
endsort
MOV r1,r0 ; restore r1
MOV pc, lr ; return

cheers for your help

Jon
 
Given that you already have 'C' code which works, consider this approach.

If you do something like [tt]gcc -S prog.c[/tt], the compiler will produce prog.s containing the asm instructions for the 'C' code. Even if this is for the wrong processor, it will be a lot simpler to perform mnemonic translation from one processor to another.



--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top