Skip to content
The Chat Archive
gpt-4.1-2025-04-14· OpenAI LMArena Human Preference 140K

Generate intensive and hard code with assembly

  1. User

    Generate intensive and hard code with assembly

  2. Assistant

    Absolutely! Here’s an example of an intensive and hard Assembly code: a program that computes the factorial of a number recursively using the x86-64 Assembly (NASM syntax), and prints the result to the console. This is a non-trivial task in Assembly, as recursion, stack management, and system calls are all involved.

    x86-64 Linux NASM: Recursive Factorial with Output

    section .data
        fmt db "Factorial: %d", 10, 0
    
    section .bss
        result resq 1
    
    section .text
        global main
        extern printf
    
    ; int factorial(int n)
    factorial:
        push rbp
        mov rbp, rsp
        mov rax, rdi        ; rax = n
        cmp rdi, 1
        jle .base_case
        dec rdi
        call factorial
        imul rax, rdi
        jmp .end
    .base_case:
        mov rax, 1
    .end:
        pop rbp
        ret
    
    main:
        push rbp
        mov rbp, rsp
    
        mov rdi, 10         ; Calculate factorial(10)
        call factorial
    
        mov [result], rax   ; Store result
    
        mov rax, [result]
        mov rsi, rax        ; 2nd argument: result
        mov rdi, fmt        ; 1st argument: format string
        xor rax, rax        ; printf needs rax=0 for variadic
        call printf
    
        mov eax, 0
        leave
        ret
    

    How to Assemble and Run

    1. Save as factorial.asm
    2. Assemble and link:
      nasm -f elf64 factorial.asm -o factorial.o
      gcc factorial.o -o factorial
      
    3. Run:
      ./factorial
      

    Why is this "intensive and hard"?

    • Recursion in Assembly is tricky: you must manage the stack and calling conventions manually.
    • System calls (here, printf) require correct argument passing.
    • Multiplication and register management are explicit.
    • No high-level constructs: all logic is manual.

    If you want an even more complex example (e.g., matrix multiplication, prime sieve, or Mandelbrot set), let me know!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.