Skip to main content
cleaned up assembly code
Source Link
user5434231
  • 1.6k
  • 11
  • 12

Equivalent assembly code: 762 705 683 670 670 characters (Counting newline as 2 chars, CR LF)

org $1000x100
ttcp equ $610x61    ; NTCPDRV interrupt

    xor ax,ax
    mov bx,10
    xor cx,cx
    mov si,$810x81     ; [ds:81]-[ds:FF] = command line args
    mov di,$800x80     ; [ds:80] = strlen(args)
    mov cl,[di]
    add di,cx
a    
@@: inc si
    mov cl,[si]     ; get character
    sub cl,'0'      ; convert char to int
    mul bx          ; ax *= 10
    add al,cl
    cmp si,di
    jb a@b
    ; now ax = port number

    mov bx,ax       ; source port (leaving this 0 doesn't work?)
    mov cx,ax       ; dest port
    mov ax,$10010x1001   ; open TCP socket for listening
    mov dx,-1       ; infinite timeout
    xor si,si       ; any dest IP
    xor di,di
    int ttcp
    ; ^ I think this call should block until a connection is established, but apparently it doesn't.

    push bx         ; bx = socket handle, save it for later

    mov ax,$12000x1200   ; read from socket
    mov di,$800x80     ; es:di = buffer (just reuse argument area to save space)
    mov cx,1        ; one byte
    mov dx,-1
    int ttcp         ; this will block until a client connects and sends one byte

    mov ax,$14000x1400   ; get TCP session status, bx=handle
    int ttcp
    ; now es:di points to a struct containing the source/dest IP addresses and ports
    ; the docs say it's two dwords for each IP address, then two bytes for "ip_prot" and "active" (whatever that means)
    ; ...but actually each IP address is followed by the port number (one word)

    xor ax,ax
    mov bx,10
    add di,6        ; [es:di+6] = client IP
    lea cx,[di+4]
b@@: add al,[es:di]  ; add all bytes together
    adc ah,0
    inc di
    cmp di,cx
    jb b@b
    ; now ax contains the IP address sum

    mov di,$840x84     ; recycle arguments area again
    mov cx,$800x80
    mov bx,10
c@@: dec di
    xor dx,dx
    div bx          ; dl = ax mod 10
    add dl,'0'      ; convert int to char
    mov [di],dl
    cmp di,cx
    ja c@b
    ; now [ds:80]-[ds:83] contains an ascii representation of the IP address sum

    push ds
    pop es
    mov ax,$130e0x130e   ; send data with newline, wait for ack
    pop bx          ; socket handle
    mov di,$800x80     ; es:di = data
    mov cx,4        ; sizeof data
    mov dx,-1
    int ttcp

    mov ax,$11000x1100   ; close TCP socket
    mov dx,1
    int ttcp

    mov ax,$4c000x4c00
    int $210x21

Equivalent assembly code: 762 705 683 670 670 characters (Counting newline as 2 chars, CR LF)

org $100
t equ $61
xor ax,ax
mov bx,10
xor cx,cx
mov si,$81
mov di,$80
mov cl,[di]
add di,cx
a:inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb a
mov bx,ax
mov cx,ax
mov ax,$1001
mov dx,-1
xor si,si
xor di,di
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
xor ax,ax
mov bx,10
add di,6
lea cx,[di+4]
b:add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb b
mov di,$84
mov cx,$80
mov bx,10
c:dec di
xor dx,dx
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja c
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

Equivalent assembly code:

org 0x100
tcp equ 0x61    ; NTCPDRV interrupt

    xor ax,ax
    mov bx,10
    xor cx,cx
    mov si,0x81     ; [ds:81]-[ds:FF] = command line args
    mov di,0x80     ; [ds:80] = strlen(args)
    mov cl,[di]
    add di,cx
    
@@: inc si
    mov cl,[si]     ; get character
    sub cl,'0'      ; convert char to int
    mul bx          ; ax *= 10
    add al,cl
    cmp si,di
    jb @b
    ; now ax = port number

    mov bx,ax       ; source port (leaving this 0 doesn't work?)
    mov cx,ax       ; dest port
    mov ax,0x1001   ; open TCP socket for listening
    mov dx,-1       ; infinite timeout
    xor si,si       ; any dest IP
    xor di,di
    int tcp
    ; ^ I think this call should block until a connection is established, but apparently it doesn't.

    push bx         ; bx = socket handle, save it for later

    mov ax,0x1200   ; read from socket
    mov di,0x80     ; es:di = buffer (just reuse argument area to save space)
    mov cx,1        ; one byte
    mov dx,-1
    int tcp         ; this will block until a client connects and sends one byte

    mov ax,0x1400   ; get TCP session status, bx=handle
    int tcp
    ; now es:di points to a struct containing the source/dest IP addresses and ports
    ; the docs say it's two dwords for each IP address, then two bytes for "ip_prot" and "active" (whatever that means)
    ; ...but actually each IP address is followed by the port number (one word)

    xor ax,ax
    mov bx,10
    add di,6        ; [es:di+6] = client IP
    lea cx,[di+4]
@@: add al,[es:di]  ; add all bytes together
    adc ah,0
    inc di
    cmp di,cx
    jb @b
    ; now ax contains the IP address sum

    mov di,0x84     ; recycle arguments area again
    mov cx,0x80
    mov bx,10
@@: dec di
    xor dx,dx
    div bx          ; dl = ax mod 10
    add dl,'0'      ; convert int to char
    mov [di],dl
    cmp di,cx
    ja @b
    ; now [ds:80]-[ds:83] contains an ascii representation of the IP address sum

    push ds
    pop es
    mov ax,0x130e   ; send data with newline, wait for ack
    pop bx          ; socket handle
    mov di,0x80     ; es:di = data
    mov cx,4        ; sizeof data
    mov dx,-1
    int tcp

    mov ax,0x1100   ; close TCP socket
    mov dx,1
    int tcp

    mov ax,0x4c00
    int 0x21
changed to machine code
Source Link
user5434231
  • 1.6k
  • 11
  • 12

8086 assemblymachine code (16-bit DOS), 762 705 683163 156 148 148 670 characters142 bytes

00000000  31 c0 bb 0a 00 31 c9 be  81 00 bf 80 00 8a 0d 01  |1....1..........|
00000010  cf 46 8a 0c 80 e9 30 f7  e3 00 c8 39 fe 72 f2 89  |.F....0....9.r..|
00000020  c3 89 c1 b8 01 10 ba ff  ff 31 f6 31 ff cd 61 53  |.........1.1..aS|
00000030  b8 00 12 bf 80 00 b9 01  00 ba ff ff cd 61 b8 00  |.............a..|
00000040  14 cd 61 31 c0 bb 0a 00  83 c7 06 8d 4d 04 26 02  |..a1........M.&.|
00000050  05 80 d4 00 47 39 cf 72  f5 bf 84 00 b9 80 00 bb  |....G9.r........|
00000060  0a 00 4f 31 d2 f7 f3 80  c2 30 88 15 39 cf 77 f2  |..O1.....0..9.w.|
00000070  1e 07 b8 0e 13 5b bf 80  00 b9 04 00 ba ff ff cd  |.....[..........|
00000080  61 b8 00 11 ba 01 00 cd  61 b8 00 4c cd 21        |a.......a..L.!|
0000008e

Equivalent assembly code: 762 705 683 670 670 characters (Counting newline as 2 chars, CR LF. Assembled size: 163 156 148 148 bytes)

org $100
t equ $61
movxor ax,0ax
mov bx,10
movxor cx,0cx
mov si,$81
mov di,$80
mov cl,[di]
add di,cx
a:
 inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb a
mov bx,ax
mov axcx,$1001ax
mov cxax,bx$1001
mov dx,-1
movxor si,0si
movxor di,0di
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
movxor ax,0ax
mov bx,10
add di,6
lea cx,[di+4]
b:
 add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb b
mov di,$84
mov cx,$80
mov bx,10
c:
 dec di
movxor dx,0dx
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja c
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

8086 assembly (16-bit DOS), 762 705 683 670 characters

(Counting newline as 2 chars, CR LF. Assembled size: 163 156 148 148 bytes)

org $100
t equ $61
mov ax,0
mov bx,10
mov cx,0
mov si,$81
mov di,$80
mov cl,[di]
add di,cx
a:
 inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb a
mov bx,ax
mov ax,$1001
mov cx,bx
mov dx,-1
mov si,0
mov di,0
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
mov ax,0
mov bx,10
add di,6
lea cx,[di+4]
b:
 add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb b
mov di,$84
mov cx,$80
mov bx,10
c:
 dec di
mov dx,0
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja c
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

8086 machine code (16-bit DOS), 163 156 148 148 142 bytes

00000000  31 c0 bb 0a 00 31 c9 be  81 00 bf 80 00 8a 0d 01  |1....1..........|
00000010  cf 46 8a 0c 80 e9 30 f7  e3 00 c8 39 fe 72 f2 89  |.F....0....9.r..|
00000020  c3 89 c1 b8 01 10 ba ff  ff 31 f6 31 ff cd 61 53  |.........1.1..aS|
00000030  b8 00 12 bf 80 00 b9 01  00 ba ff ff cd 61 b8 00  |.............a..|
00000040  14 cd 61 31 c0 bb 0a 00  83 c7 06 8d 4d 04 26 02  |..a1........M.&.|
00000050  05 80 d4 00 47 39 cf 72  f5 bf 84 00 b9 80 00 bb  |....G9.r........|
00000060  0a 00 4f 31 d2 f7 f3 80  c2 30 88 15 39 cf 77 f2  |..O1.....0..9.w.|
00000070  1e 07 b8 0e 13 5b bf 80  00 b9 04 00 ba ff ff cd  |.....[..........|
00000080  61 b8 00 11 ba 01 00 cd  61 b8 00 4c cd 21        |a.......a..L.!|
0000008e

Equivalent assembly code: 762 705 683 670 670 characters (Counting newline as 2 chars, CR LF)

org $100
t equ $61
xor ax,ax
mov bx,10
xor cx,cx
mov si,$81
mov di,$80
mov cl,[di]
add di,cx
a:inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb a
mov bx,ax
mov cx,ax
mov ax,$1001
mov dx,-1
xor si,si
xor di,di
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
xor ax,ax
mov bx,10
add di,6
lea cx,[di+4]
b:add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb b
mov di,$84
mov cx,$80
mov bx,10
c:dec di
xor dx,dx
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja c
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21
improved to 670 chars
Source Link
user5434231
  • 1.6k
  • 11
  • 12

8086 16-bit assembly (DOS16-bit DOS), 762 705 683 683670 characters

(Counting newline as 2 chars, CR LF. Assembled size: 163 156 148 148 bytes compiled)

org $100
t equ $61
mov ax,0
mov bx,10
mov cx,0
mov si,$80$81
mov di,si$80
mov cl,[si][di]
add di,cx
inc si
@@a:
inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb @ba
mov bx,ax
mov ax,$1001
mov cx,bx
mov dx,-1
mov si,0
mov di,0
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
mov ax,0
mov bx,10
add di,6
lea cx,[di+4]
@@b:
add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb @bb
mov di,$84
mov cx,$80
mov bx,10
@@c:
dec di
mov dx,0
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja @bc
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

This assumes ntcpdrv is loaded at INT 0x61 (and any suitable packet driver at 0x60). Compile with fasm tcpserv.asm.

It has some issues though:

  • It doesn't check if the argument is a valid port number, or if it's even a number at all.
  • The client must send at least one byte, since I can't seem to find any other way to tell if a client has connected.
  • It only works once, and hangs on a second attempt. Works again after a reboot.
  • The returned value is left-padded with zeroes.
  • This is my very first code golf entry, and also my very first 8086 asm program. I'm sure there are ways to improve this further.

8086 16-bit assembly (DOS), 762 705 683 characters

(163 156 148 bytes compiled)

org $100
t equ $61
mov ax,0
mov bx,10
mov cx,0
mov si,$80
mov di,si
mov cl,[si]
add di,cx
inc si
@@:
inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb @b
mov bx,ax
mov ax,$1001
mov cx,bx
mov dx,-1
mov si,0
mov di,0
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
mov ax,0
mov bx,10
add di,6
lea cx,[di+4]
@@:
add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb @b
mov di,$84
mov cx,$80
mov bx,10
@@:
dec di
mov dx,0
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja @b
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

This assumes ntcpdrv is loaded at INT 0x61 (and any suitable packet driver at 0x60). Compile with fasm tcpserv.asm.

It has some issues though:

  • It doesn't check if the argument is a valid port number, or if it's a number at all.
  • The client must send at least one byte, since I can't seem to find any other way to tell if a client has connected.
  • It only works once, and hangs on a second attempt. Works again after a reboot.
  • The returned value is left-padded with zeroes.
  • This is my very first code golf entry, and also my very first 8086 asm program. I'm sure there are ways to improve this further.

8086 assembly (16-bit DOS), 762 705 683 670 characters

(Counting newline as 2 chars, CR LF. Assembled size: 163 156 148 148 bytes)

org $100
t equ $61
mov ax,0
mov bx,10
mov cx,0
mov si,$81
mov di,$80
mov cl,[di]
add di,cx
a:
inc si
mov cl,[si]
sub cl,'0'
mul bx
add al,cl
cmp si,di
jb a
mov bx,ax
mov ax,$1001
mov cx,bx
mov dx,-1
mov si,0
mov di,0
int t
push bx
mov ax,$1200
mov di,$80
mov cx,1
mov dx,-1
int t
mov ax,$1400
int t
mov ax,0
mov bx,10
add di,6
lea cx,[di+4]
b:
add al,[es:di]
adc ah,0
inc di
cmp di,cx
jb b
mov di,$84
mov cx,$80
mov bx,10
c:
dec di
mov dx,0
div bx
add dl,'0'
mov [di],dl
cmp di,cx
ja c
push ds
pop es
mov ax,$130e
pop bx
mov di,$80
mov cx,4
mov dx,-1
int t
mov ax,$1100
mov dx,1
int t
mov ax,$4c00
int $21

This assumes ntcpdrv is loaded at INT 0x61 (and any suitable packet driver at 0x60). Compile with fasm tcpserv.asm.

It has some issues though:

  • It doesn't check if the argument is a valid port number, or if it's even a number at all.
  • The client must send at least one byte, since I can't seem to find any other way to tell if a client has connected.
  • It only works once, and hangs on a second attempt. Works again after a reboot.
  • The returned value is left-padded with zeroes.
  • This is my very first code golf entry, and also my very first 8086 asm program. I'm sure there are ways to improve this further.
added link to ntcpdrv
Source Link
user5434231
  • 1.6k
  • 11
  • 12
Loading
Rollback to Revision 2 - revert
Source Link
user5434231
  • 1.6k
  • 11
  • 12
Loading
removed one redundant instruction
Source Link
user5434231
  • 1.6k
  • 11
  • 12
Loading
removed unnecessary variable
Source Link
user5434231
  • 1.6k
  • 11
  • 12
Loading
Source Link
user5434231
  • 1.6k
  • 11
  • 12
Loading