Autor Beitrag
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Do 08.02.07 05:00 
Moin,

Ich versuche momentan den Div-Befehl in ASM zu verstehen, aber irgendwie klappt da wat nicht.

Ich machs mal ganz einfach:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function test:integer;
var x,y :integer;
begin
 x:=10//x und y sind integer
 y:=2;
 asm
  mov eax,x
  mov edx,y
  div eax,edx
  mov @result, eax
 end;
end;


Eigentlich müsste das Ergebniss der Funktion doch 5 sein, ich erhalte aber -11.
Bei x=5 und y=2 erhalte ich -6, und das register edx bleibt auf 2. Irgendwie kann ich auch keinen Zusammenhang feststellen.

Wo wird der Rest einer Division abgelegt?
Wie benutzt man den div-Befehl richtig?

danke und gruss!
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Do 08.02.07 15:15 
DIV INSTRUCTION

Purpose: Division without sign.

Syntax:

DIV source

The divider can be a byte or a word and it is the operator which is given the instruction.

If the divider is 8 bits, the 16 bits AX register is taken as dividend and if the divider is 16 bits the even DX:AX register will be taken as dividend, taking the DX high word and AX as the low.

If the divider was a byte then the quotient will be stored on the AL register and the residue on AH, if it was a word then the quotient is stored on AX and the residue on DX.

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 08.02.07 19:08 
Nix für ungut user profile iconrizla, aber ne Kopie eines englischen ASM-Handbuches hilft selten ...

Das Problem bei deinem Source ist, dass Du missachtest, dass DIV mehrere Register zusammenzieht, um das Ergebnis aufzubereiten.

Versuch das mal so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
function IntDivTest(A: Integer; B: Integer): Integer;
//  =>  EAX     Zu teilende Zahl :P
//  =>  EDX     Zahl, durch die geteilt werden soll
//  <=  EAX     The Result ;-)
asm
    XOR     ECX, ECX     //Prepare ECX to be zero
    XCHG    EDX, ECX     //Exchange ECX and EDX

    //The number to divide now is stored in EDX:EAX
    //The number to divide by is in ECX
    DIV     ECX

    //EAX now holds the result
    //EDX holds the remainder
    //ECX is left unchanged

    //Return the Result
end;


@rizla: Wenn Du schon Handbücher zitierst, dann wenigstens die korrekten Stellen. Das Zitieren eines x86-Handbuchs für 16-Bit-Umgebungen bringt einem unter einer 32-Bit-Umgebung nicht viel (auch wenn die Technik die gleiche ist).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Karlson Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Do 08.02.07 22:13 
Wunderbar.
Jetzt funktioniert es, vielen Dank Ben.

Ich dachte zunächst DIV würde ähnlich funktionieren wie ADD oder SUB.

Kennt einer von euch zufällig ein (etwas ausführlichere) Befehsauflistung? Es reicht ja ein Satz pro Befehl oder ein Stück Beispielcode.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 08.02.07 22:31 
Vom mitp-Verlag gibt's die Ge-Packt-Reihe ... u.a. auch für Assembler. Dort stehen alle Befehle incl. SIMD drin. Kostet knapp 15 Euro, lohnt sich aber ... Da sind auch die Besonderheiten einiger Befehle mit erklärt.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
rizla
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 417
Erhaltene Danke: 2

XP
FPC mit Lazarus
BeitragVerfasst: Fr 09.02.07 14:28 
@BenBe: hast ja recht, aber sollte auch nur die prinzipielle funktionsweise von div erklären. ein [e] vor die register und gut ist, insofern man mit [e]axbxcxdx arbeiten will..
:r:

_________________
if you have what they want - they'll find a way to take it (bruce sterling)
WOW - 10 JAHRE Mitglied beim Delphi-Forum. Wie die Zeit vergeht, Freunde.