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
|
# print 'n' with enough leading spaces to be right-justified in 'width'
fn print-int32-decimal-right-justified screen: (addr screen), n: int, _width: int {
# tweak things for negative numbers
var n-width/ecx: int <- int-width-decimal n
var width/eax: int <- copy _width
{
compare n-width, width
break-if->=
print-grapheme screen, 0x20 # space
width <- decrement
loop
}
print-int32-decimal screen, n
}
fn int-width-decimal n: int -> result/ecx: int {
result <- copy 1
var curr/eax: int <- copy n
# account for '-'
compare curr, 0
{
break-if->=
curr <- negate
result <- increment
}
# now we're dealing with a positive number
{
compare curr, 0xa
break-if-<
curr <- try-divide curr, 0xa
result <- increment
loop
}
}
fn test-int-width-decimal {
var x/ecx: int <- int-width-decimal 0
check-ints-equal x, 1, "F - test-int-width-decimal: 0"
x <- int-width-decimal 1
check-ints-equal x, 1, "F - test-int-width-decimal: 1"
x <- int-width-decimal 4
check-ints-equal x, 1, "F - test-int-width-decimal: 4"
x <- int-width-decimal 9
check-ints-equal x, 1, "F - test-int-width-decimal: 9"
x <- int-width-decimal 0xa
check-ints-equal x, 2, "F - test-int-width-decimal: 10"
x <- int-width-decimal 0xb
check-ints-equal x, 2, "F - test-int-width-decimal: 11"
x <- int-width-decimal 0x4f # 79
check-ints-equal x, 2, "F - test-int-width-decimal: 79"
x <- int-width-decimal 0x63 # 99
check-ints-equal x, 2, "F - test-int-width-decimal: 100"
x <- int-width-decimal 0x64 # 100
check-ints-equal x, 3, "F - test-int-width-decimal: 100"
x <- int-width-decimal 0x65 # 101
check-ints-equal x, 3, "F - test-int-width-decimal: 101"
x <- int-width-decimal 0x3e7 # 999
check-ints-equal x, 3, "F - test-int-width-decimal: 999"
x <- int-width-decimal 0x3e8 # 1000
check-ints-equal x, 4, "F - test-int-width-decimal: 1000"
x <- int-width-decimal -1
check-ints-equal x, 2, "F - test-int-width-decimal: -1"
x <- int-width-decimal -0xb # -11
check-ints-equal x, 3, "F - test-int-width-decimal: -11"
}
|