I’ve got a very complicated question in a ‘new’ primitive assembly in one of my interviews (why the hell QA requires an assembly knowledge when they told me their QA’s tests based on Python… ?), and it’s goes like this:
Assume your assembly language includes ONLY the following instructions:
'inc REG': increments a given register by one.'dec REG': decrement a given register by one.'jnz LABEL': jumps to a given LABEL if the previous instruction’s result was not zero.'HELT': stops running.
Task: A and B registers hold non-negative values.
The program should calculate the value of A*B and locate the result in C.
In addition, the language holds registers C,D,…,Z, which you can assume are initialized at program start to zero.
There are few points here that required more attention, like that you have to take in advance that A and\or B values may be zero.. and multiple by zero is… zero.. this is damn tough..
P.S. Because of that question I didn’t make it to reach the implementation of the ‘my_atoi()’ ‘s question.. 🙁
Thanks !
I’m not going to give the full answer, but the key here is that you have to define a
movroutine yourself. Assumingdec XwhereXholds 0 produces a negative (or very large) number, that can be done as:Once you have the appropriate
movroutines, you can implement addition as repeated increment and multiplication as repeated addition. In both, you’ll need theinctrick to getjnzto work, and in the multiplication routine, you’ll need to end with a subtraction.