ARM机器码,26个字节
十六进制转储(小端):
6810 4601 f852 cb04 4560 bfc8 4660 4561 bfb8 4661 3b01 d8f5 4770
这是一个函数,没有系统调用或库依赖性。编码为Thumb-2,这是用于32位ARM的变量(2或4字节)编码。可以想象,没有简单的方法可以对此处的第一个和最后一个元素进行排序和选择。总的来说,这里没有什么花哨的事情,它与参考实现大致相同。
非高尔夫装配(GNU语法):
.syntax unified
.text
.global minmax
.thumb_func
minmax:
@Input: @r0 and r1 are dummy parameters (they don't do anything)
@r2 - Pointer to list of integers (int*)
@r3 - Number of integers to sort (size_t)
@Output:
@Minimum of the list in r0 (int)
@Maximum in r1 (int)
ldr r0,[r2] @min=r2[0]
mov r1,r0 @max=min
loop:
@ip is intra-procedure call register, a.k.a. r12
ldr ip,[r2],#4 @ip=*r2++
cmp r0,ip
it gt @if (r0>ip)
movgt r0,ip @r0=ip
cmp r1,ip
it lt @if (r1<ip)
movlt r1,ip @r1=ip
subs r3,r3,#1
bhi loop @while (--r3>0)
bx lr @Return
在Raspberry Pi 3上测试;这是测试脚本(C99,通过argv输入):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
//First 2 arguments are dummies.
uint64_t minmax(int,int,int* array,size_t size);
int main(int argc,char** argv) {
int i;
int array[argc-1];
for (i=1;i<argc;i++) {
array[i-1]=atoi(argv[i]);
}
uint64_t result = minmax(0,0,array,argc-1);
printf("Minimum is %d, maximum is %d.\n",(unsigned)result,(unsigned)(result>>32));
}