include macro.inc

;ISDIGIT - determines whether @@char belongs to digits
isdigit proc pascal near
        arg @@char:BYTE
        
        mov al,@@char
        cmp al,'0'
        jb @@fail       
        cmp al,'9'
        ja @@fail               
        mov ax,00001h
        jmp @@exit
@@fail:
        xor ax,ax
@@exit:        
        ret
isdigit endp
;-----------------------------------------------------------------------------
iswhite proc pascal near
        arg @@char:BYTE
        
        mov al,@@char
        cmp al,SPACE
        je @@exit
        cmp al,TAB         
        je @@exit
@@fail:
        xor ax,ax
        ret
@@exit:    
        mov ax,00001h
        ret   
iswhite endp
;-----------------------------------------------------------------------------
;ATOI - converts an ASCIIZ - string @@str to word-integer
atoi proc pascal near
     arg @@str:WORD
     uses si,cx,dx,di
     
     cld
     mov ah,000h
     mov bx,DIG_BASE
     mov di,00001h 
     mov si,@@str
     lodsb
     cmp al,MINUS
     jne @@not_minus
     xor di,di
@@not_minus:
     sub si,di			;put back symbol if [di]!='-'
     xor cx,cx
@@next_sym:
     lodsb
     or al,al
     jz @@exit
     push ax
     push ax
     call isdigit
     or ax,ax
     pop ax
     jz @@inv_num
     sub al,'0'
     push ax
     mov ax,cx
     mul bx
     mov cx,ax
     pop ax
     jc @@too_long
     add cx,ax
     jc @@too_long
     jmp @@next_sym
@@exit:     
     mov ax,OK
     mov bx,cx
     or di,di
     jnz @@return
     neg bx
     jmp @@return
@@too_long:
     mov ax,NUMBER_TOO_LONG           
     jmp @@return
@@inv_num:    
     mov ax,INVALID_NUMBER
     jmp @@return    
@@return:
     ret
atoi endp
;-----------------------------------------------------------------------------
;ITOA - converts integer to ASCII
;RETURNS - nothing
itoa proc pascal near
     arg @@number:WORD,@@str:WORD
     uses ax,bx,cx,dx,di,si

     cld
     mov di,@@str
     mov ax,@@number
     or ax,ax
     jns @@begin
     mov [di],MINUS
     inc di
     neg ax
@@begin:   
     mov bx,DIG_BASE          
     xor cx,cx
@@next_digit:     
     xor dx,dx
     div bx
     mov si,ax
     mov al,dl
     add al,'0'
     push ax            ;need for reverse of symbol's direction
     inc cx             ;count's pushed symbols
     mov ax,si
     or ax,ax
     jnz @@next_digit
@@next_sym:     
     pop ax
     mov [di],al
     inc di
     loop @@next_sym
     mov byte ptr [di],NULL
     ret
itoa endp
;-----------------------------------------------------------------------------
charout proc pascal near
        arg @@char:BYTE
        uses ax
        
        mov ah,00Eh
        mov al,@@char
        int 010h
        ret
charout endp
;-----------------------------------------------------------------------------
newline proc pascal near
        push word ptr CR
        call charout
        push word ptr LF
        call charout
        ret
newline endp
;-----------------------------------------------------------------------------
strout proc pascal near
       arg @@str:WORD
       uses ax,bx,cx,dx

       push @@str
       call strlen
       mov cx,ax
       mov ah,40h
       mov bx,STDERR
       mov dx,@@str
       int 021h
       ret
strout endp
;----------------------------------------------------------------------------- 
;ISALPHA - determines whether @@char is alpha
isalpha proc pascal near
        arg @@char:BYTE
        
        mov al,@@char
        or al,020h
        cmp al,'a'
        jb @@fail       
        cmp al,'z'
        ja @@fail               
        mov ax,00001h
        jmp @@exit
@@fail: 
        xor ax,ax
@@exit:        
        ret
isalpha endp
;-----------------------------------------------------------------------------
;STRCHR - performs searching in memory area with pointer
;@@find_where for byte @@find_what
strchr proc pascal near
       arg @@find_where:WORD,@@find_what:BYTE,@@length:WORD
       uses di,cx,es
       
       cld
       push ds
       pop es
       mov cx,@@length
       mov di,@@find_where
       mov al,@@find_what
       repne scasb    
       jne @@fail
       lea ax,[di-001h]
       jmp @@exit
@@fail:
       xor ax,ax
@@exit:
       ret
strchr endp
;-----------------------------------------------------------------------------
strlen proc pascal near
       arg @@str:WORD
       uses cx,di,es
       
       cld
       push ds
       pop es
       mov di,@@str
       mov cx,0FFFFh
       mov al,NULL
       repne scasb
       mov ax,cx
       not ax
       dec ax
       ret
strlen endp
;-----------------------------------------------------------------------------
strlwr proc pascal near
       arg @@str:WORD
       uses ax,si,di,es
       
       mov si,@@str
       mov di,si
       push cs
       pop es
       cld
@@next_sym:       
       lodsb
       or al,al
       jz @@exit
       cmp al,'A'
       jb @@skip
       cmp al,'Z'
       ja @@skip
       or al,020h
@@skip:
       stosb
       jmp @@next_sym
@@exit:       
       ret 
strlwr endp
;-----------------------------------------------------------------------------
strcmp proc pascal near
       arg @@str1:WORD,@@str2:WORD
       uses cx,si,di,es
       
       mov si,@@str1
       push si
       call strlen
       mov cx,ax
       mov di,@@str2
       push di
       call strlen
       cmp ax,cx
       jne @@fail
       push ds
       pop es
       cld
       repe cmpsb
       jne @@fail
       mov ax,00001h
       jmp @@exit
@@fail:
       xor ax,ax
@@exit:        
       ret 
strcmp endp
;-----------------------------------------------------------------------------
strcpy proc pascal near
       arg @@source:WORD,@@dest:WORD
       uses ax,cx,si,di,es
       
       push ds
       pop es
       mov si,@@source
       push si
       call strlen
       mov cx,ax
       inc cx
       cld
       mov di,@@dest
       rep movsb
       ret
strcpy endp
;-----------------------------------------------------------------------------
get_str proc pascal near
        arg @@str:WORD
        uses ax,bx,dx
        
        mov ah,00Ah
        mov dx,@@str
        int 021h
        mov bx,dx
        mov al,[bx+001h]
        cbw
        add bx,ax
        mov [bx+002h],byte ptr NULL
        ret
get_str endp
;-----------------------------------------------------------------------------
clrscr proc pascal near
       uses ax,bx,cx,dx,ds
      
       mov ax,00600h
       mov bh,007h
       xor cx,cx
       mov ds,cx
       mov dh,ds:[00484h]
       inc dh
       mov dl,ds:[0044Ah]
       int 010h
       mov ah,00Fh
       int 010h
       mov ah,002h
       xor dx,dx
       int 010h
       ret
clrscr endp
;-----------------------------------------------------------------------------
exit_program proc near pascal
             arg @@code:BYTE
            
             mov ah,04Ch
             mov al,@@code
             int 021h
             ret
exit_program endp
;-----------------------------------------------------------------------------