serious stuff
www.cubbi.com
personal
programming
fibonacci numbers
algorithms
benchmarks
forth examples
postscript examples
muf examples
joy examples
j examples
scheme examples
hope examples
ocaml examples
haskell examples
prolog examples
c++ examples
java examples
asembly language examples
fortran examples
c examples
sh examples
awk examples
perl examples
tcl examples
asmix
hacker test
resume
science
martial arts
fun stuff
www.cubbi.org
|
Assembly languages
Date: 1952
Type: Imperative language of machine commands
Usage: basic low level language on any system
I've always been a fan of assembly languages, they take the idea of attention
to details to the extreme, and they are still the only way
to create programs that run the fastest on a given piece of hardware.
Of course, nobody needs to write entire programs in pure assembly, these examples
are just for fun.
ALGORITHM 1A: NAIVE BINARY RECURSION |
.globl _start
_start:
popq %rcx
cmpq $2,%rcx
jne usage_exit
addq $8,%rsp
popq %rsi
call rsi_to_bin
pushq %rax
call print_rax
call print_text
popq %rax
call fib
call print_rax
call print_newline
xorq %rbx,%rbx
movq $1,%rax
int $0x80
fib:
movq $-2, %rdx
cmpq $0,%rax
jge fibrec
negq %rax
testq $1,%rax
jnz fibrec
call fibrec
negq %rax
ret
.balign 4
fibrec:
cmpq $2,%rax
jl quitrec
decq %rax
pushq %rax
call fibrec
movq %rax,%rdi
popq %rax
decq %rax
pushq %rdi
call fibrec
popq %rbx
addq %rbx,%rax
quitrec:
ret
usage_exit:
movq $errtxt_l,%rdx
movq $errtxt,%rcx
call puts
movq $1,%rbx
movq $1,%rax
int $0x80
rsi_to_bin:
xorq %rax,%rax
movb $10,%cl
cmpb $45,(%rsi)
jnz next_digit
incq %rsi
movb $1,minus_flag(%rip)
next_digit:
cmpb $0,(%rsi)
jz end_rsi_to_bin
movb (%rsi),%bl
subb $48,%bl
js end_rsi_to_bin
cmpb %cl,%bl
jge end_rsi_to_bin
imulb %cl
addq %rbx,%rax
incq %rsi
jmp next_digit
end_rsi_to_bin:
cmpb $1, minus_flag(%rip)
jnz not_minus
negq %rax
not_minus:
ret
print_rax:
movq $10,%rbx
xorq %rcx,%rcx
movq $outbyte,%rdi
cmpq $0,%rax
jge divloop
negq %rax
movb $45,(%rdi)
incq %rdi
divloop:
xorq %rdx,%rdx
divq %rbx
addb $48,%dl
pushq %rdx
incq %rcx
testq %rax,%rax
jnz divloop
store:
popq %rax
stosb
loop store
subq $outbyte,%rdi
movq %rdi,%rdx
movq $outbyte,%rcx
jmp puts
print_text:
movq $outtxt_l,%rdx
movq $outtxt,%rcx
jmp puts
print_newline:
movq $1,%rdx
movq $newline,%rcx
puts:
movq $1,%rbx
movq $4,%rax
int $0x80
ret
.data
minus_flag:
.byte 0
.section .rodata
errtxt:
.ascii "Usage: ./f1a <n>"
newline:
.ascii "\n"
errtxt_l = .-errtxt
outtxt:
.ascii "th Fibonacci number is "
outtxt_l = .-outtxt
.bss
.lcomm outbyte, 22
|
ALGORITHM 2A-3: DATA STRUCTURE - SIMPLE LIST |
(Intel Assembly Language for x86, MS-DOS syscalls, COM output)
.model tiny
.code
org 100h
_start:
mov ax,cs
mov ds,ax
mov es,ax
cld
push 23
call fib
call print_eax
ret
fib: push bp
mov bp,sp
mov di,offset fib_array+2
mov cx,[bp+4]
fib_loop:
mov ax,[di-1]
add ax,[di-2]
inc di
mov [di],ax
loop fib_loop
mov bx,[bp+4]
mov ax,word ptr [bx+fib_array]
mov [bp+4],ax
pop bp
ret
print_eax:
mov bp,sp
mov di,offset outbyte
mov ax,[bp+2]
mov bx,10
xor cx,cx
divlp: xor dx,dx
div bx
add dl,48
push dx
inc cx
test ax,ax
jnz divlp
store: pop ax
stosb
loop store
mov byte ptr [di],'$'
mov dx, offset outtxt
mov ah,9
int 21h
ret 2
outtxt db "23rd Fibonacci number is "
outtxt_l = $-outtxt+1
outbyte db "00000000"
fib_array db 1,1
end _start
|
ALGORITHM 2B: SIMPLE RECURSION |
(Sun Assembly Language for SPARC, SunOS/Solaris syscalls, ELF output)
.section ".text"
.global _start
_start:
save %sp,-112,%sp
mov 45,%o2
mov 1,%o1
call fib_lambda,1
mov 1,%o0
call print_o0
nop
mov 1,%g1
ta 8
fib_lambda:
save %sp,-112,%sp
st %i2,[%fp+68]
ld [%fp+68],%o2
cmp %o2,2
bl lambda_ends
nop
st %i0,[%fp+68]
ld [%fp+68],%o1
add %o1,%i1,%o0
call fib_lambda,1
dec %o2
st %o0,[%fp+68]
ld [%fp+68],%i0
lambda_ends:
ret
restore
print_o0:
save %sp,-112,%sp
st %i0,[%fp+68]
ld [%fp+68],%o0
sethi %hi(outbyte_end),%l2
or %l2,%lo(outbyte_end),%l2
print_o0_loop:
clr %l1
o0_div_10_loop:
sub %o0,10,%o0
cmp %o0,0
bge o0_div_10_loop
inc %l1
add %o0,10+'0',%o0
stb %o0,[%l2]
dec %l1
mov %l1,%o0
cmp %l1,0
bne print_o0_loop
dec %l2
mov 1,%o0
sethi %hi(outtext),%o1
or %o1,%lo(outtext),%o1
mov outtext_l,%o2
mov 4,%g1
ta 8
mov 1,%o0
sethi %hi(outbyte),%o1
or %o1,%lo(outbyte),%o1
mov outbyte_l,%o2
mov 4,%g1
ta 8
ret
restore %g0,0,%o0
.section ".data"
outtext:
.ascii "45th Fibonacchi number is "
outtext_l = .-outtext
outbyte:
.ascii "0000000000"
.byte 012
outbyte_end = .-1
outbyte_l = .-outbyte
|
ALGORITHM 2C: NON-RECURSIVE LOOP |
(Motorola Assembly Language for PPC, LynxOS syscalls, XCOFF output)
.toc
outtext_a: .tc outtext[TC],outtext
outbyte_a: .tc outbyte_l[TC],outbyte_l
.csect __start[DS]
.globl __start
__start: .long .__start, TOC[tc0], 0
.csect .text[PR]
.__start:
li 3,45
li 7,1
li 8,1
fib_loop:
add 11,7,8
mr 7,8
mr 8,11
subi 3,3,1
cmpwi 3,0
bgt fib_loop
lwz 9,outbyte_a(2)
lwz 9,0(9)
li 10,10
div_loop:
divwu 3,7,10
mr 6,3
mullw 3,3,10
subf 3,3,7
mr 7,6
addi 3,3,'0'
stb 3,0(9)
subi 9,9,1
cmpwi 6,0
bgt div_loop
li 3,1
lwz 4,outtext_a(2)
li 5,outtext_l
li 0,26
sc
li 3,7
li 0,9
sc
.csect .data[RW]
outtext:
.byte "45th Fibonacci number is "
.byte "00000000"
outbyte:
.byte 10
outtext_l = .-outtext
outbyte_l:
.long outbyte-1
|
ALGORITHM 3A: MATRIX EQUATION |
Not yet done. (I plan to do this on SGI Assembly Language for MIPS, IRIX syscalls)
|
ALGORITHM 3B: FAST RECURSION |
(DEC Assembly Language for PDP-11, UNIX syscalls, a.out output)
.globl start
.text
start:
mov $0,(sp)
mov $27,-(sp)
jsr pc, lambda
print_r1:
mov $outbyte,r3
div_loop:
sxt r0
div $12,r0
add $60,r1
movb r1,-(r3)
mov r0,r1
tst r1
jne div_loop
mov $1,r0
sys 4; outtext; 37
mov $1,r0
sys 1
lambda:
mov 2(sp),r1
cmp $2,r1
beq gottwo
bgt gotone
sxt r0
div $2,r0
tst r1
beq even
odd:
mov 2(sp),r1
dec r1
sxt r0
div $2,r0
mov r0,-(sp)
jsr pc,lambda
add $2,sp
mov r0,r3
mov r1,r2
mov r3,r4
mul r2,r4
mov r5,r1
mov r3,r4
add r2,r4
mul r2,r4
add r5,r1
mul r3,r3
mov r3,r0
mul r2,r2
add r3,r0
rts pc
even:
mov 2(sp),r1
sxt r0
div $2,r0
dec r0
mov r0,-(sp)
jsr pc,lambda
add $2,sp
mov r0,r3
mov r1,r2
mov r2,r4
mul r2,r4
mov r5,r1
mov r2,r4
add r3,r4
mul r4,r4
add r5,r1
mov r2,r4
add r3,r4
mul r2,r4
mov r5,r0
mul r2,r3
add r3,r0
rts pc
gotone:
mov $1,r0
mov $1,r1
rts pc
gottwo:
mov $1,r0
mov $2,r1
rts pc
.data
outtext:
.byte 62,63,162,144,40,106,151,142,157,156
.byte 141,143,143,151,40,156,165,155
.byte 142,145,162,40,151,163,40
.byte 60,60,60,60,60
outbyte:
.byte 12
|
ALGORITHM 3C: BINET'S FORMULA |
Not yet done. (I don't have a different hardware platform to do this on. An account, anyone?)
|
|