I am targeting a Cortex-M0 processor, and am new to Thumb assembly available on the M0. Is there more efficient way to swap two bytes in a 32-bit word (in this example, the middle two bytes)? Thanks!
LDR R0, =0xAABBCCDD LDR R1, =0x00FF0000 LDR R2, =0x0000FF00 MOV R3, R0 MOV R4, R0 ANDS R3, R1 ANDS R4, R2 BICS R0, R1 BICS R0, R2 LSRS R3, #8 LSLS R4, #8 ORRS R0, R3 ORRS R0, R4
LDR R0, =0xAABBCCDD
LDR R1, =0x00FF0000
LDR R2, =0x0000FF00
MOV R3, R0
MOV R4, R0
ANDS R3, R1
ANDS R4, R2
BICS R0, R1
BICS R0, R2
LSRS R3, #8
LSLS R4, #8
ORRS R0, R3
ORRS R0, R4
Sounds like homework. ;-)
Hint: Can be done in 8 instructions.
LOL The only homework this 50 year old has is sweeping. I cut my teeth on a 6502 (OSI Challenger 2P, VIC-20, etc.) but have recently gotten back into assembly language for fun. There doesn't seem to be a Rodnay Zaks out there for the Cortex-M. Any recommendations for a practical (non-text-book) Cortex-M assembly language/patterns book?
And thanks for the homework :-). I'll see if I can't get it down to 8 instructions. I appreciate the challenge.
Still on 6502 myself (https://www.pouet.net/prod.php?which=85609) :-)No "tips and tricks" book AFAIK for Cortex-M, but Cortex-M0 is really simple as there are only 50 or so instructions.
Important advantage over 6502: Some instruction take three arguments.
Checkout "REV" ;-)
Anyway, I can always recommend this book:https://www.sciencedirect.com/book/9780124080829/the-definitive-guide-to-arm-cortex-m3-and-cortex-m4-processors
I think there is also one for Cortex-M0.
Also, checkout this forum for "multiply" for some tricks.
42Bastian Schick said:I can always recommend this book ...
I can always recommend this book ...
See also: http://www.keil.com/books/
Thank you! The reason I decided to go with the Cortex-M0 is because of that simplicity of learning. I appreciate the links to the books, will check them out, and thanks for the pointer to REV. Glad you didn't provide an answer... it's much more fun to figure things out.
And so cool you are making demos! Amazing what you can do in 248 bytes.
Thank you for that link! Will check out the books there.
Sensei, I was able to get it to 6 instructions using REV :-) Thank you!
LDR R0, =0xAABBCCDDLDR R1, =0x00FFFF00REV R2, R0 // R2 0xDDCCBBAAANDS R2, R1 // R2 0x00CCBB00BICS R0, R1 // R0 0xAA0000DDORRS R0, R2 // R0 0xAACCBBDD
Neat. Don't think it can be shorter.
Here is my version, which avoids the literal loading:
lsls r1,r0,#8 // r1 = bbccdd00
lsrs r1,r1,#16 // r1 = 00bbccdd
lsls r1,r1,#8 // r1 = 00bbcc00
eors r0,r1 // r0 = aa0000dd
rev16 r1,r1 // r1 = bb0000cc
rors r1,r2 // r1 = 00ccbb00
orrs r0,r1 // r0 = aaccbbdd
View all questions in Cortex-M / M-Profile forum