Skip to content

Instantly share code, notes, and snippets.

@WideWord
Created February 14, 2015 13:09
Show Gist options
  • Select an option

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

Select an option

Save WideWord/e6868ab2922a9d5b6496 to your computer and use it in GitHub Desktop.
org 0x100
push enter_number_msg
call write_str
add sp, 2
push 0
call read_int
pop ax
push ax
call fact
pop dx
push fac_msg
call write_str
add sp, 2
push ax
call write_int
add sp, 2
push is_msg
call write_str
add sp, 2
push dx
call write_int
add sp, 2
ret
fact: ; (word) -> word
push bp
mov bp, sp
push ax
push dx
mov dx, 0
cmp word [bp+4], 0
je .null
mov ax, [bp+4]
sub ax, 1
push ax
call fact
pop ax
mul word [bp+4]
mov [bp+4], ax
jmp .exit
.null:
mov word [bp+4], 1
.exit:
pop dx
pop ax
pop bp
ret
enter_number_msg:
db 'Enter number: '
db 0
fac_msg:
db 'fac('
db 0
is_msg:
db ') = '
db 0
read_int: ; () -> word
push bp
mov bp, sp
push ax
push bx
push dx
mov dx, 0
.loop1:
mov ah, 0x1
int 0x21
mov bl, al
mov bh, 0
cmp bl, 13
je .exit
sub bl, 48
mov ax, dx
mov dx, 10
mul dx
mov dx, ax
add dx, bx
jmp .loop1
.exit:
;return dx
mov [bp+4], dx
;exit subprogram
pop dx
pop bx
pop ax
pop bp
ret
write_int:; (word number) -> void
push bp
mov bp, sp
push ax
push bx
push dx
push cx
mov cx, sp
cmp word [bp+4], 0
je .null
; while (number != 0)
.loop1:
cmp word [bp+4], 0
je .exit1
; byte digit = number % 10
mov ax, [bp+4]
mov dx, 0
mov bx, 10
div bx
;number = number / 10
mov [bp+4], ax
;digit += 48
add dl, 48
;dos_putc((byte)digit)
push dx
jmp .loop1
.exit1:
.loop2
cmp cx, sp
je .exit2
mov ah, 0x2
pop dx
int 0x21
jmp .loop2
.exit2:
jmp .exit
.null:
mov ah, 0x2
mov dl, 48
int 0x21
.exit:
pop cx
pop dx
pop bx
pop ax
pop bp
ret
write_str: ;(string) -> void
push bp
mov bp, sp
push ax
push bx
push dx
mov bx, [bp+4]
.loop1:
cmp byte [bx], 0
je .exit1
mov ah, 0x2
mov dl, [bx]
int 0x21
add bx, 1
jmp .loop1
.exit1:
pop dx
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