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
Note:
* 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.
Sample question:
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
Answer
Start with
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 new 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.
basic assembly programming on a virtual computer that only uses 3 registers
Started by ayad001, Aug 21 2010 01:57 AM
3 replies to this topic
#1
Posted 21 August 2010 - 01:57 AM
|
|
|
#2
Posted 21 August 2010 - 04:42 AM
What do you have so far?
#3
Posted 21 August 2010 - 12:39 PM
this is what i have come up with so far: the thing is that we can use the formulat B + (A-B)C ->C where C is either 0 or 1, so if C was 1 this would select A but if it was 0 it would select B. Now the problem is how to set C to 0 or 1, e.g. if say A = 10 , B = 4, then C ->A/B =2 or C-> B/A = 0. But C needs to be 1 or 0.
So i am stuck, i've tried different methods but I am not able to think of anything.
So i am stuck, i've tried different methods but I am not able to think of anything.
#4
Posted 21 August 2010 - 03:36 PM
a + a -> c
c / a -> c (c = 2)
c / c -> c
c / a -> c (c = 2)
c / c -> c
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









