Quote:
> Hi freaks and other interested programmers,
> I would appreciate a better code for a kind of exchange like this:
> temp:=item1;
> item1:=item2;
> item2:=temp;
> I tried using:
> procedure change(var item1, item2 : word); assembler
> asm
> mov ax, item1
> mov bx, item2
> xchg ax, bx
> mov item1, ax <-- here it stops working
> mov item2, bx
> end;
> But it didn't work.
> Does anybody knows a fast routine (maybe predefined by pascal or in
> assembler) for this kind of work?
You neglected to mention which version of pascal you're using, so I'm
assuming TP 7.0
The problem is that when you pass something as a variable parameter to
an assembler procedure, it actually receives a pointer. The following
code seems to work:
procedure Swap (var Item1, Item2 : Word); assembler;
asm
push DS {The data segment must be preserved}
les SI, Item1 {Load the two addresses into CPU registers}
lds DI, Item2
mov AX, ES:[SI] {Swap the variables}
mov BX, DS:[DI] {Note that using xchg is actually redundant}
mov ES:[SI], BX
mov DS:[DI], AX
pop DS
end;
Hope this helps!