Skip to content

Instantly share code, notes, and snippets.

@WideWord
Created February 19, 2015 19:45
Show Gist options
  • Select an option

  • Save WideWord/114c98c740a70003bfaa to your computer and use it in GitHub Desktop.

Select an option

Save WideWord/114c98c740a70003bfaa to your computer and use it in GitHub Desktop.
sub sp, 2
call read_int
call fib
call write_int
add sp, 2
ret
fib: ; (word) -> word
push bp
mov bp, sp
push ax
push dx
; if (x == 1 || x == 2)
cmp word [bp+4], 1
je .one
cmp word [bp+4], 2
je .one
mov ax, [bp+4]
; fib(x - 1)
dec ax
push ax
call fib
pop dx
; fib(x - 2)
dec ax
push ax
call fib
pop ax
add ax, dx ; fib(x - 1) + fib(x - 2)
mov [bp+4], ax ; return fib(x - 1) + fib(x - 2)
jmp .exit
.one: ; else
mov word [bp+4], 1 ; return 1
.exit:
pop dx
pop ax
pop bp
ret
write_int: ;(word number) -> void
; сохраняем указатель стека чтобы получить аргументы функции
push bp
mov bp, sp
push ax
push bx
push cx
push dx
mov cx, 0
.loop1_start:
cmp word [bp+4], 0
je .loop1_exit
; делим number, получаем остаток
mov dx, 0
mov ax, [bp+4]
mov bx, 10
div bx; ax = dx:ax / bx (остаток dx)
mov [bp+4], ax
add dx, 48 ;выводим цифру (остаток от 10)
push dx
inc cx
jmp .loop1_start
.loop1_exit:
mov ah, 2h
cmp cx, 0
je .null
.loop2_start:
pop dx
int 21h
dec cx
cmp cx, 0
jne .loop2_start
jmp .exit
.null:
mov dl, 48
int 21h
.exit:
pop dx
pop cx
pop bx
pop ax
pop bp
ret
read_int: ; () -> int
push bp
mov bp, sp
push ax
push bx
push cx
push dx
mov cx, 0
.loop_start:
mov ah, 1h
int 21h
cmp al, 13
je .loop_exit
sub al, 48
mov bl, al
mov bh, 0
mov ax, cx
mov cx, 10
mul cx
add ax, bx
mov cx, ax
jmp .loop_start
.loop_exit:
mov [bp+4], cx
pop dx
pop cx
pop bx
pop ax
pop bp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment