Skip to main content
deleted 331 characters in body
Source Link
davey
  • 421
  • 2
  • 7

Honestly a few bytes could probably be "legally" removed because code golf doesn't require full programs, only snippets, so a few of the values could be "assumed" to be initialized such as the first mov instruction into r12, but unless someone can come up with a close competing result in x86_64 machine code, I'll leave it be.

Here's a link to my first and second attempts, in their original nasm syntax.

Honestly a few bytes could probably be "legally" removed because code golf doesn't require full programs, only snippets, so a few of the values could be "assumed" to be initialized such as the first mov instruction into r12, but unless someone can come up with a close competing result in x86_64 machine code, I'll leave it be.

Here's a link to my first and second attempts, in their original nasm syntax.

Here's a link to my first and second attempts, in their original nasm syntax.

added 191 characters in body
Source Link
davey
  • 421
  • 2
  • 7

x86_64 machine code (linux), 175 9999 76 bytes

0000000000400080 <_start>:
  400080:   4166 bcbf 09 00 00 00           mov    $0x9,%r12d%di

00000000004000860000000000400084 <_table.L2>:
  400086400084:   6a 0a                   pushq  $0xa
  400088400086:   4d 89 e5fe                   mov    %r12%edi,%r13%esi

000000000040008b0000000000400088 <_table.L3>:
  40008b400088:   4c 89 e8f0                   mov    %r13%esi,%rax%eax
  40008e40008a:   49 f7 e4e7                   mul    %r12%edi

0000000000400091000000000040008c <_printInteger>:
  40009140008c:   6a 20                   pushq  $0x20
 
0000000000400093 <_printInteger.L1> 40008e:
   400093: 3c 0a 48 31 d2                xorcmp    %rdx$0xa,%rdx%al
  400096400090:   417d b802 0a 00 00 00       mov    $0xa,%r8d    jge    400094 <_printInteger.L1>
  40009c400092:   496a f720 f0                div  pushq  %r8$0x20

0000000000400094 <_printInteger.L1>:
 40009f 400094:   4866 8331 c2d2 30             add  xor  $0x30  %dx,%rdx%dx
  4000a3400097:   52 b3 0a                   mov push   %rdx$0xa,%bl
  4000a4400099:   4866 85f7 c0f3                testdiv   %rax,%rax %bx
  4000a740009c:   7583 eac2 30                add  jne  $0x30,%edx
  40009340009f: <_printInteger.L1>
  4000a9:52   6a 3d                  push pushq  $0x3d%rdx
  4000ab4000a0:   4966 8385 c4c0 30             add  test  $0x30 %ax,%r12%ax
  4000af4000a3:   4175 54ef                   pushjne   %r12 400094 <_printInteger.L1>
  4000b14000a5:   496a 833d ec 30             sub    $0x30,%r12pushq  $0x3d
  4000b54000a7:   6a66 7857                   pushqpush  $0x78 %di
  4000b74000a9:   4980 8304 c524 30             add addb   $0x30,%r13(%rsp)
  4000bb4000ad:   41 556a 78                  push pushq  %r13$0x78
  4000bd4000af:   4966 8356 ed 31             sub    $0x31,%r13
push  4000c1: %si
  4d4000b1: 85 ed 80 04 24 30            test addb  %r13 $0x30,%r13(%rsp)
  4000c44000b5:   75ff c5ce                   jne dec   40008b <_table.L3>%esi
  4000c64000b7:   4975 ffcf cc                dec  jne  %r12  400088 <_table.L3>
  4000c94000b9:   4dff 85cf e4                test  dec %r12,%r12   %edi
  4000cc4000bb:   75 b8c7                   jne    400086400084 <_table.L2>

00000000004000ce00000000004000bd <_printChars>:
  4000ce4000bd:   66 ba b800 0908 00 00           mov    $0x9b8$0x800,%edx%dx
  4000d34000c1:   b8b0 01 00 00 00                mov    $0x1,%eax%al
  4000d84000c3:   66 bf 01 00 00 00           mov    $0x1,%edi%di
  4000dd4000c7:   48 8d89 34e6 24             lea  mov  (  %rsp),%rsi
  4000e14000ca:   0f 05                   syscall

Here's a link to my firstfirst and secondsecond attempts, in their original nasm syntax.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes).

I welcome anyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes).

EDIT: Now prints extra spaces and is golfed down even more! It's doing some pretty crazy stuff with the registers, and is probably unstable if this program were to be expanded.

x86_64 machine code (linux), 175 99 bytes

0000000000400080 <_start>:
  400080:   41 bc 09 00 00 00       mov    $0x9,%r12d

0000000000400086 <_table.L2>:
  400086:   6a 0a                   pushq  $0xa
  400088:   4d 89 e5                mov    %r12,%r13

000000000040008b <_table.L3>:
  40008b:   4c 89 e8                mov    %r13,%rax
  40008e:   49 f7 e4                mul    %r12

0000000000400091 <_printInteger>:
  400091:   6a 20                   pushq  $0x20
 
0000000000400093 <_printInteger.L1>:
   400093:   48 31 d2                xor    %rdx,%rdx
  400096:   41 b8 0a 00 00 00       mov    $0xa,%r8d
  40009c:   49 f7 f0                div    %r8
  40009f:   48 83 c2 30             add    $0x30,%rdx
  4000a3:   52                      push   %rdx
  4000a4:   48 85 c0                test   %rax,%rax
  4000a7:   75 ea                   jne    400093 <_printInteger.L1>
  4000a9:   6a 3d                   pushq  $0x3d
  4000ab:   49 83 c4 30             add    $0x30,%r12
  4000af:   41 54                   push   %r12
  4000b1:   49 83 ec 30             sub    $0x30,%r12
  4000b5:   6a 78                   pushq  $0x78
  4000b7:   49 83 c5 30             add    $0x30,%r13
  4000bb:   41 55                   push   %r13
  4000bd:   49 83 ed 31             sub    $0x31,%r13
  4000c1:   4d 85 ed                test   %r13,%r13
  4000c4:   75 c5                   jne    40008b <_table.L3>
  4000c6:   49 ff cc                dec    %r12
  4000c9:   4d 85 e4                test   %r12,%r12
  4000cc:   75 b8                   jne    400086 <_table.L2>

00000000004000ce <_printChars>:
  4000ce:   ba b8 09 00 00          mov    $0x9b8,%edx
  4000d3:   b8 01 00 00 00          mov    $0x1,%eax
  4000d8:   bf 01 00 00 00          mov    $0x1,%edi
  4000dd:   48 8d 34 24             lea    (%rsp),%rsi
  4000e1:   0f 05                   syscall

Here's a link to my first and second attempts, in their original nasm syntax.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes).

I welcome anyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

x86_64 machine code (linux), 175 99 76 bytes

0000000000400080 <_start>:
  400080:   66 bf 09 00             mov    $0x9,%di

0000000000400084 <_table.L2>:
  400084:   6a 0a                   pushq  $0xa
  400086:   89 fe                   mov    %edi,%esi

0000000000400088 <_table.L3>:
  400088:   89 f0                   mov    %esi,%eax
  40008a:   f7 e7                   mul    %edi

000000000040008c <_printInteger>:
  40008c:   6a 20                   pushq  $0x20
  40008e:   3c 0a                   cmp    $0xa,%al
  400090:   7d 02                   jge    400094 <_printInteger.L1>
  400092:   6a 20                   pushq  $0x20

0000000000400094 <_printInteger.L1>:
  400094:   66 31 d2                xor    %dx,%dx
  400097:   b3 0a                   mov    $0xa,%bl
  400099:   66 f7 f3                div    %bx
  40009c:   83 c2 30                add    $0x30,%edx
  40009f:   52                      push   %rdx
  4000a0:   66 85 c0                test   %ax,%ax
  4000a3:   75 ef                   jne    400094 <_printInteger.L1>
  4000a5:   6a 3d                   pushq  $0x3d
  4000a7:   66 57                   push   %di
  4000a9:   80 04 24 30             addb   $0x30,(%rsp)
  4000ad:   6a 78                   pushq  $0x78
  4000af:   66 56                   push   %si
  4000b1:   80 04 24 30             addb   $0x30,(%rsp)
  4000b5:   ff ce                   dec    %esi
  4000b7:   75 cf                   jne    400088 <_table.L3>
  4000b9:   ff cf                   dec    %edi
  4000bb:   75 c7                   jne    400084 <_table.L2>

00000000004000bd <_printChars>:
  4000bd:   66 ba 00 08             mov    $0x800,%dx
  4000c1:   b0 01                   mov    $0x1,%al
  4000c3:   66 bf 01 00             mov    $0x1,%di
  4000c7:   48 89 e6                mov    %rsp,%rsi
  4000ca:   0f 05                   syscall

Here's a link to my first and second attempts, in their original nasm syntax.

I welcome anyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes).

EDIT: Now prints extra spaces and is golfed down even more! It's doing some pretty crazy stuff with the registers, and is probably unstable if this program were to be expanded.

deleted 259 characters in body
Source Link
davey
  • 421
  • 2
  • 7

x86_64 machine code (linux), 175175 99 bytes

00000000004000b00000000000400080 <_start>:
  4000b0400080:   41 bc 0109 00 00 00       mov    $0x1$0x9,%r12d

00000000004000b60000000000400086 <_table.L2>:
  4000b6400086:   416a bd0a 01 00 00 00       mov    $0x1,%r13d

00000000004000bc <_table.L3>:   pushq  $0xa
  4000bc400088:   4c4d 89 e8e5                mov    %r13%r12,%rax%r13
 
000000000040008b 4000bf<_table.L3>: 
  40008b: 49 f7 e44c 89 e8              mul  mov  %r12

00000000004000c2 <_printInteger>: %r13,%rax
  4000c240008e:   6a49 20f7 e4                mul  pushq  $0x20%r12

0000000000400091 <_printInteger>:
  4000c4400091:   486a 3120 c9                xor  pushq  %rcx,%rcx$0x20

00000000004000c70000000000400093 <_printInteger.L1>:
  4000c7400093:   48 31 d2                xor    %rdx,%rdx
  4000ca:   48 ff c1                inc    %rcx
  4000cd400096:   41 b8 0a 00 00 00       mov    $0xa,%r8d
  4000d340009c:   49 f7 f0                div    %r8
  4000d640009f:   48 83 c2 30             add    $0x30,%rdx
  4000da4000a3:   52                      push   %rdx
  4000db4000a4:   48 85 c0                test   %rax,%rax
  4000de4000a7:   75 e7ea                   jne    4000c7400093 <_printInteger.L1>
  4000e04000a9:   6a 3d                   pushq  $0x3d
  4000e24000ab:   49 83 c4 30             add    $0x30,%r12
  4000e64000af:   41 54                   push   %r12
  4000e84000b1:   49 83 ec 30             sub    $0x30,%r12
  4000ec4000b5:   6a 78                   pushq  $0x78
  4000ee4000b7:   49 83 c5 30             add    $0x30,%r13
  4000f24000bb:   41 55                   push   %r13
  4000f44000bd:   49 83 ed 3031             sub    $0x30$0x31,%r13

00000000004000f8 <_printChars>:
  4000f8:   48 89 c8                mov    %rcx,%rax
  4000fb:   41 b8 08 00 00 00       mov    $0x8,%r8d
  400101:   49 f7 e0                mul    %r8
  400104:   48 83 c0 28             add    $0x28,%rax
  400108:   48 89 c2                mov    %rax,%rdx
  40010b:   b8 01 00 00 00          mov    $0x1,%eax
  4001104000c1:   bf 01 00 00 00          mov    $0x1,%edi
  400115:  4d 4885 8ded 34 24             lea test   (%rsp)%r13,%rsi%r13
  4001194000c4:   0f 05                   syscall 
75 c5 40011b:   48 83 c2 28            jne add   40008b $0x28,%rdx<_table.L3>
  40011f4000c6:   48 01 d4                add    %rdx,%rsp
  400122: 49 ff 49cc 83 c5 01             adddec    $0x1,%r13%r12
  4001264000c9:   4d 39 e585 e4               cmp test   %r12,%r13%r12
  4001294000cc:   7e75 91b8                   jlejne    4000bc400086 <_table.L3>L2>

000000000040012b00000000004000ce <_printNewline><_printChars>:
  40012b4000ce:   b8ba 01b8 0009 00 00          mov    $0x1$0x9b8,%eax%edx
  4001304000d3:   bfb8 01 00 00 00          mov    $0x1,%edi%eax
  4001354000d8:   babf 01 00 00 00          mov    $0x1,%edx
  40013a:   48 be 60 01 60 00 00    movabs $0x600160,%rsi
  400141:   00 00 00 
  400144:   0f 05                   syscall 
  400146:   49 83 c4 01             add    $0x1,%r12
  40014a:   49 83 fc 0a             cmp    $0xa,%r12
  40014e:   0f 85 62 ff ff ff       jne    4000b6 <_table.L2>

0000000000400154 <_exit>:
  400154:   b8 3c 00 00 00          mov    $0x3c,%eax%edi
  4001594000dd:   48 31 ff 8d 34 24             xorlea    %rdi(%rsp),%rdi%rsi
  40015c4000e1:   0f 05                   syscall 

Disassembly of section .data:

0000000000600160 <newline>:
  600160:   0a                      .byte 0xa

This is a dump of the binary file, and all ofThis is a dump of the binary file, and all of this is 175 bytes. It basically does the same two loops that all the answers do, but printing to the console is a bit harder and basically requires pushing the characters to print onto the stack in reverse, and then making a (linux specific) syscall to actually put those chars into stdout.

I've now optimized this is 175 bytes. It basically does the same two loopsso that all the answers do, but printing to the consoleonly 1 write operation is a bit harderperformed (faster!) and basically requireshas magic numbers (wow!) and by pushing the characters to printentire result onto the stack in reverse, and thenbackwards before making a (linux specific)the syscall to actually put those chars into stdout. I also took out the exit routine because who needs proper exit code?

ThisHonestly a few bytes could probably be "legally" removed because code golf doesn't require full programs, only snippets, so a few of the values could be "assumed" to be initialized such as the first mov instruction into r12, but unless someone can definitelycome up with a close competing result in x86_64 machine code, I'll leave it be golfed down.

Here's a bit morelink to my first and second attempts, in their original nasm syntax.

(Also, it doesn't print the extra spaces to make all the columns aligned, but this isif that's required I can put the first attemptlogic in at the cost of a few more bytes). 

I welcome anyonewhoanyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

x86_64 machine code (linux), 175 bytes

00000000004000b0 <_start>:
  4000b0:   41 bc 01 00 00 00       mov    $0x1,%r12d

00000000004000b6 <_table.L2>:
  4000b6:   41 bd 01 00 00 00       mov    $0x1,%r13d

00000000004000bc <_table.L3>:
  4000bc:   4c 89 e8                mov    %r13,%rax
  4000bf:   49 f7 e4                mul    %r12

00000000004000c2 <_printInteger>:
  4000c2:   6a 20                   pushq  $0x20
  4000c4:   48 31 c9                xor    %rcx,%rcx

00000000004000c7 <_printInteger.L1>:
  4000c7:   48 31 d2                xor    %rdx,%rdx
  4000ca:   48 ff c1                inc    %rcx
  4000cd:   41 b8 0a 00 00 00       mov    $0xa,%r8d
  4000d3:   49 f7 f0                div    %r8
  4000d6:   48 83 c2 30             add    $0x30,%rdx
  4000da:   52                      push   %rdx
  4000db:   48 85 c0                test   %rax,%rax
  4000de:   75 e7                   jne    4000c7 <_printInteger.L1>
  4000e0:   6a 3d                   pushq  $0x3d
  4000e2:   49 83 c4 30             add    $0x30,%r12
  4000e6:   41 54                   push   %r12
  4000e8:   49 83 ec 30             sub    $0x30,%r12
  4000ec:   6a 78                   pushq  $0x78
  4000ee:   49 83 c5 30             add    $0x30,%r13
  4000f2:   41 55                   push   %r13
  4000f4:   49 83 ed 30             sub    $0x30,%r13

00000000004000f8 <_printChars>:
  4000f8:   48 89 c8                mov    %rcx,%rax
  4000fb:   41 b8 08 00 00 00       mov    $0x8,%r8d
  400101:   49 f7 e0                mul    %r8
  400104:   48 83 c0 28             add    $0x28,%rax
  400108:   48 89 c2                mov    %rax,%rdx
  40010b:   b8 01 00 00 00          mov    $0x1,%eax
  400110:   bf 01 00 00 00          mov    $0x1,%edi
  400115:   48 8d 34 24             lea    (%rsp),%rsi
  400119:   0f 05                   syscall 
  40011b:   48 83 c2 28             add    $0x28,%rdx
  40011f:   48 01 d4                add    %rdx,%rsp
  400122:   49 83 c5 01             add    $0x1,%r13
  400126:   4d 39 e5                cmp    %r12,%r13
  400129:   7e 91                   jle    4000bc <_table.L3>

000000000040012b <_printNewline>:
  40012b:   b8 01 00 00 00          mov    $0x1,%eax
  400130:   bf 01 00 00 00          mov    $0x1,%edi
  400135:   ba 01 00 00 00          mov    $0x1,%edx
  40013a:   48 be 60 01 60 00 00    movabs $0x600160,%rsi
  400141:   00 00 00 
  400144:   0f 05                   syscall 
  400146:   49 83 c4 01             add    $0x1,%r12
  40014a:   49 83 fc 0a             cmp    $0xa,%r12
  40014e:   0f 85 62 ff ff ff       jne    4000b6 <_table.L2>

0000000000400154 <_exit>:
  400154:   b8 3c 00 00 00          mov    $0x3c,%eax
  400159:   48 31 ff                xor    %rdi,%rdi
  40015c:   0f 05                   syscall 

Disassembly of section .data:

0000000000600160 <newline>:
  600160:   0a                      .byte 0xa

This is a dump of the binary file, and all of this is 175 bytes. It basically does the same two loops that all the answers do, but printing to the console is a bit harder and basically requires pushing the characters to print onto the stack in reverse, and then making a (linux specific) syscall to actually put those chars into stdout.

This can definitely be golfed down a bit more, but this is the first attempt. I welcome anyonewho has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

x86_64 machine code (linux), 175 99 bytes

0000000000400080 <_start>:
  400080:   41 bc 09 00 00 00       mov    $0x9,%r12d

0000000000400086 <_table.L2>:
  400086:   6a 0a                   pushq  $0xa
  400088:   4d 89 e5                mov    %r12,%r13

000000000040008b <_table.L3>: 
  40008b:   4c 89 e8                mov    %r13,%rax
  40008e:   49 f7 e4                mul    %r12

0000000000400091 <_printInteger>:
  400091:   6a 20                   pushq  $0x20

0000000000400093 <_printInteger.L1>:
  400093:   48 31 d2                xor    %rdx,%rdx
  400096:   41 b8 0a 00 00 00       mov    $0xa,%r8d
  40009c:   49 f7 f0                div    %r8
  40009f:   48 83 c2 30             add    $0x30,%rdx
  4000a3:   52                      push   %rdx
  4000a4:   48 85 c0                test   %rax,%rax
  4000a7:   75 ea                   jne    400093 <_printInteger.L1>
  4000a9:   6a 3d                   pushq  $0x3d
  4000ab:   49 83 c4 30             add    $0x30,%r12
  4000af:   41 54                   push   %r12
  4000b1:   49 83 ec 30             sub    $0x30,%r12
  4000b5:   6a 78                   pushq  $0x78
  4000b7:   49 83 c5 30             add    $0x30,%r13
  4000bb:   41 55                   push   %r13
  4000bd:   49 83 ed 31             sub    $0x31,%r13
  4000c1:   4d 85 ed                test   %r13,%r13
  4000c4:   75 c5                   jne    40008b <_table.L3>
  4000c6:   49 ff cc                dec    %r12
  4000c9:   4d 85 e4                test   %r12,%r12
  4000cc:   75 b8                   jne    400086 <_table.L2>

00000000004000ce <_printChars>:
  4000ce:   ba b8 09 00 00          mov    $0x9b8,%edx
  4000d3:   b8 01 00 00 00          mov    $0x1,%eax
  4000d8:   bf 01 00 00 00          mov    $0x1,%edi
  4000dd:   48 8d 34 24             lea    (%rsp),%rsi
  4000e1:   0f 05                   syscall

This is a dump of the binary file, and all of this is 175 bytes. It basically does the same two loops that all the answers do, but printing to the console is a bit harder and basically requires pushing the characters to print onto the stack in reverse, and then making a (linux specific) syscall to actually put those chars into stdout.

I've now optimized this so that only 1 write operation is performed (faster!) and has magic numbers (wow!) and by pushing the entire result onto the stack backwards before making the syscall. I also took out the exit routine because who needs proper exit code?

Honestly a few bytes could probably be "legally" removed because code golf doesn't require full programs, only snippets, so a few of the values could be "assumed" to be initialized such as the first mov instruction into r12, but unless someone can come up with a close competing result in x86_64 machine code, I'll leave it be.

Here's a link to my first and second attempts, in their original nasm syntax.

(Also, it doesn't print the extra spaces to make all the columns aligned, but if that's required I can put the logic in at the cost of a few more bytes). 

I welcome anyone who has any other suggestions on how it can be improved. I can also explain the logic in more detail if anyone is curious.

Source Link
davey
  • 421
  • 2
  • 7
Loading