DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • How To Optimize Feature Sets With Genetic Algorithms
  • Crafting Mazes
  • AI in Edge Computing: Implementing Algorithms to Enhance Real-Time
  • The Power of AI: Why Web Developers Still Reign Supreme

Trending

  • Getting Started With Prometheus Workshop: Instrumenting Applications
  • Revolutionizing Software Testing
  • Agile Metrics and KPIs in Action
  • Java Parallel GC Tuning
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Implementing Simple Sort Algorithms in ARM Assembly

Implementing Simple Sort Algorithms in ARM Assembly

Create your own basic algorithms using assembly for your Raspberry Pi

Kevin Hooke user avatar by
Kevin Hooke
·
Sep. 08, 15 · Tutorial
Like (1)
Save
Tweet
Share
10.57K Views

Join the DZone community and get the full member experience.

Join For Free

I finished the first rough version of my simple sort algorithm in ARM Assembly (see part 1 and part 2 of my updates). Here it is so far (prior to some cleanup and optimization):

/*
R0 address of string used with printf ti output %d
R4 address of numbers to sort
R5 current number to be compared
R6 offset index for outer loop through numbers
R7 offset index for inner loop
R8 current smallest identified value
R9 current offset index of next uncompared value
*/
.global main
main:
  push {ip, lr}
  MOV R6, #0 @outerloop offset to numbers to be sorted
  MOV R7, #0 @innerloop offers to number to be sorted
  MOV R9, #0 @init value for index to next uncompared value
outerLoop:
  MOV R8, #99 @reset large default for next loop comparison
  MOV R7,R6 @copy outerloop offset to next starting offset for the innerloop
innerLoop:
  LDR R0, =output @load addr of output string
  LDR R4, =nums @load addr of nums to compare to R4
  LDR R5,[R4,R7] @load current num to R5 from R4 with offset R7
  MOV R1,R5 @move num for output
  BL printf
  CMP R5,R8 @is current < smallest so far
  BLT swapSmallest @if true, swap smallest to current first position then continue
continue:
  CMP R7,#16 @ 0 plus 4*4bytes for 5 entries in array
  ADD R7, R7,#4 @inc offset by 4 bytes
  BLT innerLoop
continueOuterLoop:
  CMP R6, #16 @check if we've looped through all values
  ADD R6, R6, #4
BLT outerLoop @if not, branch back to start of outer loop
_exit:
  POP {ip, lr}
resetLoopOffsets:
  MOV R7, #0 @reset loop counter
writeFinalSoredList: @TODO: this is a near copy of the innner loop - refactor this to function
  LDR R0, =writeSorted @load addr of output string
  LDR R4, =nums @load addr of nums
  LDR R5,[R4,R7] @load current num to R5 from R4 with offset R7
  MOV R1,R5 @move num for output
  BL printf
  CMP R7,#16 @ 0 plus 4*4bytes for 5 entries in array
  ADD R7, R7,#4 @inc offset by 4 bytes
  BLT writeFinalSoredList
doExit:
  MOV R1, #0
  MOV R7, #1
  SWI 0
swapSmallest:
  MOV R8,R5 @keep copy of smallest in the current loop
  LDR R10, [R4,R6] @tmp copy first position to R10
  LDR R11, [R4,R7] @tmp copy value in position currently being compared
  STR R10, [R4, +R7] @swap first position value to current position being compared
  STR R11, [R4, +R6] @swap the current smallest value into the current first position
  BX lr @return
.data
nums:
.word 5,2,7,1,8
output:
.asciz "%d\n"
writeSorted:
.asciz "%d\n"


Complete source if you want to grab a copy is in github here.

To get this far I learned plenty about ARM architecture – over time it has evolved and there are many different versions, and different ARM based CPUs implement different architecture versions. To make things more complicated, the naming scheme is a bit confusing.

The ARM CPU in the Raspberry Pi is a Broadcom BCM2835 System on a Chip (SoC), which includes an ARM1176JZF-S (ARM reference manual here). This is an ARM11 core, based on ARMv6 architecture.

Interest points about the ARMv6 instructions (not a comprehensive summary, but some rough notes to refer back to later):

  • The majority of instructions are structured ‘instruction destination, source’ but the STR (Store) for some reason is reversed so it is ‘instruction source, destination’
  • LDR (Load Register), can take a source as a label to a constant, or prefixed with ‘=’ which takes the address in memory where the constant is located.
  • LDR can move the value that is pointed to by an address in another register, using [Rn], and can also be coupled with an offset as a second argument, [Rn, Rm]

I’ll probably spend some time to see if I can clean up the code some more, but I’m happy with this so far.

Arm (geography) Sort (Unix) Assembly (CLI) Algorithm

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Optimize Feature Sets With Genetic Algorithms
  • Crafting Mazes
  • AI in Edge Computing: Implementing Algorithms to Enhance Real-Time
  • The Power of AI: Why Web Developers Still Reign Supreme

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: