📄 bubble.c
字号:
/* * Example: Bubble Sort (bubble.c): ColdFire target. * * bubble.c Includes: * function "main" - generated in default ".text" section; * function "sort" - generated in ".text2" section; * assembler macro "get_short"; * data "array" - copied from ROM to RAM for execution; * data "input_count" at location known during compilation. * swap.s Assembly function generated in ".text2" section. * crt0.s Assembly startup and termination module. */#include <stdio.h>extern void swap (int array[]);/* "I/O registers" at address known during compilation. */#pragma section IOREG far-absolute RW address=0xf0000#pragma use_section IOREG input_count short input_count; static int test_count; /* Uninitialized variable in .bss. */ int array[] = { /* Initialized variable in */ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 /* .data section. */ };asm short get_short (short *address_p) /* Assembler macro: read and */{ /* byte-swap a 2-byte I/O port. */% mem address_p; /* Parameter passing method. */ move.w (address_p),d1 /* Get I/O port value to d1. */ move.b d1,d0 /* Value's msb to d0. */ lsl.l #8,d0 /* Make it the msb in d0. */ lsr.l #8,d1 /* Make value's lsb the lsb in d1. */ move.b d1,d0 /* Combine bytes. */}/* Generate subsequent code in section ".text2". */#pragma section CODE ".text2"static void sort (int array [], int count){ int change = 1; int i; while (change) { change = 0; for (i = 0; i < count - 1; i++) { test_count ++; if (array[i] > array[i+1]) { swap (& array[i]); change = 1; } } }}/* Switch code generation back to default ".text" section, */#pragma section CODEvoid main (void){ int count; int i; count = get_short (& input_count); printf ("\nInitial array: "); for (i = 0; i < count; i++) printf ("%d ", array[i]); sort (array, count); printf ("\n\nNumber of tests: %d\n\nSorted array: ", test_count); for (i = 0; i < count; i++) printf ("%d ", array[i]); printf ("\n\n"); exit (0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -