August 21st, 2010, 05:59 AM
Basic assembly programming on a virtual computer that only uses 3 registers
Hi this is a test i sat yesterday for a job interview, I have a question from that...
- Suppose there is a virtual computer that uses Registers A, B and C only.
- You can use 4 instructions: ADD Subtract, multiply and divide.
- Each instruction takes 2 operands and one result. e.g. A+A -> A ; adds A to itself stores/overwrites it in A or A + B -> C; adds A and B and overwrites the result in C
* Division by 0 is not allowed.
* Use or literals are not allowed e.g. invalid statement: A + 2 -> B
* Move is not allowed e.g. A -> B
* virtual computer throws away the Remainder from the Division.
* Only 2 operands and a result instruction is allowed.
Given that A = 5 , B = 3 and C = 4, write Steps to put 17 in C
Answer: A * C -> A ; A - B -> C
Question I asked earlier:
A and B contain integers, C contains either 0 or 1, write steps to move A to C if C == 1 or B to C if C == 0. So write steps to move A or B depending on the value of C
A <- A - B
C <- A * C
C <- B + C
And there you have it. Mathematically, it's boils down to:
C <- B + C(A - B)
When C is 0, C(A - B) is also 0, so you end up with B.
When C is 1, C(A - B) is A - B, so you end up with B + A - B or A.
***The actual question:************
Now there are 4 Registers available to use - A , B , C and D. Contents of A and B are integers, while C and D are unknown.
Write steps to move the greater of A or B to C.
Please help me out thank you.
August 21st, 2010, 05:17 PM
Hint: Integer Division needs to be used to solve problem. This a guess on my part.
Note: The answer varies based on what happens when divide by zero happens. Or, you need to know that both A and B are not zero.
Knowing that one of A, B, C, or D is not zero is required using the idea I have. Your problem statement most likely told you more info.
Or, it is too hard for me to solve.
August 22nd, 2010, 04:46 AM
Yes I can't remember that part but I'm assuming that A and B are > 0.
Originally Posted by TimSSG
can you solve it now?
i do see that if i say -
A = 10
B = 4
A/B - > C // C = 2
C/C -> C // C = 1
A-B -> D // D = 6
C * D -> D // D = 6
D+ B -> D // D = 10
That is the way i can think of it although the problem is that, if you have A = 4 and B = 10 ,
A = 4
B = 10
A/B - > C // C = 0
C/C -> C // C = ? division by zero
A-B -> D // D = -6
C * D -> D // D = 0
D+ B -> D // D = 10
so there is a division by zero and so it doesnt work
August 22nd, 2010, 04:53 AM
August 22nd, 2010, 05:02 AM
yes that is my post, but no one answered it yet, there or here.
August 22nd, 2010, 06:00 AM
Originally Posted by anon001
A - B -> D
A/B -> C
B/A -> A
C- A -> A
C/A -> C
C+B -> C
August 22nd, 2010, 07:12 AM
Good for you, now tell everyone else where you cross-posted that you already have the answer.
Comments on this post