📄 bubble.c
字号:
/* * Example: Bubble Sort (bubble.c): SH 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 get_short(short *address_p) /* Assembler macro: read and */{ /* byte-swap a 2-byte I/O port. */% reg address_p; /* Parameter passing method. */! "r0", "r1" /* Indicate scratch registes used. */ mov.l @address_p,r1 /* Get I/O port value to r1. */ extu.b r1,r0 /* Value's msb to return reg r0. */ shll8 r0 /* Value's msb in place in r0. */ shlr8 r1 /* Value's lsb to lsb in r1. */ extu.b r1,r1 /* Mask off lsb in r1. */ add r1,r0 /* Combine bytes in return reg r0. */}/* 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 + -