ARM机器码,18个字节
十六进制转储(小端):
3803 d105 6808 ebc0 2010 b280 f2a0 1001 4770
此函数需要一个长度为字符串的指针对。输出为bash样式,其输出0为true,为非零值。在C语言中,该函数将声明为int oneTwoThree(size_t长度,char *字符串)。指令编码为thumb-2,具有2和4字节指令。在Raspberry Pi 3上测试。
非高尔夫装配:
.syntax unified
.text
.global oneTwoThree
.thumb_func
oneTwoThree:
@Input: r0 - the number of characters in the string
@r1 - A pointer to the (not necessarily NUL-terminated)
@string representation of the number (char*)
@Output: r1 - 0 if the number is in 123,234,...,789, else non-zero (bash-style)
subs r0,r0,#3
bne end @Return non-zero if r0!=3
ldr r0,[r1] @Remember that this is little endian
@So the first digit is the most siginificant byte
@I.e. if the input was 123 then r0 contains 0xXY010203 where XY is garbage
rsb r0,r0,r0,lsr #8 @r0=(r0>>8)-r0 (rsb is reverse subtract)
uxth r0,r0 @r0&=((1<<16)-1) (mask off top half)
@Now r0 is 0x0101 iff we have a matching number
sub r0,r0,#0x101
@Now r0 is 0 iff the string fit the specification
end:
bx lr @return
测试脚本(也是汇编):
.syntax unified
.text
.global main
.thumb_func
main:
push {r4,lr}
ldr r4,[r1,#4] @r0=argv[1]
mov r0,r4
bl strlen
@Now r0 is the length of the string argv[1]
mov r1,r4
bl oneTwoThree @oneTwoThree(strlen(argv[1]),argv[1])
cmp r0,#0
it ne
movne r0,#1 @Output through return code, 1 if false
pop {r4,pc}