Skip to content

Commit

Permalink
6781 - new app: RPN (postfix) calculator
Browse files Browse the repository at this point in the history
This was surprisingly hard; bugs discovered all over the place.
  • Loading branch information
akkartik committed Sep 16, 2020
1 parent 8815cf7 commit ae470b4
Show file tree
Hide file tree
Showing 23 changed files with 451 additions and 12 deletions.
6 changes: 3 additions & 3 deletions 118parse-hex-int.subx
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ parse-hex-int-from-slice: # in: (addr slice) -> result/eax: int
52/push-edx
# ecx = in
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx
# edx = in->end
# var max/edx: (addr byte) = in->end
8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 2/r32/edx 4/disp8 . # copy *(ecx+4) to edx
# var curr/ecx: (addr byte) = in->start
8b/copy 0/mod/indirect 1/rm32/ecx . . . 1/r32/ecx . . # copy *ecx to ecx
Expand Down Expand Up @@ -582,7 +582,7 @@ test-parse-hex-int-from-slice-0x-prefix:
e8/call parse-hex-int-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 0x34a, msg)
# check-ints-equal(eax, 0x34, msg)
# . . push args
68/push "F - test-parse-hex-int-from-slice-0x-prefix"/imm32
68/push 0x34/imm32
Expand Down Expand Up @@ -616,7 +616,7 @@ test-parse-hex-int-from-slice-zero:
e8/call parse-hex-int-from-slice/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
# check-ints-equal(eax, 0x34a, msg)
# check-ints-equal(eax, 0, msg)
# . . push args
68/push "F - test-parse-hex-int-from-slice-zero"/imm32
68/push 0/imm32
Expand Down
7 changes: 3 additions & 4 deletions 127next-word.subx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ next-word: # line: (addr stream byte), out: (addr slice)
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi
# edi = out
8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi
# skip-chars-matching(line, ' ')
# skip-chars-matching-whitespace(line)
# . . push args
68/push 0x20/imm32/space
ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
# . . call
e8/call skip-chars-matching/disp32
e8/call skip-chars-matching-whitespace/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
$next-word:check0:
# if (line->read >= line->write) clear out and return
# . eax = line->read
Expand Down
19 changes: 19 additions & 0 deletions 304screen.subx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ $print-string-to-real-screen:end:
5d/pop-to-ebp
c3/return

print-slice-to-real-screen: # s: (addr slice)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
#
(write-slice-buffered Stdout *(ebp+8))
(flush Stdout)
$print-slice-to-real-screen:end:
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

print-stream-to-real-screen: # s: (addr stream byte)
# . prologue
55/push-ebp
Expand Down Expand Up @@ -240,6 +253,8 @@ write-int32-decimal-buffered: # f: (addr buffered-file), n: int
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
# var ecx: (stream byte 16)
81 5/subop/subtract %esp 0x10/imm32
68/push 0x10/imm32/size
Expand All @@ -249,6 +264,10 @@ write-int32-decimal-buffered: # f: (addr buffered-file), n: int
(write-int32-decimal %ecx *(ebp+0xc))
(write-stream-data *(ebp+8) %ecx)
$write-int32-decimal-buffered:end:
# . reclaim locals
81 0/subop/add %esp 0x1c/imm32
# . restore registers
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
Expand Down
16 changes: 16 additions & 0 deletions 305keyboard.subx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ $read-key-from-real-keyboard:end:
5d/pop-to-ebp
c3/return

read-line-from-real-keyboard: # in: (addr stream byte)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
#
(read 0 *(ebp+8)) # => eax
$read-line-from-real-keyboard:end:
# . restore registers
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

== data

# iflags: octal hex
Expand Down
2 changes: 1 addition & 1 deletion 308allocate-array.subx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allocate-array2: # ad: (addr allocation-descriptor), array-len: int, elem-size:
52/push-edx
#
8b/-> *(ebp+0xc) 0/r32/eax
f7 4/subop/multiply-into-eax-edx *(ebp+0x10)
f7 4/subop/multiply-into-edx-eax *(ebp+0x10)
# TODO: check edx for overflow
(allocate-array *(ebp+8) %eax *(ebp+0x14))
$allocate-array2:end:
Expand Down
233 changes: 233 additions & 0 deletions 311parse-decimal-int.subx
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
# Helpers for parsing decimal ints.

parse-decimal-int-from-slice: # in: (addr slice) -> out/eax: int # . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
# ecx = in
8b/-> *(ebp+8) 1/r32/ecx
#
(parse-decimal-int-helper *ecx *(ecx+4)) # => eax
$parse-decimal-int-from-slice:end:
# . restore registers
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

parse-decimal-int-helper: # start: (addr byte), end: (addr byte) -> result/eax: int
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
51/push-ecx
52/push-edx
53/push-ebx
56/push-esi
57/push-edi
# var curr/esi: (addr byte) = start
8b/-> *(ebp+8) 6/r32/esi
# edi = end
8b/-> *(ebp+0xc) 7/r32/edi
# var negate?/edx: boolean = false
ba/copy-to-edx 0/imm32/false
# if (*curr == '-') ++curr, negate = true
{
$parse-decimal-int-helper:negative:
b8/copy-to-eax 0/imm32
8a/copy-byte *esi 0/r32/AL
3d/compare-eax-and 0x2d/imm32/-
75/jump-if-!= break/disp8
# . ++curr
46/increment-esi
# . negate = true
ba/copy-to-edx 1/imm32/true
}
# spill negate?
52/push-edx
# var result/eax: int = 0
b8/copy-to-eax 0/imm32
# var digit/ecx: int = 0
b9/copy-to-ecx 0/imm32
# const TEN/ebx: int = 10
bb/copy-to-ebx 0xa/imm32
{
$parse-decimal-int-helper:loop:
# if (curr >= in->end) break
39/compare %esi 7/r32/edi
73/jump-if-addr>= break/disp8
# digit = from-decimal-char(*curr)
8a/copy-byte *esi 1/r32/CL
81 5/subop/subtract %ecx 0x30/imm32/zero
# TODO: error checking
# result = result * 10 + digit
ba/copy-to-edx 0/imm32
f7 4/subop/multiply-into-edx-eax %ebx
# TODO: check edx for overflow
01/add %eax 1/r32/ecx
# ++curr
46/increment-esi
#
eb/jump loop/disp8
}
$parse-decimal-int-helper:negate:
# if (negate?) result = -result
5a/pop-to-edx
{
81 7/subop/compare %edx 0/imm32/false
74/jump-if-= break/disp8
f7 3/subop/negate %eax
}
$parse-decimal-int-helper:end:
# . restore registers
5f/pop-to-edi
5e/pop-to-esi
5b/pop-to-ebx
5a/pop-to-edx
59/pop-to-ecx
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

test-parse-decimal-int-from-slice-single-digit:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "3"
b8/copy-to-eax "3"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 3 "F - test-parse-decimal-int-from-slice-single-digit")
$test-parse-decimal-int-helper-single-digit:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

test-parse-decimal-int-from-slice-multi-digit:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "34"
b8/copy-to-eax "34"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 0x22 "F - test-parse-decimal-int-from-slice-multi-digit") # 34 in hex
$test-parse-decimal-int-helper-multi-digit:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

test-parse-decimal-int-from-slice-zero:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "00"
b8/copy-to-eax "00"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax 0 "F - test-parse-decimal-int-from-slice-zero")
$test-parse-decimal-int-helper-zero:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

test-parse-decimal-int-from-slice-negative:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "-3"
b8/copy-to-eax "-3"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax -3 "F - test-parse-decimal-int-from-slice-negative")
$test-parse-decimal-int-helper-negative:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

test-parse-decimal-int-from-slice-multi-digit-negative:
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
# . save registers
50/push-eax
51/push-ecx
# (eax..ecx) = "-32"
b8/copy-to-eax "-32"/imm32
8b/-> *eax 1/r32/ecx
8d/copy-address *(eax+ecx+4) 1/r32/ecx
05/add-to-eax 4/imm32
# var slice/ecx: slice = {eax, ecx}
51/push-ecx
50/push-eax
89/<- %ecx 4/r32/esp
#
(parse-decimal-int-from-slice %ecx) # => eax
(check-ints-equal %eax -0x20 "F - test-parse-decimal-int-from-slice-multi-digit-negative") # -32 in hex
$test-parse-decimal-int-helper-multi-digit-negative:end:
# . restore registers
59/pop-to-ecx
58/pop-to-eax
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return
12 changes: 8 additions & 4 deletions 400.mu
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ sig parse-hex-int-from-slice in: (addr slice) -> result/eax: int
#sig parse-hex-int-helper start: (addr byte), end: (addr byte) -> result/eax: int
sig is-hex-digit? c: byte -> result/eax: boolean
#sig from-hex-char in/eax: byte -> out/eax: nibble
sig parse-decimal-int-from-slice in: (addr slice) -> result/eax: int
#sig parse-decimal-int-helper start: (addr byte), end: (addr byte) -> result/eax: int
sig error-byte ed: (addr exit-descriptor), out: (addr buffered-file), msg: (addr array byte), n: byte
#sig allocate ad: (addr allocation-descriptor), n: int, out: (addr handle _)
#sig allocate-raw ad: (addr allocation-descriptor), n: int, out: (addr handle _)
Expand Down Expand Up @@ -127,10 +129,10 @@ sig compute-width-of-slice s: (addr slice) -> result/eax: int
sig emit-hex-array out: (addr buffered-file), arr: (addr array byte)
sig next-word-or-string line: (addr stream byte), out: (addr slice)
sig write-int out: (addr stream byte), n: int
sig clear-stack s: (addr stack)
sig push s: (addr stack), n: int
sig pop s: (addr stack) -> n/eax: int
sig top s: (addr stack) -> n/eax: int
#sig clear-stack s: (addr stack)
#sig push s: (addr stack), n: int
#sig pop s: (addr stack) -> n/eax: int
#sig top s: (addr stack) -> n/eax: int
sig array-equal? a: (addr array int), b: (addr array int) -> result/eax: boolean
sig parse-array-of-ints ad: (addr allocation-descriptor), s: (addr string), out: (addr handle array int)
sig check-array-equal a: (addr array int), expected: (addr string), msg: (addr string)
Expand All @@ -143,6 +145,7 @@ sig real-screen-size -> nrows/eax: int, ncols/ecx: int
sig clear-real-screen
sig move-cursor-on-real-screen row: int, column: int
sig print-string-to-real-screen s: (addr array byte)
sig print-slice-to-real-screen s: (addr slice)
sig print-stream-to-real-screen s: (addr stream byte)
sig print-grapheme-to-real-screen c: grapheme
sig print-int32-hex-to-real-screen n: int
Expand All @@ -159,6 +162,7 @@ sig show-cursor-on-real-screen
sig enable-keyboard-immediate-mode
sig enable-keyboard-type-mode
sig read-key-from-real-keyboard -> result/eax: byte
sig read-line-from-real-keyboard in: (addr stream byte)
sig open filename: (addr array byte), write?: boolean, out: (addr handle buffered-file)
sig populate-buffered-file-containing contents: (addr array byte), out: (addr handle buffered-file)
sig new-buffered-file out: (addr handle buffered-file)
Expand Down
Binary file modified apps/assort
Binary file not shown.
Binary file modified apps/braces
Binary file not shown.
Binary file modified apps/calls
Binary file not shown.
Binary file modified apps/crenshaw2-1
Binary file not shown.
Binary file modified apps/crenshaw2-1b
Binary file not shown.
Binary file modified apps/dquotes
Binary file not shown.
Binary file modified apps/factorial
Binary file not shown.
Binary file modified apps/hex
Binary file not shown.
Binary file modified apps/mu
Binary file not shown.
Loading

0 comments on commit ae470b4

Please sign in to comment.