about summary refs log tree commit diff stats
path: root/subx/064write-byte.subx
diff options
context:
space:
mode:
Diffstat (limited to 'subx/064write-byte.subx')
-rw-r--r--subx/064write-byte.subx111
1 files changed, 94 insertions, 17 deletions
diff --git a/subx/064write-byte.subx b/subx/064write-byte.subx
index 9f97c336..606cc7d3 100644
--- a/subx/064write-byte.subx
+++ b/subx/064write-byte.subx
@@ -1,4 +1,4 @@
-# write-byte: write a single byte to a buffered-file. The write may be buffered.
+# write-byte-buffered: add a single byte to a buffered-file.
 # flush: write out any buffered writes to disk.
 #
 # TODO: Come up with a way to signal failure to write to disk. This is hard
@@ -28,7 +28,7 @@ Stdout:
 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 
 # Write lower byte of 'n' to 'f'.
-write-byte:  # f : (address buffered-file), n : int -> <void>
+write-byte-buffered:  # f : (address buffered-file), n : int -> <void>
     # . prolog
     55/push-EBP
     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
@@ -41,7 +41,7 @@ write-byte:  # f : (address buffered-file), n : int -> <void>
     8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           1/r32/ECX   4/disp8         .                 # copy *(EDI+4) to ECX
     # if (f->write >= f->length) flush and clear f's stream
     3b/compare                      1/mod/*+disp8   7/rm32/EDI    .           .             .           1/r32/ECX   0xc/disp8       .                 # compare ECX with *(EDI+12)
-    7c/jump-if-lesser  $write-byte:to-stream/disp8
+    7c/jump-if-lesser  $write-byte-buffered:to-stream/disp8
     # . flush(f)
     # . . push args
     57/push-EDI
@@ -59,7 +59,7 @@ write-byte:  # f : (address buffered-file), n : int -> <void>
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
     # . f->write must now be 0; update its cache at ECX
     31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
-$write-byte:to-stream:
+$write-byte-buffered:to-stream:
     # write to stream
     # f->data[f->write] = LSB(n)
     31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
@@ -67,7 +67,7 @@ $write-byte:to-stream:
     88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           0/r32/AL    0x10/disp8      .                 # copy AL to *(EDI+ECX+16)
     # ++f->write
     ff          0/subop/increment   1/mod/*+disp8   7/rm32/EDI    .           .             .           .           4/disp8         .                 # increment *(EDI+4)
-$write-byte:end:
+$write-byte-buffered:end:
     # . restore registers
     5f/pop-to-EDI
     59/pop-to-ECX
@@ -103,10 +103,8 @@ $flush:end:
     5d/pop-to-EBP
     c3/return
 
-# - tests
-
-test-write-byte-single:
-    # - check that write-byte writes to first byte of 'file'
+test-write-byte-buffered-single:
+    # - check that write-byte-buffered writes to first byte of 'file'
     # setup
     # . clear-stream(_test-stream)
     # . . push args
@@ -124,12 +122,12 @@ test-write-byte-single:
     e8/call  clear-stream/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
-    # write-byte(_test-buffered-file, 'A')
+    # write-byte-buffered(_test-buffered-file, 'A')
     # . . push args
     68/push  0x41/imm32
     68/push  _test-buffered-file/imm32
     # . . call
-    e8/call  write-byte/disp32
+    e8/call  write-byte-buffered/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
     # flush(_test-buffered-file)
@@ -141,7 +139,7 @@ test-write-byte-single:
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
     # check-stream-equal(_test-stream, "A", msg)
     # . . push args
-    68/push  "F - test-write-byte-single"/imm32
+    68/push  "F - test-write-byte-buffered-single"/imm32
     68/push  "A"/imm32
     68/push  _test-stream/imm32
     # . . call
@@ -151,8 +149,8 @@ test-write-byte-single:
     # . end
     c3/return
 
-test-write-byte-multiple-flushes:
-    # - check that write-byte correctly flushes buffered data
+test-write-byte-buffered-multiple-flushes:
+    # - check that write-byte-buffered correctly flushes buffered data
     # setup
     # . clear-stream(_test-stream)
     # . . push args
@@ -181,12 +179,12 @@ test-write-byte-multiple-flushes:
     e8/call  write/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
-    # write-byte(_test-buffered-file, 'g')
+    # write-byte-buffered(_test-buffered-file, 'g')
     # . . push args
     68/push  0x67/imm32
     68/push  _test-buffered-file/imm32
     # . . call
-    e8/call  write-byte/disp32
+    e8/call  write-byte-buffered/disp32
     # . . discard args
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
     # flush(_test-buffered-file)
@@ -198,7 +196,7 @@ test-write-byte-multiple-flushes:
     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
     # check-stream-equal(_test-stream, "abcdef", msg)
     # . . push args
-    68/push  "F - test-write-byte-multiple-flushes: 1"/imm32
+    68/push  "F - test-write-byte-buffered-multiple-flushes: 1"/imm32
     68/push  "abcdefg"/imm32
     68/push  _test-stream/imm32
     # . . call
@@ -208,4 +206,83 @@ test-write-byte-multiple-flushes:
     # . end
     c3/return
 
+# - variant without buffering
+
+# Write lower byte of 'n' to 'f'.
+append-byte:  # f : (address stream), n : int -> <void>
+    # . prolog
+    55/push-EBP
+    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
+    # . save registers
+    51/push-ECX
+    57/push-EDI
+    # EDI = f
+    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
+    # ECX = f->write
+    8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
+    # if (f->write >= f->length) abort
+    3b/compare                      1/mod/*+disp8   7/rm32/EDI    .           .             .           1/r32/ECX   8/disp8         .                 # compare ECX with *(EDI+8)
+    7d/jump-if-greater-or-equal  $append-byte:abort/disp8
+$append-byte:to-stream:
+    # write to stream
+    # f->data[f->write] = LSB(n)
+    31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
+    8a/copy-byte                    1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/AL    0xc/disp8       .                 # copy byte at *(EBP+12) to AL
+    88/copy-byte                    1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           0/r32/AL    0xc/disp8       .                 # copy AL to *(EDI+ECX+12)
+    # ++f->write
+    ff          0/subop/increment   0/mod/indirect  7/rm32/EDI    .           .             .           .           .               .                 # increment *EDI
+$append-byte:end:
+    # . restore registers
+    5f/pop-to-EDI
+    59/pop-to-ECX
+    # . epilog
+    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
+    5d/pop-to-EBP
+    c3/return
+
+$append-byte:abort:
+    # . _write(2/stderr, error)
+    # . . push args
+    68/push  "append-byte: out of space"/imm32
+    68/push  2/imm32/stderr
+    # . . call
+    e8/call  _write/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # . syscall(exit, 1)
+    bb/copy-to-EBX  1/imm32
+    b8/copy-to-EAX  1/imm32/exit
+    cd/syscall  0x80/imm8
+    # never gets here
+
+test-append-byte-single:
+    # - check that append-byte writes to first byte of 'file'
+    # setup
+    # . clear-stream(_test-stream)
+    # . . push args
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  clear-stream/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
+    # append-byte(_test-stream, 'A')
+    # . . push args
+    68/push  0x41/imm32
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  append-byte/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
+    # check-stream-equal(_test-stream, "A", msg)
+    # . . push args
+    68/push  "F - test-append-byte-single"/imm32
+    68/push  "A"/imm32
+    68/push  _test-stream/imm32
+    # . . call
+    e8/call  check-stream-equal/disp32
+    # . . discard args
+    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
+    # . end
+    c3/return
+
 # . . vim:nowrap:textwidth=0
id='n404' href='#n404'>404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650