[Question]
  [
Write the shortest possible assembly-language [quine](http://en.wikipedia.org/wiki/Quine_%28computing%29).
Use any ISA you want, unless it has a `print-quine` instruction or equivalent. Examples include x86, MIPS, SPARC, MMIX, IBM BAL, MIX, VAX, JVM, ARM, etc.
You may link against the C standard library's `_printf` function (or the Java equivalent for JVM bytecode) for I/O.
Length will be judged both on instruction count and size of the data segment. Solutions must contain at least two instructions.
The quine should print the *assembly* code, not the assembled machine code.
  
[Answer]
## x86 Linux, AT&T syntax: 244
```
push $10
push $34
push $s
push $34
push $37
push $37
push $s
call printf
mov $0,%ebx
mov $1,%eax
int $128
s:.ascii "push $10
push $34
push $s
push $34
push $37
push $37
push $s
call printf
mov $0,%cebx
mov $1,%ceax
int $128
s:.ascii %c%s%c%c"
```
(I compiled it with this: `gcc -nostartfiles -lc quine.S -o quine`)
[Answer]
# gas for x86 Linux (~~89~~ 88 bytes, seven instructions)
Technically, this is cheating.
```
mov $4,%al
mov $1,%bl
mov $b,%ecx
mov $89,%dl
int $128
mov $1,%al
int $128
b:.incbin"a"
```
Save in a file named `a` and assemble with the following commands to create the executable named `a.out`.
```
as -o a.o <a ; ld a.o
```
The directive `.incbin` includes a file verbatim at the current location. If you use this to include the source code itself, you get a nice quine.
[Answer]
### JVM Bytecode Assembly (via [Jasmin](http://jasmin.sourceforge.net)) – 952 ~~960~~ ~~990~~
```
.class public Q
.super java/io/File
.method public static main([Ljava/lang/String;)V
.limit stack 9
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc ".class public Q%n.super java/io/File%n.method public static main([Ljava/lang/String;)V%n.limit stack 9%ngetstatic java/lang/System/out Ljava/io/PrintStream;%nldc %c%s%c%nldc 3%nanewarray java/lang/Object%ndup%ndup%nldc 0%nldc 34%ninvokestatic java/lang/Integer/valueOf(I)Ljava/lang/Integer;%ndup_x2%naastore%nldc 2%nswap%naastore%ndup2%nswap%nldc 1%nswap%naastore%ninvokevirtual java/io/PrintStream/printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;%npop%nreturn%n.end method"
ldc 3
anewarray java/lang/Object
dup
dup
ldc 0
ldc 34
invokestatic java/lang/Integer/valueOf(I)Ljava/lang/Integer;
dup_x2
aastore
ldc 2
swap
aastore
dup2
swap
ldc 1
swap
aastore
invokevirtual java/io/PrintStream/printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
pop
return
.end method
```
Sadly, Jasmin doesn't allow as many nice tricks as Microsoft's `ilasm` allows. But the JVM has a total of *six* different `dup` instructions that do all kinds of fun things. Reordering items on the stack is something .NET doesn't seem to support.
In any case, I guess none of my two entries are serious contenders for shortest code but I guess it's hard to make them much shorter. Therefore just for completeness :-)
Commented version with info about what's on the stack:
```
.class public Q
.super java/io/File
.method public static main([Ljava/lang/String;)V
.limit stack 9
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc ".class public Q%n.super java/io/File%n.method public static main([Ljava/lang/String;)V%n.limit stack 9%ngetstatic java/lang/System/out Ljava/io/PrintStream;%nldc %c%s%c%nldc 3%nanewarray java/lang/Object%ndup%ndup%nldc 0%nldc 34%ninvokestatic java/lang/Integer/valueOf(I)Ljava/lang/Integer;%ndup_x2%naastore%nldc 2%nswap%naastore%ndup2%nswap%nldc 1%nswap%naastore%ninvokevirtual java/io/PrintStream/printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;%npop%nreturn%n.end method"
ldc 3       ; stack; System.out, string, 3
anewarray java/lang/Object    ; stack: System.out, string, Object[3]
dup
dup    ; stack: System.out, string, array, array, array
ldc 0  ; stack: System.out, string, array, array, array, 0
ldc 34   ; stack: System.out, string, array, array, array, 0, 34
invokestatic java/lang/Integer/valueOf(I)Ljava/lang/Integer;
dup_x2   ; stack: System.out, string, array, array, 34, array, 0, 34
aastore  ; stack: System.out, string, array, array, 34
ldc 2    ; stack: System.out, string, array, array, 34, 2
swap     ; stack: System.out, string, array, array, 2, 34
aastore  ; stack: System.out, string, array
dup2     ; stack: System.out, string, array, string, array
swap     ; stack: System.out, string, array, array, string
ldc 1    ; stack: System.out, string, array, array, string, 1
swap     ; stack: System.out, string, array, array, 1, string
aastore  ; stack: System.out, string, array
invokevirtual java/io/PrintStream/printf(Ljava/lang/String;[Ljava/lang/Object;)Ljava/io/PrintStream;
pop
return
.end method
```
**History:**
* 2011-02-07 02:09 (990) – First working version.
* 2011-02-07 02:11 (960) – `ldc` is shorter than `bipush` or `iconst_*`.
* 2011-02-07 02:30 (952) – Who says I need to inherit from java.lang.Object? Other class names are so much shorter :-)
[Answer]
# GAS x86-64 for Linux (verified on GCC 10.1):
## ~~13 12~~ 5 instructions
## ~~148 136 129~~ 0 byte data section
### ~~298 274 260 51~~ 49 byte source code
Self-imposed rules/assumptions:
1. No standard library (including `printf` and `main`) allowed.
2. No `.incbin` allowed. This is commonly considered input in quine challenges.
3. In a similar vein, I disallowed using the `-D` flag as this would likely trivialize the problem as well as any file-content accessing flags. All other flags are fair game.
4. If it's loaded in program memory, it's fair game. As long as no files are accessed.
```
lea .+174,%rsi;inc %di;lea 49,%dx;inc %ax;syscall
```
### Annotated version:
```
// We rely on Linux initializing all GP registers to 0.
// The filename is loaded into memory at this location:
lea .+174,%rsi
inc %di
// Length
lea 49,%dx
inc %ax
// Write string
syscall
// Since there are no valid instructions after this, it crashes.
// Since Segfault info is not program output, we're good.
```
The ELF has no ".data" section. The program reads from the ".debug\_line" section as if it were one, so that's why it's 0 bytes. This requires some setup.
### The ~~Hacks~~ ~~Voodoo Magic~~ Setup
Save the file as `lea .+174,%rsi;inc %di;lea 49,%dx;inc %ax;syscall.sx` (ie, source + `.sx`) and compile with:
```
% qFile='lea .+174,%rsi;inc %di;lea 49,%dx;inc %ax;syscall.sx'
% gcc -g1 -nostdlib -no-pie $qFile -o quine
```
Explanation:
* `-g1` adds debugging info to ELF, which handily injects the filename too! This also means it gets loaded into memory at runtime!
* `-no-pie` is required for two reasons:
	1. We want to load a non-RIP-relative address
	2. The debugging info doesn't get ASLR'ed away.
* `-nostdlib` means we don't have to use `main`.
Verify with diff:
```
% ./quine > quineresult.sx; diff $qFile quineresult.sx
[1]    52466 segmentation fault (core dumped)  ./quine > quineresult.sx
```
The filename is not part of the true source code, as it is not part of the file's contents, which is why I only output this source one time. I originally penalized my source code because the filename is vital to the quine, but I realized that this was moot because the rules aren't scoring on source code size anyway. I guess this means I beat the accepted answer without `printf`!
[Answer]
## Windows .COM Format: 307 characters
Assembles, using A86, to 51 bytes. Requires no external libraries other than the DOS Int21 AH=9 function (write string to stdout).
```
db 185
db  51
db   0
db 190
db   0
db   1
db 191
db  47
db   1
db 172
db 178
db  10
db 199
db   6
db  45
db   1
db  32
db  32
db 180
db   0
db 246
db 242
db 128
db 196
db  48
db 136
db  37
db  79
db  10
db 192
db 117
db 242
db 180
db   9
db 186
db  42
db   1
db 205
db  33
db 226
db 221
db 195
db 100
db  98
db  32
db  32
db  51
db  49
db  10
db  13
db  36
```
[Answer]
# NASM, 223 bytes
```
%define a "%define "
%define b "db "
%define c "%deftok "
%define d "a, 97, 32, 34, a, 34, 10, a, 98, 32, 34, b, 34, 10, a, 99, 32, 34, c, 34, 10, a, 100, 32, 34, d, 34, 10, c, 101, 32, 100, 10, b, 101, 10"
%deftok e d
db e
```
Beating the accepted answer!
[Answer]
### .NET CIL – 623 ~~669~~ ~~691~~ ~~723~~ ~~727~~
```
.assembly H{}.method void M(){.entrypoint.locals init(string)ldstr".assembly H{0}{1}.method void M(){0}.entrypoint.locals init(string)ldstr{2}{3}{2}stloc 0ldloc 0ldc.i4 4newarr object dup dup dup dup ldc.i4 0ldstr{2}{0}{2}stelem.ref ldc.i4 1ldstr{2}{1}{2}stelem.ref ldc.i4 2ldc.i4 34box char stelem.ref ldc.i4 3ldloc 0stelem.ref call void[mscorlib]System.Console::Write(string,object[])ret{1}"stloc 0ldloc 0ldc.i4 4newarr object dup dup dup dup ldc.i4 0ldstr"{"stelem.ref ldc.i4 1ldstr"}"stelem.ref ldc.i4 2ldc.i4 34box char stelem.ref ldc.i4 3ldloc 0stelem.ref call void[mscorlib]System.Console::Write(string,object[])ret}
```
A single line, no line break at the end.
Formatted and commented first version (even though it isn't a quine anymore) – it's unlikely that I deviate much from the general concept:
```
.assembly H{}
.method void M() {
  .entrypoint
  .locals init (
    string,
    object[]
  )
  // the string
  ldstr".assembly H{0}{1}.method void M(){0}.entrypoint.locals init(string,object[])ldstr{2}{3}{2}stloc.0 ldloc.0 ldc.i4.4 newarr object stloc.1 ldloc.1 ldc.i4.0 ldstr{2}{0}{2} stelem.ref ldloc.1 ldc.i4.1 ldstr{2}{1}{2} stelem.ref ldloc.1 ldc.i4.2 ldc.i4 34 box char stelem.ref ldloc.1 ldc.i4.3 ldloc.0 stelem.ref ldloc.1 call void[mscorlib]System.Console::Write(string,object[])ret{1}"
  stloc.0   // store in first local var
  ldloc.0   // load again. Going to be the first argument to Console::Write
  ldc.i4.4 newarr object stloc.1   // create new array and store in local var
  ldloc.1 ldc.i4.0 ldstr"{" stelem.ref   // we need a literal brace
  ldloc.1 ldc.i4.1 ldstr"}" stelem.ref   // closing, too
  ldloc.1 ldc.i4.2 ldc.i4 34 box char stelem.ref   // double quote
  ldloc.1 ldc.i4.3 ldloc.0 stelem.ref   // our format string from before
  ldloc.1 // load array
  call void[mscorlib]System.Console::Write(string,object[]) // output
  ret
}
```
**History**:
* 2011-02-06 16:48 (727) – First working version.
* 2011-02-06 17:14 (723) – I don't need a space after a string literal.
* 2011-02-06 17:21 (691) – `dup` is shorter than writing `ldloc.1` every time.
* 2011-02-06 17:24 (669) – I don't need spaces after *any* literal and things like `ldloc.1` can be written as `ldloc 1` to make the last token a literal. The resulting bytecode is likely larger, but it's about the assembler code so I couldn't care less :-)
* 2011-02-06 17:34 (623) – I don't need the `object[]` as a local variable; I can do all that on the stack directly. Nice.
[Answer]
# gas for x86 Linux, ~~184~~ 176 bytes
```
.globl main
main:movw $34,B+87
push $B
call printf
call printf
pop B
ret
.data
B:.ascii".globl main
main:movw $34,B+87
push $B
call printf
call printf
pop B
ret
.data
B:.ascii"
```
Build with `gcc -m32 -o a.out quine.S`. (The `-m32` is optional if your OS is already 32-bit.)
**Edited to add:** If we modify the rules to allow `puts` to be called instead of `printf` then it can be done in ~~182~~ 174 bytes:
```
.globl main
main:movw $34,B+86
push $B+1
call puts
call puts
pop B
ret
.data
B:.ascii"
.globl main
main:movw $34,B+86
push $B+1
call puts
call puts
pop B
ret
.data
B:.ascii"
```
(Note that this one, unlike the previous one, has a terminating newline.)
[Answer]
# Bootable ASM, 660 bytes
```
[bits 16]
mov ax,07C0h
mov ds,ax 
mov ah,0
mov al,03h 
int 10h
mov si,code
call p 
jmp $
p:mov ah,0Eh
r:lodsb
cmp al,0
je d
cmp bx,0x42
jne s
c:int 10h
jmp r
s: cmp al,94 
je re
cmp al,63
je q
jmp c
q:mov al,34
jmp c
re:push si
mov bx,0x42
mov si,code
call p 
mov bx,0
pop si
jmp p 
d:ret
code:db "[bits 16]\mov ax,07C0h\mov ds,ax\mov ah,0\mov al,03h\int 10h\mov si,code\call p\jmp $\p:mov ah,0Eh\r:lodsb\cmp al,0\je d\cmp bx,0x42\jne s\c:int 10h\jmp r\s:cmp al,94\je re\cmp al,63\je q\jmp c\q:mov al,34\jmp c\re:push si\mov bx,0x42\mov si,code\call p\mov bx,0\pop si\jmp p\\d:ret\\code:db ?^?\times 510-($-$$) db 0\dw 0xAA55"
times 510-($-$$) db 0
dw 0xAA55
```
Originally by [jdiez17](https://github.com/jdiez17/quines/tree/master/bootable_quine), golfed by yours truly.
[Answer]
## x86-64, System V AMD64 ABI, GASM: 432
```
.att_syntax noprefix
.globl main
main:
pushq rbp
movq rsp, rbp
mov $.Cs, rdi
mov $0xa, rsi
mov $0x22, edx
mov $.Cs, ecx
mov $0x22, r8d
mov $0xa, r9d
xor eax, eax
call printf
xor eax, eax
leave
ret
.Cs: .string ".att_syntax noprefix
.globl main
main:
pushq rbp
movq rsp, rbp
mov $.Cs, rdi
mov $0xa, rsi
mov $0x22, edx
mov $.Cs, ecx
mov $0x22, r8d
mov $0xa, r9d
xor eax, eax
call printf
xor eax, eax
leave
ret%c.Cs: .string %c%s%c%c"
```
[Answer]
# 80x86 TASM, 561 bytes
```
MODEL TINY
.CODE
.STARTUP
DB 177
DB 076
DB 186
DB 044
DB 001
DB 172
DB 180
DB 036
DB 179
DB 004
DB 191
DB 080
DB 001
DB 079
DB 136
DB 037
DB 212
DB 010
DB 004
DB 048
DB 134
DB 196
DB 075
DB 117
DB 244
DB 180
DB 009
DB 205
DB 033
DB 178
DB 071
DB 226
DB 228
DB 178
DB 038
DB 205
DB 033
DB 195
DB 013
DB 010
DB 069
DB 078
DB 068
DB 036
DB 077
DB 079
DB 068
DB 069
DB 076
DB 032
DB 084
DB 073
DB 078
DB 089
DB 013
DB 010
DB 046
DB 067
DB 079
DB 068
DB 069
DB 013
DB 010
DB 046
DB 083
DB 084
DB 065
DB 082
DB 084
DB 085
DB 080
DB 013
DB 010
DB 068
DB 066
DB 032
END
```
[Answer]
I wrote this in assembly on paper and assembled it into machine code by hand with an opcode chart. I then entered the machine code into the program editor on the TI-84 Plus CE and executed it with asm(. It starts at it's own load address, copies that byte into the accumulator, then makes a system call that prints the contents of the accumulator in ascii on the display and repeats for every byte of the program, essentially printing it's own machine code.... Is it technically a quine because I didn't use a computer to assemble it and because the exact code I entered into the calculator is printed by the program itself?
This is ez80 machine language on a TI-84 Plus CE:
34 bytes. First byte loads @ D1A881, whitespace ignored:
```
181F
50
7E
23
0602
4F
07070707
E60F
C630
FE3A
3802
C607
CDB80702
79
10EF
42
10E2
C9
C9
```
[Answer]
## [TAL](http://wiki.tcl.tk/2561)
```
push puts
push \100
push {push puts
push \100
push {@}
dup
strmap
invokeStk 2}
dup
strmap
invokeStk 2
```
To execute it, call `::tcl::unsuppoted::assemble` with the code as argument.  
Tcl 8.6 only.
]