[BITS 16]
[section text]

org 100h

jmp _main

global _printf2
global _main
global _putc

%include "io.asm"



_main:
	push bp
	mov bp,sp
	push 101
	push C0
	call _printf2
	add sp,word 4
	mov ax,word 0
	leave
	ret


%define _ptr -2
%define _fmt 4

_printf2:
	push bp
	mov bp,sp
	sub sp,word 2
	mov ax,word [bp+_fmt]
	mov [bp+_ptr],ax
L1:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word 0
	pop dx
	cmp dx,bx
	jne L2
	mov ax,word 0
	jmp L3
L2:	
	mov ax,word 1
L3:	
	cmp ax,word 0
	je L0
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	mov bx,word '%'
	pop dx
	cmp dx,bx
	je L5
	mov ax,word 0
	jmp L6
L5:	
	mov ax,word 1
L6:	
	cmp ax,word 0
	je L7
	push word 'Z'
	call _putc
	add sp,word 2
	jmp L4
L7:	
L4:	
	mov di,[bp+_ptr]
	mov al,[di]
	cpush al
	call _putc
	add sp,word 2
	inc word [bp+_ptr]
	jmp L1
L0:	
	mov ax,word 0
	leave
	ret

%undef _ptr
%undef _fmt


[section data]
C0: db 'HELLO, WORLD ID=%d!',10,13,0
