-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.asm
107 lines (95 loc) · 2.55 KB
/
io.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
; Standard IO routines
; PROCEDURE WRITECH writes a single character to the monitor
; without a new-line. Character must be in dl register
;
writech PROC
mov ah,02h ; DOS write function
int 21h
ret
writech ENDP
;PROCEDURE WRITEINT calls writech to write a signed integer value
; to standard output. Integer must be in ax register
; USES ax,bx,cx,dx registers
;
writeint PROC
mov cx,1
mov dx,0
loop_div: mov bx,10
div bx
push dx
mov dx,0
cmp ax,0
je loop_wr
inc cx
jmp loop_div
loop_wr: pop dx
add dl,'0'
call writech
dec cx
cmp cx,0
jne loop_wr
ret
writeint ENDP
;PROCEDURE WRITESTR writes an ASCII$ string to standard output
; requires the offset of the character string be in DX
;;
;; usage: mov DX, OFFSET desired_string
;; call writestr
;;
writestr PROC ;writestr
push ax ;save contents of ax register
mov ah, 09 ;DOS string write
int 21h
pop ax
ret
writestr ENDP ;writestr
;PROCEDURE WRITELN writes a newline to standard output
;
writeln PROC ; writeln
push ax
push dx
mov ah,02h
mov dl,13
int 21h
mov dl,10
int 21h
pop dx
pop ax
ret
writeln ENDP ;writeln
; PROCEDURE READCH reads a single character from the keyboard
; and returns it in the al register
;
readch PROC ;readch
mov ah,01h ; DOS read function
int 21h
PUSH AX
MOV DL,' '
CALL WRITELN
POP AX
RET
readch ENDP ;readch
; PROCEDURE READINT reads an integer from the keyboard
; valid range is -32768 to 32767 does no error checking
; returns integer in bx register
readint PROC ;readint
mov bx,0
mov cx,10 ; base 10 multiplier
loop_read: mov ah,01h ; DOS read char function
int 21h
cmp al,' '
je end_read
cmp al,13
je end_read
push ax
mov ax,bx
imul cx
mov bx,ax
pop ax
sub al,'0'
mov ah,0
add bx,ax
jmp loop_read
end_read: call writeln
ret
readint ENDP ;readint