-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint_bios.s
158 lines (121 loc) · 2.13 KB
/
print_bios.s
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
149
150
151
152
153
154
155
156
157
158
.text
.globl print_char, print_string, print_int, print_endl, print_hex
.code16
# Print a character pushed onto the stack
print_char:
enter $0, $0 # makes sure that bp is what is needed
pusha
# we've only pushed a letter on to the stack => a byte is enough
movb 4(%bp), %al # 4(%bp):parameter, 2(%bp):return address, (%bp): subprogram data
movb $0x0e, %ah
movb $0x00, %bh
movb $0x02, %bl
int $0x10
popa
leave
ret
# Print a string pushed onto the stack, specified by a pointer, will print until encountering '\0'
print_string:
enter $0, $0
pusha
#movw 4(%bp), %cx
movw 4(%bp), %bx
print_string_loop:
pushw (%bx)
call print_char
addw $2, %sp
addw $1, %bx
movw (%bx), %ax
testb %al, %al
jne print_string_loop
#decw %cx
#cmp $0, %cx
#jg print_string_loop
popa
leave
ret
# Print an integer
print_int:
enter $0, $0
pusha
# %ax = number to be displayed
movw 4(%bp), %ax
# %cx = number of digits it has
movw $0, %cx
# We'll be displaying a base 10 number
movw $10, %bx
div_loop:
movw $0, %dx
# Divides %dx:%ax to %bx
divw %bx
# Remainder is in %dx, quotient in %ax
pushw %dx
inc %cx
cmpw $0, %ax
jg div_loop
print_int_loop:
popw %ax
addw $'0', %ax
pushw %ax
call print_char
add $2, %sp
dec %cx
cmp $0, %cx
jg print_int_loop
popa
leave
ret
# Print a hexadecimal number
print_hex:
enter $0, $0
pusha
# %ax = number to be displayed
movw 4(%bp), %ax
# %cx = number of digits it has
movw $0, %cx
# We'll be displaying a base 10 number
movw $16, %bx
hex_div_loop:
movw $0, %dx
# Divides %dx:%ax to %bx
divw %bx
# Remainder is in %dx, quotient in %ax
pushw %dx
inc %cx
cmpw $0, %ax
jg hex_div_loop
pushw $'0'
call print_char
add $2, %sp
pushw $'x'
call print_char
add $2, %sp
print_hex_loop:
popw %ax
cmpw $0xa, %ax
jge hex_letter
addw $'0', %ax
jmp print_hex_char
hex_letter:
subw $10, %ax
addw $'a', %ax
xorb %ah, %ah
print_hex_char:
pushw %ax
call print_char
add $2, %sp
dec %cx
cmp $0, %cx
jg print_hex_loop
popa
leave
ret
# Print a newline
print_endl:
pushw $'\r'
call print_char
add $2, %sp
pushw $'\n'
call print_char
add $2, %sp
ret