1

I'm a student and I got an assignment to create a calculator for big numbers (unlimited length) in c and Assembly. I'm getting the input from stdin. How do I store the input in c in most convenient way so I can do the calculations in Assembly?

I'm getting the input in postfix equation.

For example: The input can be 135136136136900913950915183591 151541514 +

sharon_k
  • 38
  • 4
  • 1
    You can sacrifice a bit of space and store the numbers in chunks of 9 digits, making it a base 10^8 representation. This makes it very easy to convert from a string to an array of longs (just convert the number into groups of 9 digits). Depending on the operations to implements and the performance goals, you can even avoid converting to an array of longs: just performs the calculations digit by digit. – Margaret Bloom Mar 24 '18 at 10:54
  • "_calculations digit by digit_" wasn't that what the IBM360 did in COBOL and using EBCDIC? – Paul Ogilvie Mar 24 '18 at 10:55
  • I don't see why you'd need assembly for this. – Steve Summit Mar 24 '18 at 10:56
  • @PaulOgilvie: I believe you're referring to [Binary-coded decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal) – President James K. Polk Mar 24 '18 at 12:14
  • 1
    @JamesKPolk, isn't that what EBCDIC includes? _Extended Binary Coded Decimal Interchange Code_ – Paul Ogilvie Mar 24 '18 at 12:15
  • @PaulOgilvie: I guess, I don't remember my EBCDIC or my BCD that well, because if you're old enough to remember them at all you're too old to remember them well :) – President James K. Polk Mar 24 '18 at 12:17
  • @MargaretBloom: 9 decimal digits per chunk (aka limb in http://gmplib.org/ terminology) is base 10^9, not 10^8. It's the largest power of 10 that fits in a 32-bit integer, and 2*10^9 doesn't wrap so carry-out is easy. (@ sharon: See https://codegolf.stackexchange.com/questions/133618/extreme-fibonacci/135618#135618 for an asm addition loop for 1000-digit numbers in this format, and for printing in decimal. It's well commented, which compensates for being optimized for code size (and performance in the inner loop. The basic method of lea/cmp/cmov replaces `adc` if you don't use a 2^n base) – Peter Cordes Mar 24 '18 at 20:08
  • @SteveSummit: have you tried getting a C compiler to generate *efficient* code for extended-precision addition, with carry-in from the previous limb, and carry-out to the next, for chunks of 2^32 or 2^64? [It's not easy to write *correct* code in C, let alone efficient](https://stackoverflow.com/q/46701364/224132). Here's another example of [an SO answer with the same subtle bug](https://stackoverflow.com/questions/4153852/assembly-adc-add-with-carry-to-c?rq=1#comment53703145_4154170) for carry-in. It's easier in asm where you can use `adc` (and that's probably the point of the assignment.) – Peter Cordes Mar 24 '18 at 20:25
  • @PeterCordes I may have misread the question, but it seemed so basic in its wording that I assumed it was relatively speaking a beginner's problem, with the point of the exercise being getting it to work at all, without worrying too much about making it maximally efficient. That's why I was surprised to see any mention of assembly. (In answer to your question, no I have not. My own MP lib, which I wrote just for fun and which makes no claims to maximal efficiency, is in pure C. It's base-2147483648, i.e. 31 bits per digit, precisely so that I can handle carries easily and simplemindedly.) – Steve Summit Mar 24 '18 at 20:48
  • @SteveSummit: Yeah, I don't think there's any efficiency requirement in the assignment, I just meant that it's one of the few things C can't do with near-maximal efficiency. Thus it's an interesting topic if you already wanted to give a C + asm assignment, which apparently this is. – Peter Cordes Mar 24 '18 at 20:55
  • @sharon: if your have extended-precision functions to shift and add, you can use those to multiply by 10, and implement the standard `atoi` loop of `total = total*10 + next_digit`, if you want to work with numbers in a binary format with chunks of 2^n, instead of chunks of 10^9 in 32-bit, or 10^18 or 10^19 in 64-bit. (or single decimal digits each in their own byte, if you don't care at all about efficiency.) – Peter Cordes Mar 24 '18 at 21:00

0 Answers0