factorial.asm

来自「介绍用Java解析网络数据的三种特殊方法」· 汇编 代码 · 共 88 行

ASM
88
字号
; This sample gets the number
; from the user, and calculates
; factorial for it.
; Supported input from 0 to 8
; inclusive!

#make_COM#

include 'emu8086.inc'

ORG     100h

start:

; get first number:

CALL    PTHIS
DB 13, 10, 'Enter the number: ', 0

CALL    scan_num


; factorial of 0 = 1:
MOV     AX, 1
CMP     CX, 0
JE      print_result

; move the number to BX:
; CX will be a counter:

MOV     BX, CX

MOV     AX, 1
MOV     BX, 1

calc:
MUL     BX
CMP     DX, 0
JNE     overflow
INC     BX
LOOP    calc

print_result:

; print result in AX:
CALL    PTHIS
DB 13, 10, 'Factorial: ', 0

CALL    PRINT_NUM_UNS
JMP     exit


overflow:
CALL    PTHIS
DB 13, 10, 'The result is too big!', 13, 10, 'Use values from 0 to 8.', 0
JMP     start

exit:

RET

;=================================
; here we define the functions
; from emu8086.inc

; SCAN_NUM reads a
; number from the user and stores
; it in CX register.

DEFINE_SCAN_NUM

; PRINT_NUM prints a signed
; number in AX.
; PRINT_NUM_UNS prints an unsigned
; number in AX (required by 
; PRINT_NUM):

DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS

; PTHIS prints NULL terminated
; string defined just after
; the CALL PTHIS instruction:
DEFINE_PTHIS

;=================================
END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?