-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathINDEC.asm
72 lines (72 loc) · 1.63 KB
/
INDEC.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
INDEC PROC
; reads a number in rage -32768 to 32767
; input: none
; output: AX = binary equivalent of number
PUSH BX ; save registers used
PUSH CX
PUSH DX
; print prompt
@BEGIN:
MOV AH, 2
MOV DL, '?'
INT 21H ; print '?'
; total = 0
MOV BX, 0 ; BX holds total
; negative = false
MOV CX, 0 ; CX holds sign
; read a character
MOV AH, 1
INT 21H ; character in AL
; case character of
CMP AL, '-' ; minus sign ?
JE @MINUS ; yes, set sign
CMP AL, '+' ; plus sign
JE @PLUS ; yes, get another character
JMP @REPEAT2 ; start processing characters
@MINUS:
MOV CX, 1 ; negative = true
@PLUS:
INT 21H ; read a character
; end_case
@REPEAT2:
; if character is between '0' and '9'
CMP AL, '0' ; character >= '0' ?
JNGE @NOT_DIGIT ; illegal character
CMP AL, '9' ; character <= '9' ?
JNLE @NOT_DIGIT ; no, illegal character
; then convert character to a digit
SUB AL, 30H ; convert to digit
MOV AH, 0
PUSH AX ; save on stack
; total = total x 10 + digit
MOV AX, 10 ; get 10
MUL BX ; AX = total x 10
POP BX ; retrieve digit
ADD BX, AX ; total = total x 10 + digit
; read a character
MOV AH,1
INT 21H
CMP AL, 0DH ; carriage return ?
JNE @REPEAT2 ; no, keep going
; until CR
MOV AX, BX ; store number in AX
; if negative
CMP CX, 0 ; negatvie number
JE @EXIT ; no, exit
; then
NEG AX ; yes, negate
; end_if
@EXIT:
POP DX ; restore registers
POP CX
POP BX
RET ; and return
; here if illegal character entered
@NOT_DIGIT:
MOV AH, 2 ; move cursor to a new line
MOV DL, 0DH
INT 21H
MOV DL, 0AH
INT 21H
JMP @BEGIN ; go to beginning
INDEC ENDP