#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    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


    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 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Posts
    38
    Rep Power
    9
    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.

    Tim S.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by TimSSG
    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.

    Tim S.
    Yes I can't remember that part but I'm assuming that A and B are > 0.

    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

    any thoughts?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0
    yes that is my post, but no one answered it yet, there or here.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by anon001
    yes that is my post, but no one answered it yet, there or here.
    got it

    A - B -> D
    A/B -> C
    B/A -> A
    C- A -> A
    C/A -> C
    D*C ->C
    C+B -> C
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    Good for you, now tell everyone else where you cross-posted that you already have the answer.

    Comments on this post

    • ptr2void agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo