0

I have written a code in IJVM which performs multiplication, addition and subtraction on 1 digit numbers.

Basically it reads the input char, then checks the arithmetic symbol (+|-|*), reads the second char and performs the corresponding code (based on the symbol) calculations and inputs the result

so the input:

2+4=

results in output:

6

input:

2*4=

output:

8

Now the problem is obviously that I can't output values greater than 9 as bigger digits are more than one char. 9+1= results in output of ":",


9+2=";" (0x3B)


9+3="<" (0x3C)


Now I've tried looking into add.jas example which is provided as part of the IJVM MIC documentation on ontko.com website. it takes 2 numbers from the input and outputs the sum. However the way it handles the input and output is let's just say... different. After going through it line by line I still can't figure out how to output number consisting of 2 or 3 digits (the requirements are up to "999" for the output result and single digits for the input calculations). And ideally, I don't need those zeros at the beginning in the output which add.jas generates which are not needed and have no purpose. I mean: ("

2+4=00000006

" ? Gimme a break.)


What's the most efficient way of being able to handle larger numbers in the output (up to 999) to implement for my code?


I believe it's absolutely not necessary to paste my whole code as for someone who is more advanced in IJVM who would know what I'm talking about - he surely has a pretty good idea of how my code looks like based on my description.


For the record: I'm reluctant to share my code due to the fact that this is UNI assignment and rules regarding duplicate code / copying someone else are extremely strict with quite harsh consequences for both parties. And I know for a fact that some/many people from my UNI who have the same assignment are scouting the internet. Thank you for understanding!

SmOg3R
  • 159
  • 14
  • manual integer -> string conversion in base 10 is done with repeated division by 10. See https://stackoverflow.com/questions/13166064/how-do-i-print-an-integer-in-assembly-level-programming-without-printf-from-the/46301894#46301894 for a C implementation. (And x86 asm.) If IJVM doesn't have div / mod instructions, then use [a multiplicative inverse](https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication-by-a-strange-number-in-implementing-integer-divi) to divide by 10 (and `x - x/10*10` for mod). Even if you have to implement mul yourself, it's easier than division. – Peter Cordes Jan 10 '18 at 18:20
  • But it sounds like you already have an example of conversion *with* leading zeros. So use that but break out of the loop when the quotient is zero, instead of after a fixed number of digits. (Use a `do{}while()` loop structure to make sure you get a single `0` when the input is `0`.) – Peter Cordes Jan 10 '18 at 18:23
  • Yes it sounds quite simple when you put it this way, but let me point you to that actual example (add.jas): http://www.ontko.com/mic1/add.jas the commented bits in this file make sense, but things start to become complicated when you try to implement this piece of code or change it. It looks as if they wanted to show as many code scenarios as possible and put everything into this example. But it's too complicated for my needs and too hard to actually work with. Hence I'm not working around it but use my own code. PS. IJVM doesn't have any math instructions other than IADD and ISUB. – SmOg3R Jan 10 '18 at 18:54
  • So just to clarify, I do have a perfectly working program for single digits, but I do need an efficient way to be able to return the result properly when it's greater than 9 using nothing more than very limited IJVM instructions. – SmOg3R Jan 10 '18 at 18:58
  • You can divide by powers of ten (having lookup table for all base10 digits, starting with largest power of 10 supported, i.e. if you want to print 32 bit integers, you need 1e9 as first constant (2**32-1 = 4294967295)), then 1e8, etc... down to 100, 10, and you don't need 1 in table, you can just print the remainder smaller than 10 directly. ... btw, to return result as binary integer you need certain amount of bits, the base10 conversion is needed for conversion into characters. So your last comment is confusing for me, do you want to return value, or output it for human? What is "return"? – Ped7g Jan 10 '18 at 19:10
  • By return I simply meant the mic1 simulator output value ;) I've read your comment and I don't want to sound ignorant, but given the very limited amount of instructions in IJVM it just didn't make much sense to me and made me more confused.... Now I was thinking.. Maybe the solution to this is to convert the data to decimal values rather than char's from ASCII table? So that 0xA would correspond to 11 rather than "new line" and 0x32 would correspond to 50 rather than 2? Is that a viable solution? – SmOg3R Jan 10 '18 at 19:30
  • You have AND and conditional branches, so with that + ADD you can implement a multiply function. (`ADD same,same` is a left-shift by 1, i.e. a multiply by 2, so that's all you need to build an efficient multiply: https://stackoverflow.com/questions/2776211/how-can-i-multiply-and-divide-using-only-bit-shifting-and-adding) – Peter Cordes Jan 10 '18 at 19:46
  • Oh BTW, your `add` example converts to hex, not decimal. Hex is much easier, because every 4-bit nibble is independent. That's why it has all those leading zeros: because it's not dividing, it's just converting all 8 nibbles in a 32-bit word without checking for zero or non-zero. – Peter Cordes Jan 10 '18 at 19:49

0 Answers0