-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprints.asm
148 lines (119 loc) · 1.77 KB
/
prints.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
[ORG 0x7C00] ;Origin, tell the assembler that where the code will
[BITS 16]
mov cx, 0x31
push cx
mov cx, 0x32
push cx
mov ax, word ss:[esp]
call print_char
push 0x33
mov ax, word ss:[esp]
call print_char
mov byte [i], 0x35
mov ax, word [i]
call print_char
jmp $
jmp print_int
print_hello:
mov ah, 0x0e
mov bx, 0
mov edx, hello
mov al, [edx]
int 0x10
inc edx
inc bx
cmp bx, helloLen
jl print_hello
; ax = input
print_int:
mov ax, 27
mov si, ax
call count_digits ; cx = digits number
dec cx
mov ax, 10
call pow_int
mov ax, si
mov si, 10
print_int_loop:
xor dx, dx
div cx
call print_digit
mov ax, dx
mov bx, dx ; save remainder
xor dx, dx
mov ax, cx
div si
mov cx, ax
mov ax, bx
cmp cx, 0
ja print_int_loop
ret
; xor dx, dx
; print_digit:
; xor dx, dx
; mov ax, 1234
; mov bx, 1000
; div bx ; ax = quotient dx = remainder
; mov bx, ax ; bx = quotient
; add bx, 0x30
; mov ah, 0x0e
; mov al, bl
; int 0x10
; mov ax, dx
; xor dx, dx
; mov bx, 100
; div bx
; mov bx, ax ; bx = quotient
; add bx, 0x30
; mov ah, 0x0e
; mov al, bl
; int 0x10
jmp $
; ax = input
; cx = output
count_digits:
mov cx, 0
cd_loop:
inc cx
xor dx, dx
mov bx, 10
div bx
cmp ax, 0
jne cd_loop
ret
; ax = input
; cx = power
; cx = output
pow_int:
mov bx, ax
mov ax, 1
pow_int_loop:
jcxz pow_int_done
mul bx
loop pow_int_loop
pow_int_done:
mov cx, ax
ret
; ax = input
print_digit:
mov bx, ax
xor ax, ax
add bx, 0x30
mov ah, 0x0e
mov al, bl
int 0x10
ret
; ax = input
print_char:
mov bx, ax
xor ax, ax
mov ah, 0x0e
mov al, bl
int 0x10
ret
i: db 0
ten: db 10
hello: db 'Hello, world!'
helloLen: equ $-hello
times 510 - ($ - $$) db 0
dw 0xaa55