* newbie :adding larger value
@ 2003-11-27 7:17 Vadiraj C S
2003-11-27 8:07 ` Frederic Marmond
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Vadiraj C S @ 2003-11-27 7:17 UTC (permalink / raw)
To: linux-assembly
Hello Everyone..
I'm a newbie. Has any one worked on adding a large value in assembly. I mean
a value which is larger than 32 bit. How is this mathematics taken care...
--
Regards
Vadiraj C S
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: newbie :adding larger value 2003-11-27 7:17 newbie :adding larger value Vadiraj C S @ 2003-11-27 8:07 ` Frederic Marmond 2003-11-27 15:20 ` Philip Jacob Smith 2003-11-27 12:41 ` peter willy krause 2003-11-27 16:34 ` Randall Hyde 2 siblings, 1 reply; 8+ messages in thread From: Frederic Marmond @ 2003-11-27 8:07 UTC (permalink / raw) To: Vadiraj C S; +Cc: linux-assembly hum, as a float ar as an integer? i don't have much time to take care of all cases, here is the basic: 64bits integer not signed: you must add the lower 32 bits, take care of the carry, add this to the upper 32 bits and add then the upper 32 bits. (but you may loose the upper carry...) something like this: ;add 64 bits eax(upper 32bits)ebx(lower 32bits) ;to 64 bits ecx(upper) edx(lower) add edx,ebx ;add the lower 32 bits jnc no_lowcarry : if no carry, just add the upper 32bits inc ecx ;if carry, increments the upper 32bits no_lowcarry: add ecx,eax ;add the upper 32bits jnc no_upcarry ;error: this would result to a 65 bits number (too big to stands in a 64 bits number) ;handle it here no_upcarry: Fred Vadiraj C S wrote: > Hello Everyone.. > > I'm a newbie. Has any one worked on adding a large value in assembly. I mean > a value which is larger than 32 bit. How is this mathematics taken care... > > > > > > > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 8:07 ` Frederic Marmond @ 2003-11-27 15:20 ` Philip Jacob Smith 2003-11-28 6:59 ` Frederic Marmond 0 siblings, 1 reply; 8+ messages in thread From: Philip Jacob Smith @ 2003-11-27 15:20 UTC (permalink / raw) To: linux-assembly > jnc no_lowcarry : if no carry, just add the upper 32bits > inc ecx ;if carry, increments the upper 32bits > no_lowcarry: Oh my god... Ok, this is real easy now. Say we have two 64-bit numbers, as such: big_one dd 1, 2 big_two dd 3, 4 In normal intel backwardness (the right way, btw) 1 and 3 are the least signifigant dwords. It's as easy as this: add [big_one+0], [big_two+0] adc [big_one+4], [big_two+4] or, if you prefer code that compiles: mov eax, [big_two] add [big_one], eax mov eax, [big_two+4] adc [big_one+4], eax jc overflow_handler You just go from least signifigant to most signifigant adding them together, and use 'add' for the first and 'adc' for each one after that, and when you're done, the carry flag is the same as if you had added just one number, the 'jc overflow_handler' will jump only if the answer in this case would not fit into 64 bits. To subtract you do exactly the same thing, just using sub and sbc. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 15:20 ` Philip Jacob Smith @ 2003-11-28 6:59 ` Frederic Marmond 0 siblings, 0 replies; 8+ messages in thread From: Frederic Marmond @ 2003-11-28 6:59 UTC (permalink / raw) To: pj; +Cc: linux-assembly ! yes ! Hum, I feel like I have to play more often with x86 asm, I'm forgetting all but the basic... (my work makes me let the asm in the side and focus on C/C++ progs, ...) Of course you are right! Thanks to refresh my (so volatile?) memory! ;) Fred Philip Jacob Smith wrote: >> jnc no_lowcarry : if no carry, just add the upper 32bits >> inc ecx ;if carry, increments the upper 32bits >> no_lowcarry: > > > Oh my god... > > Ok, this is real easy now. Say we have two 64-bit numbers, as such: > > big_one dd 1, 2 > big_two dd 3, 4 > > In normal intel backwardness (the right way, btw) 1 and 3 are the > least signifigant dwords. > > It's as easy as this: > > add [big_one+0], [big_two+0] > adc [big_one+4], [big_two+4] > > or, if you prefer code that compiles: > > mov eax, [big_two] > add [big_one], eax > mov eax, [big_two+4] > adc [big_one+4], eax > > jc overflow_handler > > You just go from least signifigant to most signifigant adding them > together, and use 'add' for the first and 'adc' for each one after > that, and when you're done, the carry flag is the same as if you had > added just one number, the 'jc overflow_handler' will jump only if the > answer in this case would not fit into 64 bits. To subtract you do > exactly the same thing, just using sub and sbc. > - > To unsubscribe from this list: send the line "unsubscribe > linux-assembly" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 7:17 newbie :adding larger value Vadiraj C S 2003-11-27 8:07 ` Frederic Marmond @ 2003-11-27 12:41 ` peter willy krause 2003-11-27 11:51 ` Vadiraj C S 2003-11-27 12:57 ` peter willy krause 2003-11-27 16:34 ` Randall Hyde 2 siblings, 2 replies; 8+ messages in thread From: peter willy krause @ 2003-11-27 12:41 UTC (permalink / raw) To: Vadiraj C S, linux-assembly, linux-assembly On Thursday 27 November 2003 07:17, Vadiraj C S wrote: > Hello Everyone.. > > I'm a newbie. Has any one worked on adding a large value in > assembly. I mean a value which is larger than 32 bit. How is this > mathematics taken care... example from 'lib4th', adding two counted (by l.s. cell value) unlimited size integers (re http://www.lxhp.in-berlin.de/lhpf8.html ). hp -------------- counted integer representation: l.s.cell [ count ][ cell-1 ] ... [ celll-n ] EDI--^ EAX [ .. memory ] tos --^ n (n) cells ^-- m.s. cell 'cell' is a 32-bit item in memory. 2-s complement representation for -ve values. memory arrangement as w. the (lib4th) Forth data stack, pointed to by reg EDI, 'push' towards _higher_ addresses; 'top' item also in EAX. 'tos' for 'top of stack', strikt 'little endian' byte order storage. addition: (nasm syntax): nadd: call nnorm ; assure equal cells' count ; by signed extending the 'shorter' item mov ecx,eax ; counter, n cells (count of 32bit items) neg eax ; disp to 2nd oprd push ecx ; save count push esi ; & reg lea esi,[edi+eax*4] ; m.s. cell 1st oprnd lea edi,[esi+eax*4-4] : ... 2nd push esi ; save ptr to result 'tos' or eax,eax ; clr initial carry .l: mov eax,[edi] ; 2nd adc eax,[esi] ; 1st mov [edi],eax ; sum lea edi,[edi+4] ; 'lea' and 'dec' lea esi,[esi+4] ; preserve the carry flag dec ecx ; count cells size of int's jg .l pop edi ; new 'tos' ptr pop esi ; saved reg pop eax ; count item of sum integer lea edi,[edi+4] ; drop count item of 1st operand ret above code requires items of equal size. the original 'nnorm' routine is too complicated for an introductory example - it runs some other tests and modifies the int's for the shortest pair without loss of praecision. ----------------- mail to 'hp': - lx at lxhp : in-berlin : de - ----------------- ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 12:41 ` peter willy krause @ 2003-11-27 11:51 ` Vadiraj C S 2003-11-27 12:57 ` peter willy krause 1 sibling, 0 replies; 8+ messages in thread From: Vadiraj C S @ 2003-11-27 11:51 UTC (permalink / raw) To: peter willy krause; +Cc: linux-assembly > -------------- > counted integer representation: > l.s.cell [ count ][ cell-1 ] ... [ celll-n ] > EDI--^ EAX [ .. memory ] > tos --^ n (n) cells ^-- m.s. cell > > 'cell' is a 32-bit item in memory. > 2-s complement representation for -ve values. > Thanks a lot, got some idea, i guess you have algos for these type of mathematical calculations..called GMP.. -- Regards Vadiraj C S ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 12:41 ` peter willy krause 2003-11-27 11:51 ` Vadiraj C S @ 2003-11-27 12:57 ` peter willy krause 1 sibling, 0 replies; 8+ messages in thread From: peter willy krause @ 2003-11-27 12:57 UTC (permalink / raw) To: Vadiraj C S, linux-assembly, linux-assembly sorry for the confusion... > example from 'lib4th', adding two counted (by l.s. cell value) > unlimited size integers (re http://www.lxhp.in-berlin.de/lhpf8.html ). data in memory (which is of no concern w. the mentioned example): > counted integer representation: > l.s.cell [ count ][ cell-1 ] ... [ celll-n ] > EDI--^ EAX [ .. memory ] > tos --^ n (n) cells ^-- m.s. cell data on 'stack', on which the example operates, are stored in the opposite order, 'count' in EAX & @tos, m.s.cell at 'tos'-4, l.s.cell @tos-(4*eax): [ count ][ cell-n ] ... [ cell-1 ] EDI--^ EAX [ .. memory ] tos --^ n ^-- m.s.cell (n) cells ^-- l.s. cell -- mail to 'hp': lx at lxhp : in-berlin : de ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: newbie :adding larger value 2003-11-27 7:17 newbie :adding larger value Vadiraj C S 2003-11-27 8:07 ` Frederic Marmond 2003-11-27 12:41 ` peter willy krause @ 2003-11-27 16:34 ` Randall Hyde 2 siblings, 0 replies; 8+ messages in thread From: Randall Hyde @ 2003-11-27 16:34 UTC (permalink / raw) To: Vadiraj C S, linux-assembly Take a look at the chapter on Advanced Arithmetic in "The Art of Assembly Language" at http://webster.cs.ucr.edu Cheers, Randy Hyde ----- Original Message ----- From: "Vadiraj C S" <vadiraj@mail.cyberneme.com> To: <linux-assembly@vger.kernel.org> Sent: Wednesday, November 26, 2003 11:17 PM Subject: newbie :adding larger value > > > Hello Everyone.. > > I'm a newbie. Has any one worked on adding a large value in assembly. I mean > a value which is larger than 32 bit. How is this mathematics taken care... > > > > > > -- > Regards > Vadiraj C S > > > - > To unsubscribe from this list: send the line "unsubscribe linux-assembly" in > the body of a message to majordomo@vger.kernel.org > More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2003-11-28 6:59 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2003-11-27 7:17 newbie :adding larger value Vadiraj C S 2003-11-27 8:07 ` Frederic Marmond 2003-11-27 15:20 ` Philip Jacob Smith 2003-11-28 6:59 ` Frederic Marmond 2003-11-27 12:41 ` peter willy krause 2003-11-27 11:51 ` Vadiraj C S 2003-11-27 12:57 ` peter willy krause 2003-11-27 16:34 ` Randall Hyde
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).