{
DWORD.INC (c) by EmiSoft 1998
operatii cu numere pe 32 de biti.
Contine:
hex_dw mov_dw_w mul16 str2dw
neg_dw mov_dw_dw div16 dw2str
shr_dw cmp_dw_dw mul_w_w
max_dw add_dw_w mul_dw_w
add_dw_dw div_dw_w
sub_dw_dw
}
procedure hex_w(w:word);
const h:array [0..$F] of char='0123456789ABCDEF';
begin
write(
h[Hi(w) shr 4],h[Hi(w) and $0F],
h[Lo(w) shr 4],h[Lo(w) and $0F]);
end;
procedure hex_dw(var x:dword);
begin
hex_w(x[1]);
hex_w(x[0]);
writeln;
end;
procedure neg_dw(var x:dword);assembler;
asm
push ds
lds si,x
not word ptr [si]
not word ptr [si+2]
inc word ptr [si]
jno @1
inc word ptr [si+2]
@1:
pop ds
end;
procedure shr_dw(var x:dword);assembler;
asm
push ds
lds si,x
xor ax,ax
shr word ptr [si+2],1
adc ax,ax
rcr ax,1
shr word ptr [si],1
or [si],ax
pop ds
end;
procedure max_dw(var x:dword);assembler;
asm
les di,x
mov ax,$FFFF
cld
stosw
stosw
end;
procedure mov_dw_w(var dest:dword; source:word);assembler;
asm
les di,dest { dest:=source }
cld
mov ax,source
stosw
xor ax,ax
stosw
end;
procedure mov_dw_dw(var dest,source:dword);assembler;
asm
push ds { dest:=source }
les di,dest
lds si,source
cld
movsw
movsw
pop ds
end;
function cmp_dw_dw(var p1,p2:dword):shortint;assembler;
const n=2;
m=2; {=2*n-2}
asm
push ds { cmp = 0,1,-1 daca p1=p2, p1>p2, p1
lds si,p1
les di,p2
add si,m
add di,m
mov cx,n
std
@cmp:
rep cmpsw
ja @maimare
jb @maimic
mov al,0
jmp @end
@maimic:
mov al,-1
jmp @end
@maimare:
mov al,1
@end:
pop ds
end;
procedure add_dw_w(var dest:dword; source:word);assembler;
asm
push ds { dest:=dest+source }
lds si,dest
mov ax,source
xor bx,bx
add [si],ax
adc [si+2],bx
pop ds
end;
procedure add_dw_dw(var dest,source:dword);assembler;
asm
push ds { dest:=dest+source }
lds si,source
les di,dest
cld
lodsw
add es:[di],ax
lodsw
adc es:[di+2],ax
pop ds
end;
procedure sub_dw_dw(var dest,source:dword);assembler;
asm
push ds { dest:=dest-source }
lds si,source
les di,dest
cld
lodsw
sub es:[di],ax
lodsw
sbb es:[di+2],ax
pop ds
end;
function mul16(x,y:word):longint;
inline($5A/$58/$F7/$E2);
function div16(x:dword;y:word):word;
inline($59/$58/$5A/$F7/$F1);
procedure mul_w_w(var dest:dword; x,y:word);assembler;
asm
push ds
mov ax,x
mov bx,y
mul bx
lds si,dest
cld
stosw
mov ax,dx
stosw
pop ds
end;
procedure mul_dw_w(var dest:dword; source:word);assembler;
asm
push ds { dest:=dest*source }
lds si,dest
mov bx,source
cld
lodsw
mov cx,ax
lodsw
xchg ax,cx
mul bx
les di,dest
stosw
mov ax,cx
mov cx,dx
mul bx
add ax,cx
stosw
pop ds
end;
procedure div_dw_w(var dest:dword; source:word; var rest:word);assembler;
asm
push ds
lds si,dest
les di,dest
add di,2
cld
lodsw
mov cx,ax
lodsw
xor dx,dx
mov bx,source
div bx
std
stosw
mov ax,cx
div bx
stosw
les di,rest
mov ax,dx
stosw
pop ds
end;
function dw2real(n:dword):real;
var x:real;
begin
x := n[0];
x := x + $00010000*n[1];
dw2real:=x;
end;
procedure str2dw(var dest:dword; var source:string);
var
x,y:dword;
i:byte;
n,r:word;
begin
mov_dw_w(dest,0);
mov_dw_w(x,1);
for i:=1 to byte(source[0])-1 do mul_dw_w(x,10);
for i:=1 to byte(source[0]) do begin
mov_dw_dw(y,x);
n:=byte(source
)-byte('0');
mul_dw_w(y,n);
add_dw_dw(dest,y);
div_dw_w(x,10,r);
end;
end;
procedure dw2str(var dest:string; var source:dword);
var s:string;
t,zero:dword;
rest:word; c:byte;
begin
s:='';
mov_dw_w(zero,0);
mov_dw_dw(t,source);
while cmp_dw_dw(t,zero)=1 do begin
div_dw_w(t,10,rest);
c:=byte('0')+rest;
s:=char(c)+s;
end;
if s='' then s:='0';
dest:=s;
end;