~ianloic/AdventOfCode2019

b4533c4a1216e74b55a589c659690a8429c47c1f — Ian McKellar 3 years ago a6aeced master
day 4.2: dos asm
3 files changed, 144 insertions(+), 1 deletions(-)

M 04-dos-asm/Makefile
A 04-dos-asm/part2.asm
A 04-dos-asm/part2.com
M 04-dos-asm/Makefile => 04-dos-asm/Makefile +4 -1
@@ 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

A 04-dos-asm/part2.asm => 04-dos-asm/part2.asm +140 -0
@@ 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

A 04-dos-asm/part2.com => 04-dos-asm/part2.com +0 -0