~ianloic/AdventOfCode2019

a6aeced8f346b58d6c1f55f400e2515e942fa990 — Ian McKellar 4 years ago fd270be
day 4, part 1: dos assembly language
3 files changed, 190 insertions(+), 0 deletions(-)

A 04-dos-asm/Makefile
A 04-dos-asm/part1.asm
A 04-dos-asm/part1.com
A 04-dos-asm/Makefile => 04-dos-asm/Makefile +7 -0
@@ 0,0 1,7 @@
all: part1.com

%.com: %.asm
	nasm -o $@ $+

run1: part1.com
	dosbox part1.com

A 04-dos-asm/part1.asm => 04-dos-asm/part1.asm +183 -0
@@ 0,0 1,183 @@
section .text
org 0x100

	call say_hello

main_loop:

	; find adjacent digits
	mov al, byte [number+0]
	cmp al, byte [number+1]
	je ascending
	mov al, byte [number+1]
	cmp al, byte [number+2]
	je ascending
	mov al, byte [number+2]
	cmp al, byte [number+3]
	je ascending
	mov al, byte [number+3]
	cmp al, byte [number+4]
	je ascending
	mov al, byte [number+4]
	cmp al, byte [number+5]
	jne thank_u_next

ascending:
	mov al, byte [number+0]
	cmp al, byte [number+1]
	jg thank_u_next
	mov al, byte [number+1]
	cmp al, byte [number+2]
	jg thank_u_next
	mov al, byte [number+2]
	cmp al, byte [number+3]
	jg thank_u_next
	mov al, byte [number+3]
	cmp al, byte [number+4]
	jg thank_u_next
	mov al, byte [number+4]
	cmp al, byte [number+5]
	jg thank_u_next

found:
	; found an answer!
	mov di, number
	call print_number
	mov di, count
	call increment_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


say_hello:
	mov	ah, 0x9
	mov	dx, hello
	int	0x21
	ret

; 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
	

; ; prints the count in hex
; print_count:
; 	; print 0x
; 	mov ah, 0x09
; 	mov dx, ox
; 	int 0x21

; 	mov cx, 7
; print_count_loop:
; 	mov ah, 0x02
; 	mov bx, word [count]
; 	shr bx, cl
; 	and bx, 0x000F
; 	mov dl, byte [hex + bx]
; 	int 0x21
; 	loop print_count_loop

; 	; xor ax, ax
; 	; mov dx, word [count]
; 	; mov cx, 10000
; 	; div cx
; 	; push dx
; 	; mov dx, ax
; 	; add dl, '0'
; 	; ; print to stdout
; 	; mov ah, 0x02
; 	; int 0x21
; 	; pop dx

; 	; print crlf
; 	mov ah, 0x9
; 	mov dx, crlf
; 	int 0x21
	; ret

overflow:
	mov ah, 0x9
	mov dx, hello
	int 0x21

	mov	ax, 0x4c00
	int	0x21

exit:

	mov di, count
	call print_number

	mov	ax, 0x4c00
	int	0x21

section .data
; constants
hello:	db 'Hello, world!' ; intentional fall-through
crlf: db 13, 10, '$'
repeats: db 'Number has repeats', 13, 10, '$'
ox: db '0x$'
hex: db '0123456789ABCDEF'

; number digits
number: db 2, 4, 0, 9, 2, 0

; number of possible passwords
count: db 0, 0, 0, 0, 0, 0
\ No newline at end of file

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