• Advertisement
Sign in to follow this  

Unity Assembly programming question

This topic is 3614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was given this question during a take-home-videogame-job-interview-exam. I had almost no clue how to solve it at the time. I am just curious what the solution is and also thought it might be useful for people trying to break into the industry. So here goes: You have been given the opportunity to do some work on the new TISC (two instruction code) microprocessor, which has been designed to execute at lightning speeds. The processor implements only two instructions: STO <register>,<address> ( store from this register to this address ) SUB <register>,<address> (subtract the value at this address, from this register) It also contains only two 32 bit registers: A and B The machine you’re working on has two 32 bit ram addresses: 0 and 1 When the machine boots up, assume it magically begins executing your code. The value of A and B are unknown. Write a program to copy RAM location 0 into RAM location 1, and then optimize the program to use the fewest number of instructions possible.

Share this post


Link to post
Share on other sites
Advertisement
This is what I can work out in a few mins... surely not optimal, but should do the job.

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = - m0
STO A, (0) // m0 = - m0

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = 0 - (-m0) = m0

STO A, (1)

Share this post


Link to post
Share on other sites
Looks like others beat me to it.


STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0




EDIT: oops, im a retard. I was using address 1 to zero with. fixed now.

Share this post


Link to post
Share on other sites
Quote:
Original post by samoth
This is what I can work out in a few mins... surely not optimal, but should do the job.

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = - m0
STO A, (0) // m0 = - m0

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = 0 - (-m0) = m0

STO A, (1)


Given his description, that doesn't work! The first parameters to STO and SUB can only be registers.

In fact, given the description, this is impossible! Register A and B can be any value. You can't store any of your own values to A or B. Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.

Share this post


Link to post
Share on other sites
Quote:

In fact, given the description, this is impossible! Register A and B can be any value. You can't store any of your own values to A or B. Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.


Its entirely possible, as many people have shown. We don't need our own values, the only important value is the value in register 0. Its like using variables in math class ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by NerdInHisShoe
I came up with:

STO A, 1
SUB A, 0
SUB A, 1
STO A, 1
STO B, 1
SUB B, 0
STO B, 1
SUB A, 1
STO A, 1



Hmm, could you briefly explain what each line does.

Share this post


Link to post
Share on other sites
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Halifax2
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.


Share this post


Link to post
Share on other sites
Quote:
Original post by Halifax2
Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.
It says "from the register" so the assumption is the result is stored in the register.

Here's my version which has 1 less instruction than some others:

STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

Quote:
Original post by Halifax2
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.
When he's saying '-0' and '+0' in the comments he means the -/+ of the value originally stored in memory location 0, not actually the value 0. So SUB B,0 is equivalent to 0 - (-0) = +0.

[Edited by - prtc22 on March 24, 2008 8:15:08 PM]

Share this post


Link to post
Share on other sites
Well the question doesn't state whether address 0 needs to be kept original or not, so here's one that'll keep address 0 intact:

STO A,1
SUB A,1 ; A = 0

SUB A,0 ; A = -0
STO A,1
SUB A,1 ; A = 0
SUB A,1 ; A = +0
STO A,1

Edit: D'oh! Beaten by a minute! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by prtc22
Quote:
Original post by Halifax2
Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.
It says "from the register" so the assumption is the result is stored in the register.

Here's my version which has 1 less instruction than some others:

STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

Quote:
Original post by Halifax2
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.
When he saying '-0' and '+0' in the comments he means the -/+ of the value originally stored in memory location 0, not actually the value 0.


Ah okay, my fault.

And by the way, which company gave you this test?

Share this post


Link to post
Share on other sites
Quote:
Original post by samoth
This is what I can work out in a few mins... surely not optimal, but should do the job.

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = - m0
STO A, (0) // m0 = - m0

STO A, (1) // dup A
SUB (1), A // A = 0

SUB (0), A // A = 0 - (-m0) = m0

STO A, (1)


Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]

Share this post


Link to post
Share on other sites
Quote:
Original post by Halifax2
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.


I'm assuming SUB uses unsigned integral math, in which case "-0" is just my shorthand for the complement of the value in the 0 address, with respect to the memory size of 32 bits.

For example, if the memory size was 4 bits, the maximum representable value would be 15 (once again, assuming unsigned values). Just for kicks, assume the value in address 0 is 5 at the start. After zero-ing both registers, the last four commands are:

SUB A,0 // register A now has (0 - 5) % 15 => 10
STO A,0 // address 0 now has A's value of 10
SUB B,0 // register B now has (0 - 10) % 15 => 5
STO B,1 // address 1 now has B's value of 5

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]
Your second to last line doesn't actually do [A = 0-(-m0) = m0], it does [A = (-m0) - 0 = -m0], so the result you then store in memory location 1 is -m0.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]

Edit: yah... lotsa editting in this thread...

Share this post


Link to post
Share on other sites
Quote:
Original post by lougv22
Quote:
Original post by NerdInHisShoe
I came up with:

STO A, 1
SUB A, 0
SUB A, 1
STO A, 1
STO B, 1
SUB B, 0
STO B, 1
SUB A, 1
STO A, 1



Hmm, could you briefly explain what each line does.


Sorry, theres an error in my first solution, disregard it.

I did this on paper like this:

A C
B D

A, B are registers, C D are memory locations 0 and 1 respectively

The trick is to get -C in memory and 0 in a register to do 0 - (-C) = C, then move it to memory


STO A,1
A C
B A

SUB A,0
A-C C
B A

SUB A,1
-C C
B A

STO A,1
-C C
B -C

STO B,0
-C B
B -C

SUB B,0
-C B
0 -C

SUB B,1
-C B
C -C

STO B,1
-C B
C C

Share this post


Link to post
Share on other sites
Quote:
Original post by prtc22
Quote:
Original post by Bob_the_dev
Using this, and assuming I'm doing this correctly, I got it down to 6 lines:
STO A, (1) [m1 = A]
SUB A, (1) [A = A - A = 0]
STO A, (1) [m1 = 0]
SUB A, (0) [A = 0 - m0 = -m0]
SUB A, (1) [A = 0-(-m0) = m0]
STO A, (1) [m1 = m0]
Your second to last line doesn't actually do [A = 0-(-m0) = m0], it does [A = (-m0) - 0 = -m0], so the result you then store in memory location 1 is -m0.


Ahh you're right. Bummer

Share this post


Link to post
Share on other sites
Quote:
Original post by Halifax2
Quote:
Original post by prtc22
Quote:
Original post by Halifax2
Where does the subtraction end up at when the value at the address is subtracted from the register. Is it in memory, or does the result get stored in the register.
It says "from the register" so the assumption is the result is stored in the register.

Here's my version which has 1 less instruction than some others:

STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

Quote:
Original post by Halifax2
Quote:
Original post by jorgander
STO A,1 // Following four commands zero both registers
SUB A,1
STO B,1
SUB B,1

SUB A,0 // A has -0
STO A,0 // 0 has -0
SUB B,0 // B has +0
STO B,1 // 1 has +0


That doesn't work because the 'SUB B,0' is subtracting -0 from the number 0. Which results in -0 again.
When he saying '-0' and '+0' in the comments he means the -/+ of the value originally stored in memory location 0, not actually the value 0.


Ah okay, my fault.

And by the way, which company gave you this test?


I think it was Budcat Creations.

Share this post


Link to post
Share on other sites
I noticed many of you are only using register A in their solutions. It seems to me if register B wasn't needed they wouldn't give it to us. Can those who didn't utilize register B explain why?

Share this post


Link to post
Share on other sites
This was my solution:
Quote:
Original post by prtc22
STO A,1 // [1] = A
SUB A,1 // A = 0
SUB A,0 // A = -v (where v = original value at memory location 0)
STO A,1 // [1] = A = -v
SUB A,1 // A = -v - (-v) = 0
SUB A,1 // A = 0 - (-v) = v
STO A,1 // [1] = v

You don't need to use both registers for this question because you don't need to hold more than one subtraction result at a time to complete the task.

Share this post


Link to post
Share on other sites
I came up with this:

Instruction |	State
____________________________________________
| A: x B: y 0: v 1: ?
STO A, 1 | A: x B: y 0: v 1: x
SUB A, 1 | A: 0 B: y 0: v 1: x
STO B, 1 | A: 0 B: y 0: v 1: y
SUB B, 1 | A: 0 B: 0 0: v 1: y
SUB A, 0 | A: -v B: 0 0: v 1: y
STO A, 1 | A: -v B: 0 0: v 1: -v
SUB B, 1 | A: -v B: v 0: v 1: -v
STO B, 1 | A: -v B: v 0: v 1: v


(effectively NerdInHisShoe's solution, from a quick look)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Vu Chi Thien
      Hi fellow game devs,
      First, I would like to apologize for the wall of text.
      As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha). 
      Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
      - The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
      - When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
      - There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
      speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
      Also I have a few questions:
      - In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
      - Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
      - Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
      Please help me out with this. I haven't found any topic about this yet on gamedev, and this is my final piece of the puzzle. Thank you guys very very much.
    • By Estra
      Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
      Looking for
      Programmer
      1 experience using Unity/C++
      2 have a portfolio of Programmer
      3 like RPG game ( Rune rune factory / zelda series / FF series )
      4 Have responsibility + Time Management
      and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
      Split %: Revenue share. We can discuss. Fully Funded servers and contents
      and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
      we can talk more detail in Estherfanworld@gmail.com Don't comment here
      Thank you so much for reading
      More about our game
      Memory Trees : forget me not

      Thank you so much for reading
      Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
      because If not it will waste time not one but both of us
       

    • By RoKabium Games
      We've now started desinging the 3rd level of "Something Ate My Alien".
      This world is a gas planet, and all sorts of mayhem will be getting in our aliens way!
      #screenshotsaturday
    • By Pacoquinha Studios
      Kepuh's Island is Multiplayer 3D Survival Game where you survive on the Kepuh's Islands, confronting challenges that are not only other players but also bosses, and even the environment itself.
      We have a lowpoly faster battle-royale idea, where about 12 players on the map fighting for survival! Also adding some more things into that style such as bosses around the map giving you abilities and much more such as vehicles, weapons, skins, etc...
      Now we are on cartase which is a crowdfunding online which purpose is to raise funds for the development of the game. Come and be part of this development.
      Link for Cartase: https://www.catarse.me/kepuhsisland?ref=project_link
      We post updates and trailers on
      Twitter: https://twitter.com/pcqnhastudios
      Facebook: https://www.facebook.com/pacoquinhastudios/
      Site: http://pacoquinhastudios.com.br
      If you could check out it would be great
      Thnks
      Some images:





  • Advertisement