Jump to content

basic assembly programming on a virtual computer that only uses 3 registers

- - - - -

  • Please log in to reply
3 replies to this topic

#1
ayad001

ayad001

    Newbie

  • Members
  • PipPip
  • 14 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,822 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ayad001

ayad001

    Newbie

  • Members
  • PipPip
  • 14 posts
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.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
a + a -> c
c / a -> c (c = 2)
c / c -> c




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users