Earlier quoted context omitted.
No, loop decrements rcx until it reaches zero and then stops
But rcx has not been initialised.
Ask HN: What are some examples of beautiful x86 assembly code?
31–40 of 93 posts
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#32Earlier quoted context omitted.
Wow it's close indeed, even stack manipulation looks the same. Would be MOV R0, -(SP) .... MOV (SP)+, R0 for the PDP code.
My faster (?, see [0]) strlen seems to compile (using MIT 68k syntax): > cat > test.asm strlen: movem.l %a0-%a1,-(%sp) movl %a0, %a1 ;# copy a0 to a1 slenloop: tst.b (%a0)+ bne.b slenloop addq.l #1, %a1 sub.l %a0, %a1 move.l %a1, %d0 movem.l (%sp)+,%a0-%a1 rts ^D > m68k-linux-gnu-as test.asm && m68k-linux-gnu-objdump -d a.out a.out: file format elf32-m68k Disassembly of section .text: 00000000 : 0: 48e7 00c0 moveml %…
But that gave me a new idea: copy pointer to d0 and complement the difference instead of addq, saving a1 register (+stack operations) and a move-instruction:
strlen: move.l %a0,-(%sp)
movl %a0, %d0 ;# d0 = a0;
slenloop: tst.b (%a0)+ ;# test *a0, post incr
bneb slenloop ;# loop if non-zero
sub.l %a0, %d0 ;# d0 = d0 - a0;
not.l %d0 ;# d0 = ~d0;
move.l (%sp)+,%a0
rts ;# d0 is the return value
Equivalent C-code (http://cpp.sh/2oecd): #include
#include
size_t strlen68k(char* a0) {
int zero_flag;
uintptr_t d0 = (uintptr_t) a0;
do {
// tstb (%a0)+
if (*a0 == 0)
zero_flag = 1;
else
zero_flag = 0;
a0++; // (%a0)+ (post increment)
} while(!zero_flag); // bneb slenloop
d0 = d0 - (uintptr_t) a0; // sub.l %a0, %d0
d0 = ~d0; // not.l %d0
return d0;
}
void strlen_test(char* str) {
printf("str \"%s\" length is %lu\n", str, strlen68k(str));
}
int main(void) {
strlen_test("");
strlen_test("a");
strlen_test("abcd");
return 0;
}
Again, untested, but I think it probably works. I guess this is also faster for all string lengths >0 as well.This idea might not work on PDP-11 anymore, depending on how binary arithmetic is implemented.
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#33Re: Ask HN: What are some examples of beautiful x86 assembly code?
#34My interest, save for occasional tweak in ARM boot code, is mostly historical. Grew up on Z-80 assembly but really enjoy PDP-11 code for didactic purposes. You can easily see that the rise of optimising C compilers was helped by divergence from PDP instruction set. Not ever since C idioms map down so nicely. strlen(): LEN: MOV #-1, R0 1⊙ INC R0 TSTB (R1)+ BNE 1⊙ or take strcpy(): COPY: MOVB (R1)+, (R0)+ BNE COPY vers…
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#35Earlier quoted context omitted.
But rcx has not been initialised.
If all functions took (rax, rbx, rcx, ...) and you had loop statement that defrements rcx and jumps unless zero, then how would you write fib-function’s body?
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#36disassembly of Pokémon Red/Blue
https://github.com/pret/pokered
TONC GBA Programming Principles
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#37Re: Ask HN: What are some examples of beautiful x86 assembly code?
#38https://gist.githubusercontent.com/jwieder/7e7e643cc71c81f63...
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#39Pokémon! GBA (ARM7) is even being used in some undergrad level OS classes. Although you may get more mileage out of RP3 development. disassembly of Pokémon Red/Blue https://github.com/pret/pokered TONC GBA Programming Principles https://www.coranac.com/tonc/text/toc.htm
Re: Ask HN: What are some examples of beautiful x86 assembly code?
#40This Defcon 18 talk called 'Trolling with Math' about trying to defeat reverse engineering with assembly tricks https://youtu.be/y124L75ZKAc