[BITS 16]
[section text]

org 100h

jmp _main

global _print_short
global _printf2
global _main
global _putc

%include "io.asm"



_main:
	push bp
	mov bp,sp
	push C0
	call _printf2
	add sp,word 2
	push C1
	call _printf2
	add sp,word 2
	push C2
	call _printf2
	add sp,word 2
	push C3
	call _printf2
	add sp,word 2
	push C4
	call _printf2
	add sp,word 2
	push C5
	call _printf2
	add sp,word 2
	push C6
	call _printf2
	add sp,word 2
	push C7
	call _printf2
	add sp,word 2
	push C8
	call _printf2
	add sp,word 2
	mov ax,word 0
	leave
	ret


%define _buffer -20
%define _byte_array -40
%define _index -42
%define _n -44
%define _ret -46
%define _number 4

_print_short:
	push bp
	mov bp,sp
	sub sp,word 46
	mov [bp+_ret],word 0
	lea bx,[bp+_byte_array]
	mov dx,word 0
	imul dx,word 2
	add bx,dx
	mov dx,word '0'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 1
	imul dx,word 2
	add bx,dx
	mov dx,word '1'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 2
	imul dx,word 2
	add bx,dx
	mov dx,word '2'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 3
	imul dx,word 2
	add bx,dx
	mov dx,word '3'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 4
	imul dx,word 2
	add bx,dx
	mov dx,word '4'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 5
	imul dx,word 2
	add bx,dx
	mov dx,word '5'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 6
	imul dx,word 2
	add bx,dx
	mov dx,word '6'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 7
	imul dx,word 2
	add bx,dx
	mov dx,word '7'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 8
	imul dx,word 2
	add bx,dx
	mov dx,word '8'
	mov [bx],dx
	lea bx,[bp+_byte_array]
	mov dx,word 9
	imul dx,word 2
	add bx,dx
	mov dx,word '9'
	mov [bx],dx
	mov dx,word [bp+_number]
	cmp dx,word 0
	je .L1
	mov ax,word 0
	jmp .L2
.L1:	
	mov ax,word 1
.L2:	
	cmp ax,word 0
	je .L3
	push word '0'
	call _putc
	add sp,word 2
	jmp .L0
.L3:	
.L0:	
	mov [bp+_index],word 0
.L5:	
	mov ax,word [bp+_number]
	cmp ax,word 0
	je .L4
	mov bx,word 10
	mov ax,word [bp+_number]
	xor dx,dx
	idiv bx
	mov [bp+_n],dx
	mov bx,word 10
	mov ax,word [bp+_number]
	xor dx,dx
	idiv bx
	mov [bp+_number],ax
	lea bx,[bp+_buffer]
	mov dx,word [bp+_index]
	imul dx,word 2
	add bx,dx
	push bx
	lea bx,[bp+_byte_array]
	mov dx,word [bp+_n]
	imul dx,word 2
	add bx,dx
	mov dx,word [bx]
	pop bx
	mov [bx],dx
	inc word [bp+_index]
	jmp .L5
.L4:	
	dec word [bp+_index]
.L7:	
	mov dx,word [bp+_index]
	cmp dx,word 0
	jge .L8
	mov ax,word 0
	jmp .L9
.L8:	
	mov ax,word 1
.L9:	
	cmp ax,word 0
	je .L6
	lea bx,[bp+_buffer]
	mov dx,word [bp+_index]
	imul dx,word 2
	add bx,dx
	push word [bx]
	call _putc
	add sp,word 2
	dec word [bp+_index]
	jmp .L7
.L6:	
	mov ax,word [bp+_ret]
	leave
	ret

%undef _buffer
%undef _byte_array
%undef _index
%undef _n
%undef _ret
%undef _number

%define _i -2
%define _ptr -4
%define _ptr0 -6
%define _ret -8
%define _fmt 4

_printf2:
	push bp
	mov bp,sp
	sub sp,word 8
	mov [bp+_ret],word 0
	mov ax,word [bp+_fmt]
	mov [bp+_ptr],ax
	mov [bp+_i],word 2
.L11:	
	mov di,[bp+_ptr]
	mov al,[di]
	cmp al,byte 0
	je .L10
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word '%'
	pop dx
	cmp dx,bx
	je .L13
	mov ax,word 0
	jmp .L14
.L13:	
	mov ax,word 1
.L14:	
	cmp ax,word 0
	je .L15
	inc word [bp+_ptr]
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word '%'
	pop dx
	cmp dx,bx
	je .L17
	mov ax,word 0
	jmp .L18
.L17:	
	mov ax,word 1
.L18:	
	cmp ax,word 0
	je .L19
	push word '%'
	call _putc
	add sp,word 2
	inc word [bp+_ptr]
	jmp .L16
.L19:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word 'x'
	pop dx
	cmp dx,bx
	je .L20
	mov ax,word 0
	jmp .L21
.L20:	
	mov ax,word 1
.L21:	
	cmp ax,word 0
	je .L22
	lea dx,[bp+_fmt]
	mov bx,word [bp+_i]
	add dx,bx
	mov [bp+_ptr0],dx
	mov di,[bp+_ptr0]
	mov dx,[di]
	cpush dx
	call _tohex
	mov bx,[bp+_i]
	mov dx,word 2
	add bx,dx
	mov [bp+_i],bx
	inc word [bp+_ptr]
	jmp .L16
.L22:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word 'd'
	pop dx
	cmp dx,bx
	je .L23
	mov ax,word 0
	jmp .L24
.L23:	
	mov ax,word 1
.L24:	
	cmp ax,word 0
	je .L25
	lea dx,[bp+_fmt]
	mov bx,word [bp+_i]
	add dx,bx
	mov [bp+_ptr0],dx
	mov di,[bp+_ptr0]
	mov dx,[di]
	cpush dx
	call _print_short
	mov bx,[bp+_i]
	mov dx,word 2
	add bx,dx
	mov [bp+_i],bx
	inc word [bp+_ptr]
	jmp .L16
.L25:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word 's'
	pop dx
	cmp dx,bx
	je .L26
	mov ax,word 0
	jmp .L27
.L26:	
	mov ax,word 1
.L27:	
	cmp ax,word 0
	je .L28
	lea dx,[bp+_fmt]
	mov bx,word [bp+_i]
	add dx,bx
	mov [bp+_ptr0],dx
.L30:	
	mov di,[bp+_ptr0]
	mov dx,[di]
	cpush dx
	pop ax
	cmp ax,word 0
	je .L29
	mov di,[bp+_ptr0]
	mov dx,[di]
	cpush dx
	call _putc
	inc word [bp+_ptr0]
	jmp .L30
.L29:	
	mov bx,[bp+_i]
	mov dx,word 2
	add bx,dx
	mov [bp+_i],bx
	inc word [bp+_ptr]
	jmp .L16
.L28:	
	inc word [bp+_ptr]
.L16:	
	jmp .L12
.L15:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	call _putc
	add sp,word 2
	inc word [bp+_ptr]
.L12:	
	jmp .L11
.L10:	
	mov ax,word 0
	leave
	ret

%undef _i
%undef _ptr
%undef _ptr0
%undef _ret
%undef _fmt


[section data]
C0: db 'Content-type: text/html',10,13,10,13,0
C1: db '<HTML>',10,13,0
C2: db '<HEAD>',10,13,0
C3: db '<BODY BGCOLOR="WHITE">',10,13,0
C4: db '<TITLE>cgi</TITLE>',10,13,0
C5: db '</HEAD>',10,13,0
C6: db '<H1>cgi</H1>',10,13,0
C7: db '</BODY>',10,13,0
C8: db '</HTML>',10,13,0
