Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 835 → Rev 836

/programs/develop/libraries/console/examples/proc32.inc
0,0 → 1,268
 
; Macroinstructions for defining and calling procedures
 
macro stdcall proc,[arg] ; directly call STDCALL procedure
{ common
if ~ arg eq
reverse
pushd arg
common
end if
call proc }
 
macro invoke proc,[arg] ; indirectly call STDCALL procedure
{ common
if ~ arg eq
reverse
pushd arg
common
end if
call [proc] }
 
macro ccall proc,[arg] ; directly call CDECL procedure
{ common
size@ccall = 0
if ~ arg eq
reverse
pushd arg
size@ccall = size@ccall+4
common
end if
call proc
if size@ccall
add esp,size@ccall
end if }
 
macro cinvoke proc,[arg] ; indirectly call CDECL procedure
{ common
size@ccall = 0
if ~ arg eq
reverse
pushd arg
size@ccall = size@ccall+4
common
end if
call [proc]
if size@ccall
add esp,size@ccall
end if }
 
macro proc [args] ; define procedure
{ common
match name params, args>
\{ define@proc name,<params \} }
 
prologue@proc equ prologuedef
 
macro prologuedef procname,flag,parmbytes,localbytes,reglist
{ if parmbytes | localbytes
push ebp
mov ebp,esp
if localbytes
sub esp,localbytes
end if
end if
irps reg, reglist \{ push reg \} }
 
epilogue@proc equ epiloguedef
 
macro epiloguedef procname,flag,parmbytes,localbytes,reglist
{ irps reg, reglist \{ reverse pop reg \}
if parmbytes | localbytes
leave
end if
if flag and 10000b
retn
else
retn parmbytes
end if }
 
macro define@proc name,statement
{ local params,flag,regs,parmbytes,localbytes,current
if used name
name:
match =stdcall args, statement \{ params equ args
flag = 11b \}
match =stdcall, statement \{ params equ
flag = 11b \}
match =c args, statement \{ params equ args
flag = 10001b \}
match =c, statement \{ params equ
flag = 10001b \}
match =params, params \{ params equ statement
flag = 0 \}
virtual at ebp+8
match =uses reglist=,args, params \{ regs equ reglist
params equ args \}
match =regs =uses reglist, regs params \{ regs equ reglist
params equ \}
match =regs, regs \{ regs equ \}
match =,args, params \{ defargs@proc args \}
match =args@proc args, args@proc params \{ defargs@proc args \}
parmbytes = $ - (ebp+8)
end virtual
name # % = parmbytes/4
all@vars equ
current = 0
match prologue:reglist, prologue@proc:<regs> \{ prologue name,flag,parmbytes,localbytes,reglist \}
macro locals
\{ virtual at ebp-localbytes+current
macro label . \\{ deflocal@proc .,:, \\}
struc db [val] \\{ \common deflocal@proc .,db,val \\}
struc dw [val] \\{ \common deflocal@proc .,dw,val \\}
struc dp [val] \\{ \common deflocal@proc .,dp,val \\}
struc dd [val] \\{ \common deflocal@proc .,dd,val \\}
struc dt [val] \\{ \common deflocal@proc .,dt,val \\}
struc dq [val] \\{ \common deflocal@proc .,dq,val \\}
struc rb cnt \\{ deflocal@proc .,rb cnt, \\}
struc rw cnt \\{ deflocal@proc .,rw cnt, \\}
struc rp cnt \\{ deflocal@proc .,rp cnt, \\}
struc rd cnt \\{ deflocal@proc .,rd cnt, \\}
struc rt cnt \\{ deflocal@proc .,rt cnt, \\}
struc rq cnt \\{ deflocal@proc .,rq cnt, \\} \}
macro endl
\{ purge label
restruc db,dw,dp,dd,dt,dq
restruc rb,rw,rp,rd,rt,rq
restruc byte,word,dword,pword,tword,qword
current = $-(ebp-localbytes)
end virtual \}
macro ret operand
\{ match any, operand \\{ retn operand \\}
match , operand \\{ match epilogue:reglist, epilogue@proc:<regs>
\\\{ epilogue name,flag,parmbytes,localbytes,reglist \\\} \\} \}
macro finish@proc \{ localbytes = (((current-1) shr 2)+1) shl 2
end if \} }
 
macro defargs@proc [arg]
{ common
if ~ arg eq
forward
local ..arg,current@arg
match argname:type, arg
\{ current@arg equ argname
label ..arg type
argname equ ..arg
if dqword eq type
dd ?,?,?,?
else if tbyte eq type
dd ?,?,?
else if qword eq type | pword eq type
dd ?,?
else
dd ?
end if \}
match =current@arg,current@arg
\{ current@arg equ arg
arg equ ..arg
..arg dd ? \}
common
args@proc equ current@arg
forward
restore current@arg
common
end if }
 
macro deflocal@proc name,def,[val]
{ common
match vars, all@vars \{ all@vars equ all@vars, \}
all@vars equ all@vars name
forward
local ..var,..tmp
..var def val
match =?, val \{ ..tmp equ \}
match any =dup (=?), val \{ ..tmp equ \}
match tmp : value, ..tmp : val
\{ tmp: end virtual
initlocal@proc ..var,def value
virtual at tmp\}
common
match first rest, ..var, \{ name equ first \} }
 
macro initlocal@proc name,def
{ virtual at name
def
size@initlocal = $ - name
end virtual
position@initlocal = 0
while size@initlocal > position@initlocal
virtual at name
def
if size@initlocal - position@initlocal < 2
current@initlocal = 1
load byte@initlocal byte from name+position@initlocal
else if size@initlocal - position@initlocal < 4
current@initlocal = 2
load word@initlocal word from name+position@initlocal
else
current@initlocal = 4
load dword@initlocal dword from name+position@initlocal
end if
end virtual
if current@initlocal = 1
mov byte [name+position@initlocal],byte@initlocal
else if current@initlocal = 2
mov word [name+position@initlocal],word@initlocal
else
mov dword [name+position@initlocal],dword@initlocal
end if
position@initlocal = position@initlocal + current@initlocal
end while }
 
macro endp
{ purge ret,locals,endl
finish@proc
purge finish@proc
restore regs@proc
match all,args@proc \{ restore all \}
restore args@proc
match all,all@vars \{ restore all \} }
 
macro local [var]
{ common
locals
forward done@local equ
match varname[count]:vartype, var
\{ match =BYTE, vartype \\{ varname rb count
restore done@local \\}
match =WORD, vartype \\{ varname rw count
restore done@local \\}
match =DWORD, vartype \\{ varname rd count
restore done@local \\}
match =PWORD, vartype \\{ varname rp count
restore done@local \\}
match =QWORD, vartype \\{ varname rq count
restore done@local \\}
match =TBYTE, vartype \\{ varname rt count
restore done@local \\}
match =DQWORD, vartype \\{ label varname dqword
rq count+count
restore done@local \\}
match , done@local \\{ virtual
varname vartype
end virtual
rb count*sizeof.\#vartype
restore done@local \\} \}
match :varname:vartype, done@local:var
\{ match =BYTE, vartype \\{ varname db ?
restore done@local \\}
match =WORD, vartype \\{ varname dw ?
restore done@local \\}
match =DWORD, vartype \\{ varname dd ?
restore done@local \\}
match =PWORD, vartype \\{ varname dp ?
restore done@local \\}
match =QWORD, vartype \\{ varname dq ?
restore done@local \\}
match =TBYTE, vartype \\{ varname dt ?
restore done@local \\}
match =DQWORD, vartype \\{ label varname dqword
dq ?,?
restore done@local \\}
match , done@local \\{ varname vartype
restore done@local \\} \}
match ,done@local
\{ var
restore done@local \}
common
endl }
/programs/develop/libraries/console/examples/test_getch.asm
0,0 → 1,166
 
include 'proc32.inc'
 
DLL_ENTRY equ 1
DLL_EXIT equ -1
REQ_DLL_VER equ 3
 
use32
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem
dd mem
dd 0
dd 0
 
start:
stdcall load_dll_and_import, dllname, imports
test eax, eax
jz exit
 
; check version
cmp word [dll_ver], REQ_DLL_VER
jb exit
cmp word [dll_ver+2], REQ_DLL_VER
ja exit
push DLL_ENTRY
call [dll_start]
 
; yes! Now do some work (getch() demo in this case).
 
push caption
push -1
push -1
push -1
push -1
call [con_init]
 
; C-equivalent of the following code:
; con_printf(start_string);
; int c;
; while ((c=con_getch())!=27) // Esc=exit
; {
; if (c)
; con_printf("normal character with code %d=0x%02X\n",c,c);
; else
; {
; c=con_getch();
; con_printf("extended character with code %d=0x%02X\n",c,c);
; }
; }
push start_string
call [con_printf]
pop ecx
mainloop:
call [con_getch]
cmp al, 27
jz done
test eax, eax
jz extended
push eax
push eax
push string_normal
@@:
call [con_printf]
add esp, 12
jmp mainloop
extended:
call [con_getch]
push eax
push eax
push string_extended
jmp @b
done:
push 1
call [con_exit]
exit:
or eax, -1
int 0x40
 
proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
pushad
; load DLL
push 68
pop eax
push 19
pop ebx
mov ecx, [_dllname]
int 0x40
test eax, eax
jz import_fail
 
; initialize import
mov edi, eax
mov esi, [_imports]
import_loop:
lodsd
test eax, eax
jz import_done
mov edx, edi
import_find:
mov ebx, [edx]
test ebx, ebx
jz import_not_found
push eax
@@:
mov cl, [eax]
cmp cl, [ebx]
jnz import_find_next
test cl, cl
jz import_found
inc eax
inc ebx
jmp @b
import_find_next:
pop eax
add edx, 8
jmp import_find
import_found:
pop eax
mov eax, [edx+4]
mov [esi-4], eax
jmp import_loop
import_not_found:
import_fail:
popad
xor eax, eax
ret
import_done:
popad
xor eax, eax
inc eax
ret
endp
 
align 4
 
imports:
dll_start dd szStart
dll_ver dd szVersion
con_init dd szcon_init
con_printf dd szcon_printf
con_exit dd szcon_exit
con_getch dd szcon_getch
dd 0
 
szStart db 'START',0
szVersion db 'version',0
szcon_init db 'con_init',0
szcon_printf db 'con_printf',0
szcon_exit db 'con_exit',0
szcon_getch db 'con_getch',0
 
dllname db '/sys/lib/console.obj',0
 
caption db 'Console test - getch()',0
start_string db 'Press any key to see its code, or Esc to exit',10,0
string_normal db 'normal character with code %d=0x%02X',10,0
string_extended db 'extended character with code %d=0x%02X',10,0
 
i_end:
 
align 4
rb 2048 ; stack
mem:
/programs/develop/libraries/console/examples/test_gets.asm
0,0 → 1,154
 
include 'proc32.inc'
 
DLL_ENTRY equ 1
DLL_EXIT equ -1
REQ_DLL_VER equ 3
 
use32
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem
dd mem
dd 0
dd 0
 
start:
stdcall load_dll_and_import, dllname, imports
test eax, eax
jz exit
 
; check version
cmp word [dll_ver], REQ_DLL_VER
jb exit
cmp word [dll_ver+2], REQ_DLL_VER
ja exit
push DLL_ENTRY
call [dll_start]
 
; yes! Now do some work (gets() demo in this case).
 
push caption
push -1
push -1
push -1
push -1
call [con_init]
 
; C-equivalent of the following code:
; for (;;)
; {
; con_write_asciiz("Enter string (empty for exit): ");
; con_gets(s,256);
; if (s[0] == '\n') break;
; con_write_asciiz("You entered: ");
; con_write_asciiz(s);
; }
mainloop:
push str1
call [con_write_asciiz]
push 256
push s
call [con_gets]
cmp [s], 10
jz done
push str2
call [con_write_asciiz]
push s
call [con_write_asciiz]
jmp mainloop
done:
push 1
call [con_exit]
exit:
or eax, -1
int 0x40
 
proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
pushad
; load DLL
push 68
pop eax
push 19
pop ebx
mov ecx, [_dllname]
int 0x40
test eax, eax
jz import_fail
 
; initialize import
mov edi, eax
mov esi, [_imports]
import_loop:
lodsd
test eax, eax
jz import_done
mov edx, edi
import_find:
mov ebx, [edx]
test ebx, ebx
jz import_not_found
push eax
@@:
mov cl, [eax]
cmp cl, [ebx]
jnz import_find_next
test cl, cl
jz import_found
inc eax
inc ebx
jmp @b
import_find_next:
pop eax
add edx, 8
jmp import_find
import_found:
pop eax
mov eax, [edx+4]
mov [esi-4], eax
jmp import_loop
import_not_found:
import_fail:
popad
xor eax, eax
ret
import_done:
popad
xor eax, eax
inc eax
ret
endp
 
align 4
 
imports:
dll_start dd szStart
dll_ver dd szVersion
con_init dd szcon_init
con_write_asciiz dd szcon_write_asciiz
con_exit dd szcon_exit
con_gets dd szcon_gets
dd 0
 
szStart db 'START',0
szVersion db 'version',0
szcon_init db 'con_init',0
szcon_write_asciiz db 'con_write_asciiz',0
szcon_exit db 'con_exit',0
szcon_gets db 'con_gets',0
 
dllname db '/sys/lib/console.obj',0
 
caption db 'Console test - gets()',0
str1 db 'Enter string (empty for exit): ',0
str2 db 'You entered: ',0
 
i_end:
 
s rb 256
 
align 4
rb 2048 ; stack
mem:
/programs/develop/libraries/console/examples/testcon.asm
0,0 → 1,106
use32
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem
dd mem
dd 0
dd 0
 
REQ_DLL_VER = 2
DLL_ENTRY = 1
 
start:
; First 3 steps are intended to load/init console DLL
; and are identical for all console programs
 
; load DLL
mov eax, 68
mov ebx, 19
mov ecx, dll_name
int 0x40
test eax, eax
jz exit
 
; initialize import
mov edx, eax
mov esi, myimport
import_loop:
lodsd
test eax, eax
jz import_done
push edx
import_find:
mov ebx, [edx]
test ebx, ebx
jz exit;import_not_found
push eax
@@:
mov cl, [eax]
cmp cl, [ebx]
jnz import_find_next
test cl, cl
jz import_found
inc eax
inc ebx
jmp @b
import_find_next:
pop eax
add edx, 8
jmp import_find
import_found:
pop eax
mov eax, [edx+4]
mov [esi-4], eax
pop edx
jmp import_loop
import_done:
 
; check version
cmp word [dll_ver], REQ_DLL_VER
jb exit
cmp word [dll_ver+2], REQ_DLL_VER
ja exit
push DLL_ENTRY
call [dll_start]
 
; yes! Now do some work (say helloworld in this case).
push caption
push -1
push -1
push -1
push -1
call [con_init]
push aHelloWorld
call [con_write_asciiz]
push 0
call [con_exit]
exit:
or eax, -1
int 0x40
 
dll_name db '/sys/lib/console.obj',0
caption db 'Console test',0
aHelloWorld db 'Hello, World!',10,0
 
align 4
myimport:
dll_start dd aStart
dll_ver dd aVersion
con_init dd aConInit
con_write_asciiz dd aConWriteAsciiz
con_exit dd aConExit
dd 0
 
aStart db 'START',0
aVersion db 'version',0
aConInit db 'con_init',0
aConWriteAsciiz db 'con_write_asciiz',0
aConExit db 'con_exit',0
 
i_end:
 
align 4
rb 2048 ; stack
mem:
/programs/develop/libraries/console/examples/testcon2.asm
0,0 → 1,161
 
include 'proc32.inc'
 
DLL_ENTRY equ 1
DLL_EXIT equ -1
REQ_DLL_VER equ 2
 
use32
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem
dd mem
dd 0
dd 0
 
start:
stdcall load_dll_and_import, dllname, imports
test eax, eax
jz exit
 
; check version
cmp word [dll_ver], REQ_DLL_VER
jb exit
cmp word [dll_ver+2], REQ_DLL_VER
ja exit
push DLL_ENTRY
call [dll_start]
 
; yes! Now do some work (show color strings in this case).
 
push caption
push -1
push -1
push -1
push -1
call [con_init]
; C-equivalent of the following code:
; for (ebx=0;ebx<0x100;ebx++)
; {
; con_printf(t1,ebx);
; eax = con_set_flags(ebx);
; con_write_asciiz(text);
; con_set_flags(eax);
; }
; N.B. For efficiency result of first con_set_flags is not saved
; in register, but is pushed beforehand to the stack
; for second con_set_flags.
; Note that such code cannot be generated by stdcall macros.
xor ebx, ebx
@@:
push ebx
push t1
call [con_printf]
add esp, 8
push ebx
call [con_set_flags]
push eax
push text
call [con_write_asciiz]
call [con_set_flags]
inc bl
jnz @b
push text2
call [con_write_asciiz]
push 0
call [con_exit]
exit:
or eax, -1
int 0x40
 
proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
pushad
; load DLL
push 68
pop eax
push 19
pop ebx
mov ecx, [_dllname]
int 0x40
test eax, eax
jz import_fail
 
; initialize import
mov edi, eax
mov esi, [_imports]
import_loop:
lodsd
test eax, eax
jz import_done
mov edx, edi
import_find:
mov ebx, [edx]
test ebx, ebx
jz import_not_found
push eax
@@:
mov cl, [eax]
cmp cl, [ebx]
jnz import_find_next
test cl, cl
jz import_found
inc eax
inc ebx
jmp @b
import_find_next:
pop eax
add edx, 8
jmp import_find
import_found:
pop eax
mov eax, [edx+4]
mov [esi-4], eax
jmp import_loop
import_not_found:
import_fail:
popad
xor eax, eax
ret
import_done:
popad
xor eax, eax
inc eax
ret
endp
 
align 4
 
imports:
dll_start dd szStart
dll_ver dd szVersion
con_init dd szcon_init
con_write_asciiz dd szcon_write_asciiz
con_printf dd szcon_printf
con_set_flags dd szcon_set_flags
con_exit dd szcon_exit
dd 0
 
szStart db 'START',0
szVersion db 'version',0
szcon_init db 'con_init',0
szcon_write_asciiz db 'con_write_asciiz',0
szcon_printf db 'con_printf',0
szcon_set_flags db 'con_set_flags',0
szcon_exit db 'con_exit',0
 
dllname db '/sys/lib/console.obj',0
 
caption db 'Console test - colors',0
t1 db '–¢¥â 0x%02X: ',0
text db '‚®â ¯à¨¬¥à ⥪áâ .',10,0
text2 db 27,'[7m€ íâ® ¯à¨¬¥à ¨á¯®«ì§®¢ ­¨ï '
db 27,'[1;36;41mEsc'
db 27,'[7m-¯®á«¥¤®¢ â¥«ì­®á⥩.',10,0
 
i_end:
 
align 4
rb 2048 ; stack
mem:
/programs/develop/libraries/console/examples/testcon2_eng.asm
0,0 → 1,161
 
include 'proc32.inc'
 
DLL_ENTRY equ 1
DLL_EXIT equ -1
REQ_DLL_VER equ 2
 
use32
db 'MENUET01'
dd 1
dd start
dd i_end
dd mem
dd mem
dd 0
dd 0
 
start:
stdcall load_dll_and_import, dllname, imports
test eax, eax
jz exit
 
; check version
cmp word [dll_ver], REQ_DLL_VER
jb exit
cmp word [dll_ver+2], REQ_DLL_VER
ja exit
push DLL_ENTRY
call [dll_start]
 
; yes! Now do some work (show color strings in this case).
 
push caption
push -1
push -1
push -1
push -1
call [con_init]
; C-equivalent of the following code:
; for (ebx=0;ebx<0x100;ebx++)
; {
; con_printf(t1,ebx);
; eax = con_set_flags(ebx);
; con_write_asciiz(text);
; con_set_flags(eax);
; }
; N.B. For efficiency result of first con_set_flags is not saved
; in register, but is pushed beforehand to the stack
; for second con_set_flags.
; Note that such code cannot be generated by stdcall macros.
xor ebx, ebx
@@:
push ebx
push t1
call [con_printf]
add esp, 8
push ebx
call [con_set_flags]
push eax
push text
call [con_write_asciiz]
call [con_set_flags]
inc bl
jnz @b
push text2
call [con_write_asciiz]
push 0
call [con_exit]
exit:
or eax, -1
int 0x40
 
proc load_dll_and_import stdcall, _dllname:dword, _imports:dword
pushad
; load DLL
push 68
pop eax
push 19
pop ebx
mov ecx, [_dllname]
int 0x40
test eax, eax
jz import_fail
 
; initialize import
mov edi, eax
mov esi, [_imports]
import_loop:
lodsd
test eax, eax
jz import_done
mov edx, edi
import_find:
mov ebx, [edx]
test ebx, ebx
jz import_not_found
push eax
@@:
mov cl, [eax]
cmp cl, [ebx]
jnz import_find_next
test cl, cl
jz import_found
inc eax
inc ebx
jmp @b
import_find_next:
pop eax
add edx, 8
jmp import_find
import_found:
pop eax
mov eax, [edx+4]
mov [esi-4], eax
jmp import_loop
import_not_found:
import_fail:
popad
xor eax, eax
ret
import_done:
popad
xor eax, eax
inc eax
ret
endp
 
align 4
 
imports:
dll_start dd szStart
dll_ver dd szVersion
con_init dd szcon_init
con_write_asciiz dd szcon_write_asciiz
con_printf dd szcon_printf
con_set_flags dd szcon_set_flags
con_exit dd szcon_exit
dd 0
 
szStart db 'START',0
szVersion db 'version',0
szcon_init db 'con_init',0
szcon_write_asciiz db 'con_write_asciiz',0
szcon_printf db 'con_printf',0
szcon_set_flags db 'con_set_flags',0
szcon_exit db 'con_exit',0
 
dllname db '/sys/lib/console.obj',0
 
caption db 'Console test - colors',0
t1 db 'Color 0x%02X: ',0
text db 'This is sample text.',10,0
text2 db 27,'[7mAnd this is an example of '
db 27,'[1;36;41mEsc'
db 27,'[7m-sequences.',10,0
 
i_end:
 
align 4
rb 2048 ; stack
mem: