-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_question.asm
71 lines (54 loc) · 2.31 KB
/
1_question.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
; Program description
StSeg Segment STACK 'STACK'
DB 100H DUP (?)
StSeg ENDS
DtSeg Segment
; place data here
DtSeg ENDS
CDSeg Segment
ASSUME CS: CDSeg, DS: DtSeg, SS: StSeg
Start:
MOV AX, DtSeg
MOV DS, AX
; my code starts here
push 0000H ; flaf is 0
xor CX, CX ; CX = 0
xor DX, DX ; DX = 0
input_number:
mov AH, 07H ; read char
INT 21H
CMP AL, 2DH ; AL = '-'
jz is_neg
sub AL, 0DH ; AL -= 13
jz end ; if AL = '\n' go to end
sub AL, 23H ; AL -= 35 (in fact AL -= 48 to make binary from ascii)
Xor AH, AH ; AH = 0
xchg AX, CX ; prepare for mul
xchg DX, AX ; store valuable values in CX to retrieve after mul
MOV BX, 000AH ; BX = 10
MUL BX ; DX, AX = AX * 10
xchg AX, DX ; retrieve values of DX and AX
xchg AX, CX ; retrieve values of DX and AX
ADD DX, AX ; DX += AX
JMP input_number ; continue reading chars and convert them to int
is_neg:
push 01H ; flag for negative number is 1
JMP input_number ; continue reading chars and convert them to int
end: ; final
POP AX ; AX is the flag
CMP AX, 0001H ; if AX == 1 then zf = 1
jz negate_num ; the number is negative
mov [1500H], DX ; store in [1500hex] 1500H: DH, 1501H: DL
MOV AH, 4CH ; end of code interrupt
MOV AL, 0 ; zero return condition
INT 21H ; the end
negate_num:
not DX ; 1's comp
ADD DX, 01H ; 2's comp
mov [1500H], DX ; store in [1500hex] 1500H: DL, 1501H: DH
; my code ends here
MOV AH, 4CH
MOV AL, 0
INT 21H
CDSeg ENDS
END Start