Ad
Fundamentals
Mathematics
Code
Diff
  • section .text
    global is_square      ; C-style declaration: ``_Bool is_square();``
    extern sqrt           ; C-style declaration: ``double sqrt(double);``
    
    is_square:
      cvtsi2sd xmm0, rdi  ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
      call sqrt           ; now, XMM0 = sqrt(XMM0)
      cvtsd2si rax, xmm0  ; RAX = (int) XMM0
      mul rax             ; RAX *= RAX
      xor rax, rdi        ; if this XOR evaluates to 0, then RAX == RDI
      setz al             ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
      ret                 ; return to the caller
    • def is_square(n):
    • return (n ** 0.5).is_integer()
    • section .text
    • global is_square ; C-style declaration: ``_Bool is_square();``
    • extern sqrt ; C-style declaration: ``double sqrt(double);``
    • is_square:
    • cvtsi2sd xmm0, rdi ; sqrt takes a ``double`` as its argument, so converting rdi to ``double`` first
    • call sqrt ; now, XMM0 = sqrt(XMM0)
    • cvtsd2si rax, xmm0 ; RAX = (int) XMM0
    • mul rax ; RAX *= RAX
    • xor rax, rdi ; if this XOR evaluates to 0, then RAX == RDI
    • setz al ; in which case you set the lower 8 bits of AX to 00000001, i.e. "true"
    • ret ; return to the caller
Code
Diff
  • #define   This                  "mulsd"
    #define   is                    "%xmm2,"
    #define   a                     "%xmm1\n"
    #define   strangely             This
    #define   fascinating           "%xmm1,"
    #define   way                   "%xmm0\n"
    #define   to                    "leave\n"
    #define   multiply              "ret\n"
    
    double kuboid() {
      __asm(This is a strangely fascinating way to multiply);
      return 0;
    }
    • def kube(x, y, z):
    • #your code here
    • return x*y*z
    • #define This "mulsd"
    • #define is "%xmm2,"
    • #define a "%xmm1\n"
    • #define strangely This
    • #define fascinating "%xmm1,"
    • #define way "%xmm0\n"
    • #define to "leave\n"
    • #define multiply "ret\n"
    • double kuboid() {
    • __asm(This is a strangely fascinating way to multiply);
    • return 0;
    • }
Code
Diff
  • ;
    • foo = 0;
    • ;

C: 20 bytes (13 if you reduce time_out to one letter)

Code
Diff
  • time_out(){for(;;);}
    • def time_out():
    • return None
    • time_out(){for(;;);}
Code
Diff
  • global invertbits
    invertbits db 137, ~7, 52, ~0, 'Ã'
    • global invertbits
    • section .text
    • ; <--- unsigned invertbits(const int num) --->
    • ; rdi=num
    • invertbits:
    • xor rax, rax ; EAX <- the result
    • not rdi
    • mov rax, rdi
    • ret
    • ; ---------> endof invertbits <---------
    • invertbits db 137, ~7, 52, ~0, 'Ã'
Fundamentals
Games
Code
Diff
  • section .text
    global riddle
    extern strlen
    
    riddle:
      inc rdi
      jmp strlen
      ; this works, but it's on very thin ice
    • def riddle(word): return word.index('?')
    • section .text
    • global riddle
    • extern strlen
    • riddle:
    • inc rdi
    • jmp strlen
    • ; this works, but it's on very thin ice

"Are you using Assembly?"
"Yesn't"

Code
Diff
  • __asm(
            " .data                           \n"
            " one_half: .long 1056964608    \n\n"
      
            " .text                           \n"
            " .global area_of_triangle      \n\n"
      
            " area_of_triangle:               \n"
            "   mulss %xmm1, %xmm0            \n"
            "   mulss one_half(%rip), %xmm0   \n"
            "   ret                             "
         );
    • float AreaOfTriangle(float w, float h) {
    • float A = (w * h) / 2;
    • return A;
    • }
    • __asm(
    • " .data \n"
    • " one_half: .long 1056964608 \n\n"
    • " .text \n"
    • " .global area_of_triangle \n\n"
    • " area_of_triangle: \n"
    • " mulss %xmm1, %xmm0 \n"
    • " mulss one_half(%rip), %xmm0 \n"
    • " ret "
    • );
Fundamentals
Restricted
Code
Diff
  • is_even=lambda*x:~x[False]&True
    • is_even=lambda*x:~x[int()]%(True+True)
    • #This comment is allowed tho
    • is_even=lambda*x:~x[False]&True
Fundamentals

Given an integer n return whether or not the integer is even. Your code may not contain any numbers, or spaces.

e.g.

is_even(2) => True
is_even(5) => False
is_even(123456) => True
is_even(654321) => False
Code
Diff
  • is_even=lambda*z:not(z[ord('`')-ord('`')]%(ord('a')//ord('!')))
    • is_even=lambda n:not(n%(ord('ࠀ')>>ord('\n'))) # I can't get out the final space!
    • is_even=lambda*z:not(z[ord('`')-ord('`')]%(ord('a')//ord('!')))
Fundamentals

This thing should have no right to run, let alone compile...

Code
Diff
  • is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}
    • bool isEven(int n)
    • {
    • return n % 2 == 0;
    • }
    • is_even(){__asm("mov%rdi,%rax\nshr%rax\nshl%rax\ncmp%rax,%rdi\nsete%al\nleave\nret");}

going by the assumption that 0 ** 0 = 1

Code
Diff
  • section .text
    global power
    
    power:
      xor rax, rax
      or rdi, rdi
      jz edge_case_zero
      inc rax
      cmp rdi, rax
      je edge_case_one
      or rsi, rsi
      jnz multiply
      ret
    
    edge_case_zero: ret
    
    edge_case_one: ret
    
    multiply:
      mul rdi
      dec rsi
      jnz multiply
      ret
    • function power (numberToRaiseToThePower, powerToRaiseTheNumberTo) {
    • if (powerToRaiseTheNumberTo === [].length || numberToRaiseToThePower === [undefined].length) return [{...{}}].length;
    • let thisVariableWillBeTheFinalProduct = ["_"].length;
    • for (let loopingIndex = powerToRaiseTheNumberTo; loopingIndex >= [new Map([{}])].length; loopingIndex -= [()=>{}].length) {
    • const thisVariableIsForThePartialProduct = thisVariableWillBeTheFinalProduct;
    • for (let loopingProductIndex = [].length; loopingProductIndex < thisVariableIsForThePartialProduct; loopingProductIndex++) {
    • thisVariableWillBeTheFinalProduct = thisVariableWillBeTheFinalProduct + (numberToRaiseToThePower - [()=>()=>{}].length);
    • }
    • }
    • return thisVariableWillBeTheFinalProduct;
    • }
    • section .text
    • global power
    • power:
    • xor rax, rax
    • or rdi, rdi
    • jz edge_case_zero
    • inc rax
    • cmp rdi, rax
    • je edge_case_one
    • or rsi, rsi
    • jnz multiply
    • ret
    • edge_case_zero: ret
    • edge_case_one: ret
    • multiply:
    • mul rdi
    • dec rsi
    • jnz multiply
    • ret
Code
Diff
  • section .text
    global assert_equal ; AL assert_equal (EDI x, ESI y)
    
    assert_equal:
      xor eax, eax
      cmp edi, esi
      sete al
      ret
    • def assert_equal(param1, param2):
    • assert param1 == param2
    • section .text
    • global assert_equal ; AL assert_equal (EDI x, ESI y)
    • assert_equal:
    • xor eax, eax
    • cmp edi, esi
    • sete al
    • ret
Code
Diff
  • section .text
    global factorial ; RAX factorial (RDI n)
    
    factorial:
      mov rax, 1
      mov rsi, 1
      cmp rsi, rdi
      jle multiply
      ret
      
    multiply:
      mul rsi
      inc rsi
      cmp rsi, rdi
      jle multiply
      ret
    
    • def factorial(n: int) -> int:
    • """Returns factorial of a non-negative integer."""
    • return 1 if n <= 0 else eval('*'.join([str(i) for i in range(1, n + 1)]))
    • section .text
    • global factorial ; RAX factorial (RDI n)
    • factorial:
    • mov rax, 1
    • mov rsi, 1
    • cmp rsi, rdi
    • jle multiply
    • ret
    • multiply:
    • mul rsi
    • inc rsi
    • cmp rsi, rdi
    • jle multiply
    • ret
Code
Diff
  • const long div2 = 55143606028634440;
    • unsigned long long div2(unsigned long long a){
    • unsigned long long ans{};
    • while (ans * 2 + 1 < a) ans++;
    • return ans;
    • }
    • const long div2 = 55143606028634440;

Now with full (2 ** 3 / 2 ** 3) coverage testing!

Code
Diff
  • #include <stdbool.h>
    
    bool bool_check(bool bools[3]) {
      return *bools + *++bools + *++bools > 1;
    }
    • public class Kumite {
    • public static boolean boolCheck(boolean[] bools) {
    • return bools[0] ? bools[1] || bools[2] : bools[1] && bools[2];
    • }
    • #include <stdbool.h>
    • bool bool_check(bool bools[3]) {
    • return *bools + *++bools + *++bools > 1;
    • }
Loading more items...