Created
May 8, 2018 09:33
-
-
Save hakatashi/55fd0603b6d45b7213c61af5449fdc76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; z80asm z80.asm && docker run -v `pwd | sed "s /mnt "`:/golf esolang/z80 sh -c "z80 /golf/a.bin < /golf/in" | xxd | head | |
ld ix, $1000 | |
call read_int | |
ld ix, $1001 | |
call read_int | |
ld ix, $1002 | |
call read_int | |
ld ix, $1003 | |
call read_int | |
ld ix, $1004 | |
call read_int | |
ld ix, $1005 | |
call read_int | |
ld ix, $1006 | |
call read_int | |
ld ix, $1007 | |
call read_int | |
ld ix, $1008 | |
call read_int | |
ld ix, $1009 | |
call read_int | |
ld ix, $100A | |
call read_int | |
ld ix, $100B | |
call read_int | |
ld ix, $1000 | |
ld a, ($1003) | |
sub (ix+$0) | |
ld ($1003), a | |
ld a, ($1006) | |
sub (ix+$0) | |
ld ($1006), a | |
ld a, ($1009) | |
sub (ix+$0) | |
ld ($1009), a | |
ld ix, $1001 | |
ld a, ($1004) | |
sub (ix+$0) | |
ld ($1004), a | |
ld a, ($1007) | |
sub (ix+$0) | |
ld ($1007), a | |
ld a, ($100A) | |
sub (ix+$0) | |
ld ($100A), a | |
ld ix, $1002 | |
ld a, ($1005) | |
sub (ix+$0) | |
ld ($1005), a | |
ld a, ($1008) | |
sub (ix+$0) | |
ld ($1008), a | |
ld a, ($100B) | |
sub (ix+$0) | |
ld ($100B), a | |
ld ix, $2000 | |
ld (ix+0), 3 | |
ld (ix+1), 7 | |
ld (ix+2), 11 | |
call calc_mul3 | |
call nz, negate | |
ld a, (ix+3) | |
ld (ix+6), a | |
ld a, (ix+4) | |
ld (ix+7), a | |
ld a, (ix+5) | |
ld (ix+8), a | |
ld (ix+0), 4 | |
ld (ix+1), 8 | |
ld (ix+2), 9 | |
call calc_mul3 | |
call nz, negate | |
call add3 | |
ld (ix+0), 5 | |
ld (ix+1), 6 | |
ld (ix+2), 10 | |
call calc_mul3 | |
call nz, negate | |
call add3 | |
ld (ix+0), 3 | |
ld (ix+1), 8 | |
ld (ix+2), 10 | |
call calc_mul3 | |
call z, negate | |
call add3 | |
ld (ix+0), 4 | |
ld (ix+1), 6 | |
ld (ix+2), 11 | |
call calc_mul3 | |
call z, negate | |
call add3 | |
ld (ix+0), 5 | |
ld (ix+1), 7 | |
ld (ix+2), 9 | |
call calc_mul3 | |
call z, negate | |
call add3 | |
ld h, (ix+6) | |
ld l, (ix+7) | |
ld c, 6 | |
call div | |
ld h, a | |
ld d, l | |
ld l, (ix+8) | |
ld c, 6 | |
call div | |
; now answer is dl | |
ld h, d | |
ld c, 10 | |
ld ix, $2000 | |
ld b, 6 | |
modloop: | |
call div | |
ld (ix+0), a | |
inc ix | |
djnz modloop | |
ld ix, $2005 | |
ld b, 6 | |
printloop: | |
ld c, b | |
call div | |
ld a, (ix+0) | |
add a, '0' | |
call $8000 | |
ld b, c | |
dec ix | |
djnz printloop | |
halt | |
read_int: ; read int into (ix) | |
call $8003 ; getchar | |
sub '0' | |
ld b, a | |
sla b | |
sla b | |
sla b ; b = a * 8 | |
sla a ; a = a * 2 | |
add a, b | |
ld b, a | |
call $8003 ; getchar | |
sub '0' | |
add a, b | |
ld (ix+$0), a | |
call $8003 ; getchar | |
ret | |
mul: ; store result of de*c into ahl | |
ld hl, 0 | |
ld a, c | |
or a | |
ret z ; return zero if c is 0 | |
ld b, c | |
xor a ; a = 0 | |
mul_loop: | |
add hl, de | |
jp nc, non_carry | |
inc a | |
non_carry: | |
djnz mul_loop | |
ret | |
abs: ; a = abs(a) | |
bit 7, a | |
ret z | |
neg | |
ret | |
calc_mul3: | |
ld hl, $1000 | |
ld b, 0 | |
ld c, (ix+0) | |
add hl, bc | |
ld a, (hl) | |
call abs | |
ld d, $0 | |
ld e, a | |
ld hl, $1000 | |
ld b, 0 | |
ld c, (ix+1) | |
add hl, bc | |
ld a, (hl) | |
call abs | |
ld c, a | |
call mul | |
ld d, h | |
ld e, l | |
ld hl, $1000 | |
ld b, 0 | |
ld c, (ix+2) | |
add hl, bc | |
ld a, (hl) | |
call abs | |
ld c, a | |
call mul | |
ld (ix+3), a | |
ld (ix+4), h | |
ld (ix+5), l | |
; calculate sign of multiplication | |
ld b, 0 | |
ld hl, $1000 | |
ld d, 0 | |
ld e, (ix+0) | |
add hl, de | |
ld a, (hl) | |
bit 7, a | |
jp z, skip0 | |
ld a, b | |
xor $1 | |
ld b, a | |
skip0: | |
ld hl, $1000 | |
ld d, 0 | |
ld e, (ix+1) | |
add hl, de | |
ld a, (hl) | |
bit 7, a | |
jp z, skip1 | |
ld a, b | |
xor $1 | |
ld b, a | |
skip1: | |
ld hl, $1000 | |
ld d, 0 | |
ld e, (ix+2) | |
add hl, de | |
ld a, (hl) | |
bit 7, a | |
jp z, skip2 | |
ld a, b | |
xor $1 | |
ld b, a | |
skip2: | |
bit 0, b | |
ret | |
negate: ; negate (ix+3) - (ix+5) | |
ld a, (ix+3) | |
cpl | |
ld d, a | |
ld a, (ix+4) | |
cpl | |
ld b, a | |
ld a, (ix+5) | |
cpl | |
ld c, a | |
inc bc ; FIXME | |
ld (ix+3), d | |
ld (ix+4), b | |
ld (ix+5), c | |
ret | |
add3: ; add (ix+6) - (ix+8) by (ix+3) - (ix+5) | |
ld a, (ix+3) | |
ld b, (ix+4) | |
ld c, (ix+5) | |
ld d, (ix+6) | |
ld h, (ix+7) | |
ld l, (ix+8) | |
add hl, bc | |
adc a, d | |
ld (ix+6), a | |
ld (ix+7), h | |
ld (ix+8), l | |
ret | |
; divide hl by c | |
; http://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Division#16.2F8_division | |
div: | |
xor a | |
ld b, 16 ; loop counter | |
divloop: | |
add hl, hl | |
rla | |
jr c, $+5 | |
cp c | |
jr c, $+4 | |
sub c | |
inc l | |
djnz divloop | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment