Yeshvanth Muniraj
Published

C vs. Assembly on ARM Microcontroller

A cool experiment to compare the results of Assembly code programmed by Human vs. Assembly generated by the ARM compiler

BeginnerProtip1 hour857
C vs. Assembly on ARM Microcontroller

Things used in this project

Hardware components

STM32 Nucleo-64 Board
STMicroelectronics STM32 Nucleo-64 Board
×1

Software apps and online services

Arm Keil MDK

Story

Read more

Code

main.s

Assembly x86
Assembly File containing code for bubble sort
	AREA _data, DATA
		
ADDR	EQU 0x20000000
	
	AREA _code, CODE
		ENTRY
		EXPORT __main
			
__main
	MOV r0, r0
	MOV r0, r1
	MOV r0, r2
	MOV r1, r1
	MOV r1, r0
	MOV r1, r2
	MOV r2, r0
	LDR r0, =ADDR
	MOV r3, #0
loop_1 
	ADD r4, R3, #1
loop_2
	LDRB r1, [r0, r3]
	LDRB r2, [r0, r4]
	CMP r1, r2
	BCC branch
	STRB r1, [r0, r4]
	STRB r2, [r0, r3]
branch
	ADD r4, r4, #1
	CMP r4, #10
	BNE loop_2
	ADD r3, r3, #1
	CMP r3, #9
	BNE loop_1

stop	B stop

	END

main.c

C/C++
C code containing code for bubble sort
#include <stdint.h>

#define SRAM_ADDR		0x20000000

int main() {
		uint8_t *ptr = (uint8_t*)SRAM_ADDR;
		uint8_t i, j, temp;
		
		for(i = 0; i < 9; i++) {
				for(j = i + 1; j < 10; j++) {
						if( *(ptr + i) > *(ptr + j)) { 
								temp = *(ptr + i);
								*(ptr + i) = *(ptr + j);
								*(ptr + j) = temp;
						}
				}
		}
		return 0;
}

Credits

Yeshvanth Muniraj

Yeshvanth Muniraj

20 projects • 35 followers
Hands-on experience in Embedded Systems and IoT. Good knowledge of FPGAs and Microcontrollers.

Comments