flat assembler
Message board for the users of flat assembler.
Index
> Windows > Dividing negative numbers |
Author |
|
AsmGuru62 20 Sep 2013, 13:49
I think you must use FPU to get that correctly done.
This does not look like integer division/addition: Code: pi += (long double) sign / (long double) denominator; |
|||
20 Sep 2013, 13:49 |
|
lem 20 Sep 2013, 13:57
Yeah, so idiv does not does that, does it?
Do you have ideas how to actually use FP in here? |
|||
20 Sep 2013, 13:57 |
|
AsmGuru62 20 Sep 2013, 15:14
IDIV will divide EDX:EAX as an integer value, so you get the integer remainder and integer result, like if you divide 10 by 3 you get 3 (RESULT) and 1 (REMAINDER) - all integer.
I will try to make a code sample for FPU. |
|||
20 Sep 2013, 15:14 |
|
lem 20 Sep 2013, 15:16
Wow, thanks, i just did not know that idiv works with integers only. Looking forward for that sample and in meantime i'll read this
http://www.website.masmforum.com/tutorials/fptute/index.html which you've posted somewhere else in the past. |
|||
20 Sep 2013, 15:16 |
|
AsmGuru62 20 Sep 2013, 16:07
Here is some working code.
I did not print anything, so you'll have to stop in debugger and check the results. Code: ; --------------------------------------------------------------------------- ; FILE: GetPI.Asm ; DATE: September 20, 2013 ; --------------------------------------------------------------------------- format PE GUI 4.0 entry start stack 4000h, 4000h include 'Win32W.Inc' ; {INSMODDEF} Module Definitions inserted immediately before this line ; --------------------------------------------------------------------------- section '.data' data readable writeable double_PI1 dt 0.0 double_PI2 dt 0.0 ; --------------------------------------------------------------------------- section '.code' code readable executable ; {INSMODIMPL} Module Implementations inserted immediately before this line ; --------------------------------------------------------------------------- virtual at 0 loc1: .Long_Sign dd ? .Long_Denominator dd ? .CurrentPi dt ? ; 10 bytes DOUBLE .Padding rb 2 ; To align stack on DWORD .size = $ end virtual align 16 GetPI_Entry: ; --------------------------------------------------------------------------- ; INPUT: ; ECX = # of iterations ; EDI = address of a DOUBLE value where to put the result (calculated PI) ; --------------------------------------------------------------------------- ; ; Allocate local variables ; sub esp, loc1.size mov esi, esp ; ; Prepare locals ; push 1 pop eax mov [esi + loc1.Long_Denominator], eax ; Denominator = 1 add eax, 3 mov [esi + loc1.Long_Sign], eax ; Sign = 4 fldz fstp [esi + loc1.CurrentPi] ; Pi = 0.0 ; ; Loop the # of iterations in ECX ; .iteration: fild [esi + loc1.Long_Sign] fidiv [esi + loc1.Long_Denominator] fld [esi + loc1.CurrentPi] faddp fstp [esi + loc1.CurrentPi] neg [esi + loc1.Long_Sign] add [esi + loc1.Long_Denominator], 2 loop .iteration ; ; Store the result ; fld [esi + loc1.CurrentPi] fstp tword [edi] ; ; Release locals and exit ; add esp, loc1.size ret ; ---------------------------------------------- ; C++ Equivalent for ABOVE ^^^ (just the calculations) ; ---------------------------------------------- ;int main() ;{ ; int i; ; cout << "Number of iterations: "; ; cin >> i; ; long int denominator = 1; ; long double pi = 0; ; int sign = 4; ; ; for(long long int j = 0; j < i; j++) ; { ; pi += (long double) sign / (long double) denominator; ; sign = -sign; ; denominator += 2; ; } ; cout.precision(10); ; cout << "Liczba Pi w przyblizeniu wynosi: "; ; cout << pi; ; return 0; ;} ; --------------------------------------------------------------------------- ; PROGRAM ENTRY POINT ; --------------------------------------------------------------------------- align 16 start: ; ; Try it twice with different # of iterations ; mov edi, double_PI1 mov ecx, 256 call GetPI_Entry mov edi, double_PI2 mov ecx, 4096 call GetPI_Entry ; ; Now load them into FPU and compare with 'real' Pi ; fld [double_PI2] fld [double_PI1] fldpi ; ; Stop in debugger and look into FPU registers: ; ; ST0 = Pi calculated by FPU ; ST1 = Pi calculated by 256 iterations ; ST2 = Pi calculated by 4096 iterations ; int3 ; ; Get back to Windows ; invoke ExitProcess, 0 ; --------------------------------------------------------------------------- section '.idata' import data readable writeable library kernel32,'KERNEL32.DLL',user32,'USER32.DLL',gdi32,'GDI32.DLL' include 'API\Kernel32.Inc' include 'API\User32.Inc' include 'API\Gdi32.Inc' |
|||
20 Sep 2013, 16:07 |
|
lem 20 Sep 2013, 16:19
Yup, I've checked them and they are really good. Thanks a lot, that's a really nice code you did there and it's definitely more advanced than mine crap. However with being more advanced it looks simpler. Anyways, thanks a lot for your work : )
|
|||
20 Sep 2013, 16:19 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.