https://github.com/akkartik/mu/blob/master/407print-int32-decimal-right-justified.mu
1
2 fn print-int32-decimal-right-justified screen: (addr screen), n: int, _width: int {
3
4 var n-width/ecx: int <- int-width-decimal n
5 var width/eax: int <- copy _width
6 {
7 compare n-width, width
8 break-if->=
9 print-grapheme screen, 0x20
10 width <- decrement
11 loop
12 }
13 print-int32-decimal screen, n
14 }
15
16 fn int-width-decimal n: int -> result/ecx: int {
17 result <- copy 1
18 var curr/eax: int <- copy n
19
20 compare curr, 0
21 {
22 break-if->=
23 curr <- negate
24 result <- increment
25 }
26
27 {
28 compare curr, 0xa
29 break-if-<
30 curr <- try-divide curr, 0xa
31 result <- increment
32 loop
33 }
34 }
35
36 fn test-int-width-decimal {
37 var x/ecx: int <- int-width-decimal 0
38 check-ints-equal x, 1, "F - test-int-width-decimal: 0"
39 x <- int-width-decimal 1
40 check-ints-equal x, 1, "F - test-int-width-decimal: 1"
41 x <- int-width-decimal 4
42 check-ints-equal x, 1, "F - test-int-width-decimal: 4"
43 x <- int-width-decimal 9
44 check-ints-equal x, 1, "F - test-int-width-decimal: 9"
45 x <- int-width-decimal 0xa
46 check-ints-equal x, 2, "F - test-int-width-decimal: 10"
47 x <- int-width-decimal 0xb
48 check-ints-equal x, 2, "F - test-int-width-decimal: 11"
49 x <- int-width-decimal 0x4f
50 check-ints-equal x, 2, "F - test-int-width-decimal: 79"
51 x <- int-width-decimal 0x63
52 check-ints-equal x, 2, "F - test-int-width-decimal: 100"
53 x <- int-width-decimal 0x64
54 check-ints-equal x, 3, "F - test-int-width-decimal: 100"
55 x <- int-width-decimal 0x65
56 check-ints-equal x, 3, "F - test-int-width-decimal: 101"
57 x <- int-width-decimal 0x3e7
58 check-ints-equal x, 3, "F - test-int-width-decimal: 999"
59 x <- int-width-decimal 0x3e8
60 check-ints-equal x, 4, "F - test-int-width-decimal: 1000"
61 x <- int-width-decimal -1
62 check-ints-equal x, 2, "F - test-int-width-decimal: -1"
63 x <- int-width-decimal -0xb
64 check-ints-equal x, 3, "F - test-int-width-decimal: -11"
65 }