-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.s
114 lines (105 loc) · 2.06 KB
/
print.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
;
; Printing routines
;
; Steve Maddison, 24/02/2007
;
print_float:
ret
; In: BCDE
print_hex_32:
push hl
push bc
pop hl
call print_hex_16
push de
pop hl
call print_hex_16
pop hl
ret
; In: HL
print_hex_16:
push af
ld a,h
call print_hex_8
ld a,l
call print_hex_8
pop af
ret
; In: A
print_hex_8: push af ; save
and 0xf0
srl a ; shift upper nibble >> lower nibble
srl a
srl a
srl a
call print_hex_4 ; print upper nibble
pop af ; restore
call print_hex_4 ; print lower nibble
ret
print_hex_4: and 0x0f ; lower nibble only
cp 0x0a ; check for A-F
jp c,print_hex_4_numeric
add a,39 ; offset between chars '0' and 'a'
print_hex_4_numeric:
add a,'0'
call console_outb
ret
; Print signed 16-bit integer in HL
print_int_16: push af
push bc
push de
; most significant bit of HL is set, number is negative
bit 7,h
jp z,print_int_16_positive
; print a "-" and get the absolute value of HL
ld a,'-'
call console_outb
call int_2s_comp_16
print_int_16_positive:
; Test for special case of HL = 0
ld a,h
or l
jp nz,print_int_16_non_zero
ld a,'0'
call console_outb
jp print_int_16_end
print_int_16_non_zero:
ld de,10 ; Initialise divisor of 10
xor a ; push a zero onto the stack
push af
push hl ; set up for division subroutine
pop bc
print_int_16_div_loop:
; Keep dividing by 10 until there's nothing left, pushing
; the character representing the remainder to the stack
; each iteration. Yes, pushing F to the stack is wasteful
; but it saves a lot of work here.
call int_div_16
ld a,l
add a,'0'
push af
ld a,b ; check for end case: BC = 0
or c
jp nz,print_int_16_div_loop
print_int_16_output:
; Pop the first character in order to simplify the loop below.
pop af
print_int_16_output_loop:
; Keep printing and poping until we hit the zero we pushed
; during initialisation.
call console_outb
pop af
cp 0
jp nz,print_int_16_output_loop
print_int_16_end:
pop de
pop bc
pop af
ret
print_int_8:
push hl
ld h,0
ld l,a
call print_int_16
pop hl
ret