From b4533c4a1216e74b55a589c659690a8429c47c1f Mon Sep 17 00:00:00 2001 From: Ian McKellar Date: Wed, 4 Dec 2019 14:27:06 -0800 Subject: [PATCH] day 4.2: dos asm --- 04-dos-asm/Makefile | 5 +- 04-dos-asm/part2.asm | 140 +++++++++++++++++++++++++++++++++++++++++++ 04-dos-asm/part2.com | Bin 0 -> 216 bytes 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 04-dos-asm/part2.asm create mode 100644 04-dos-asm/part2.com diff --git a/04-dos-asm/Makefile b/04-dos-asm/Makefile index 35f4165..6c10a8f 100644 --- a/04-dos-asm/Makefile +++ b/04-dos-asm/Makefile @@ -1,7 +1,10 @@ -all: part1.com +all: part1.com part2.com %.com: %.asm nasm -o $@ $+ run1: part1.com dosbox part1.com + +run2: part2.com + dosbox part2.com diff --git a/04-dos-asm/part2.asm b/04-dos-asm/part2.asm new file mode 100644 index 0000000..53e3b91 --- /dev/null +++ b/04-dos-asm/part2.asm @@ -0,0 +1,140 @@ +section .text +org 0x100 + + +main_loop: + + ; make sure digits aren't descending + mov bx, 0 +ascending_loop: + mov al, byte [number+0+bx] + cmp al, byte [number+1+bx] + jg thank_u_next + inc bx + cmp bx, 5 + jne ascending_loop + +adjacent: + ; find adjacent digits + mov bx, 0 +adjacent_loop: + mov al, byte [number+bx] + cmp al, byte [number+bx+1] + je found_adjacent +continue_adjacent: + inc bx + cmp bx, 5 + jne adjacent_loop + jmp thank_u_next + +skip_rest_of_group: + inc bx + cmp bx, 5 + je thank_u_next + mov al, byte [number+bx] + cmp al, byte [number+bx+1] + je skip_rest_of_group + jmp adjacent_loop + +found_adjacent: + mov al, byte [number+bx] + cmp al, byte [number+bx+2] + je skip_rest_of_group ; group of 3 or more + ; found exactly two + jmp found + +found: + ; found an answer! + mov di, count + call increment_number + jmp thank_u_next + + mov di, number + call print_number + +thank_u_next: +; increment nuber + mov di, number + call increment_number + +; loop if we haven't reached 789857 + cmp byte [number+0], 7 + jne main_loop + cmp byte [number+1], 8 + jne main_loop + cmp byte [number+2], 9 + jne main_loop + cmp byte [number+3], 8 + jne main_loop + cmp byte [number+4], 5 + jne main_loop + cmp byte [number+5], 7 + jne main_loop + + jmp exit + + + +; increment the number +increment_number: + ; bl holds the digit number + mov bx, 5 +increment_number_loop: + inc byte [di + bx] + cmp byte [di + bx], 10 + jne increment_number_done + ; digit overflowed + mov byte [di + bx], 0 + cmp bx, 0 + je increment_number_overflow + dec bx + jmp increment_number_loop +increment_number_done: + ret +increment_number_overflow: + ; panic + int 3 + +; prints the number +print_number: + mov bx, 0 +print_number_loop: + ; load next digit + mov dl, [ds:di + bx] + ; make it ascii + add dl, '0' + ; print to stdout + mov ah, 0x02 + int 0x21 + + inc bl + cmp bl, 6 + jne print_number_loop + + ; print crlf + mov ah, 0x9 + mov dx, crlf + int 0x21 + ret + + + + +exit: + + mov di, count + call print_number + + mov ax, 0x4c00 + int 0x21 + +section .data +; constants +crlf: db 13, 10, '$' + +; number digits +number: db 2, 4, 0, 9, 2, 0 +past_the_number: db 0xff ; to make comparisons simpler + +; number of possible passwords +count: db 0, 0, 0, 0, 0, 0 \ No newline at end of file diff --git a/04-dos-asm/part2.com b/04-dos-asm/part2.com new file mode 100644 index 0000000000000000000000000000000000000000..14a6eeb49dc20d89f8d88334c64fe4820ebbe8b7 GIT binary patch literal 216 zcmdnZz|hrxn$fEL3}d~!bMtT3(hs{4LM2iVq1QSfx0S@c)xyk_$)KfF?L;o4TlW2Fr8KWcevp< nTj~2PoV!jio>e>yvi08o9SlBa6&ZNBRG3&8IGGs!Gk^gAc63