|
| 1 | +; yasm -fbin -oaoc22_02.com aoc2022_02.asm && dosbox aoc22_02.com |
| 2 | +; yasm -fbin -oaoc22_02.com aoc2022_02.asm && dosbox -c "mount c: ." -c "c:" -c "td aoc22_02.com" |
| 3 | + |
| 4 | +BITS 16 |
| 5 | +CPU 8086 |
| 6 | +ORG 100h |
| 7 | + |
| 8 | + ; open file |
| 9 | + mov ax, 3D00h ; DOS function "open file" (for reading) |
| 10 | + mov dx, file |
| 11 | + int 21h |
| 12 | + jc io_err |
| 13 | + mov bx, ax ; save file handle for next operation |
| 14 | + |
| 15 | + ; read file contents into the (nearly) 64k of empty space in this .COM file |
| 16 | + mov ah, 3Fh ; DOS function "read file" |
| 17 | + mov cx, 0FF00h ; read as much as we (somewhat) safely can |
| 18 | + sub cx, dx |
| 19 | + int 21h |
| 20 | + jnc io_ok |
| 21 | +io_err: |
| 22 | + mov ax, 4C01h ; DOS function "terminate program" (with exit code 1) |
| 23 | + int 21h |
| 24 | +io_ok: |
| 25 | + mov si, dx ; store address of 'file' variable for later |
| 26 | + add dx, ax ; append null terminator to end of file data |
| 27 | + mov di, dx |
| 28 | + mov byte [di], 0 |
| 29 | + |
| 30 | + ; close file (optional - the OS will clean up our mess too, if needed :) |
| 31 | +; mov ah, 3Eh ; DOS function "close file" |
| 32 | +; int 21h |
| 33 | + |
| 34 | + ; now iterate over the input file |
| 35 | +nextchar: |
| 36 | + lodsb ; load a byte |
| 37 | + or al, al ; EOF reached? |
| 38 | + jz eof |
| 39 | + cmp al, 'C' ; is is possibly an XYZ value? |
| 40 | + ja .maybe_xyz |
| 41 | + sub al, 'A' ; not XYZ -> check for ABC (and map to ABC=012) |
| 42 | + cmp al, 3 |
| 43 | + jae nextchar ; not a valid character |
| 44 | + inc al ; save decoded ABC value (mapped to ABC=123) |
| 45 | + mov ah, al |
| 46 | + jmp nextchar |
| 47 | +.maybe_xyz: |
| 48 | + sub al, 'X' ; check for XYZ (and map to XYZ=012) |
| 49 | + cmp al, 3 |
| 50 | + jae nextchar ; not a valid character |
| 51 | + push ax ; save original result (will be clobbered in part 1 evaluation) |
| 52 | + |
| 53 | + ; part 1: check if we have won |
| 54 | + xor cx, cx ; reset score |
| 55 | + inc al ; remap our result to XYZ=123 |
| 56 | + cmp ah, al ; draw? |
| 57 | + je .p1_draw |
| 58 | + inc ah ; correct_guess = opponents_guess + 1 |
| 59 | + cmp ah, 4 ; ... with wrap-around |
| 60 | + jb .p1_nowrap |
| 61 | + sub ah, 3 |
| 62 | +.p1_nowrap: |
| 63 | + cmp ah, al ; won? |
| 64 | + jne .p1_finish ; if not, just add score |
| 65 | + add cl, 3 ; add 3 (of 6) points for win |
| 66 | +.p1_draw: |
| 67 | + add cl, 3 ; add 3 points for draw (or another 3 points for win) |
| 68 | +.p1_finish: |
| 69 | + add cl, al ; add score for local player's choice |
| 70 | + add [part1], cx ; add to accumulator |
| 71 | + |
| 72 | + ; part 2: create a suitable score |
| 73 | + pop ax ; restore the original input data |
| 74 | + xor cx, cx ; reset score |
| 75 | + add cl, al ; ... and set to 3 * winning_indicator |
| 76 | + add cl, al |
| 77 | + add cl, al |
| 78 | + add al, 2 ; convert winning_indicator to offset: lose=2, draw=3(=0 mod 3), win=4(=1 mod 3) |
| 79 | + add al, ah ; ... and add offset to opponent's choice to get our choice |
| 80 | +.p2_wrapcheck: |
| 81 | + cmp al, 4 ; check for wrap-around |
| 82 | + jb .p2_nowrap |
| 83 | + sub al, 3 |
| 84 | + jmp .p2_wrapcheck |
| 85 | +.p2_nowrap: |
| 86 | + add cl, al ; add score for local player's choice |
| 87 | + add [part2], cx ; add to accumulator |
| 88 | + |
| 89 | + jmp nextchar ; continue with next input character |
| 90 | + |
| 91 | +eof: |
| 92 | + mov ax, [part1] |
| 93 | + call OutputWord |
| 94 | + mov ax, [part2] |
| 95 | + ; now run right into the OutputWord function which will also exit the program |
| 96 | + |
| 97 | +; ----------------------------------------------------------------------------- |
| 98 | +; FUNCTION: OutputWord - output a word in decimal onto the screen |
| 99 | +; in AX = word to output |
| 100 | +; clobbers BX, CX, DX |
| 101 | +OutputWord: |
| 102 | + ; add terminating EOL to output stack |
| 103 | + mov bl, 0Ah |
| 104 | + push bx |
| 105 | + mov bl, 0Dh |
| 106 | + push bx |
| 107 | + |
| 108 | +.nextdigit: |
| 109 | + mov bx, 10 ; common quotient |
| 110 | + xor dx, dx ; clear upper part |
| 111 | + div bx ; divide |
| 112 | + add dl, '0' ; convert remainder into ASCII |
| 113 | + push dx ; ... and store to output stack |
| 114 | + or ax, ax ; another iteration if value is still nonzero |
| 115 | + jnz .nextdigit |
| 116 | + |
| 117 | + ; flush output stack |
| 118 | + mov ah, 2 ; DOS function "write character" |
| 119 | +.outchar: |
| 120 | + pop dx |
| 121 | + int 21h |
| 122 | + cmp dl, 0Ah |
| 123 | + jne .outchar |
| 124 | + |
| 125 | + ret |
| 126 | + |
| 127 | +; ----------------------------------------------------------------------------- |
| 128 | +; DATA SECTION |
| 129 | + |
| 130 | +; score accumulators for parts 1 and 2 |
| 131 | +part1 dw 0 |
| 132 | +part2 dw 0 |
| 133 | + |
| 134 | +; input filename; gets overwritten with file data later |
| 135 | +file: db "INPUT.TXT", 0 |
0 commit comments