how to implement arithmetic?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

how to implement arithmetic?

kawakami
I'm at a loss when I implement arithmetic operations.
I think I should do "POP 1 or 2 times, execute process and then PUSH".
But what segments or symbols in the text do I reserve "POP"ed value(s)?
Arbitrary selected symbol name is OK?

My guess is as follows. If I have these pseudo vm code,

  push X
  push Y
  add

asm code would be

@whereX
M=0
@whereY
MD=j
@whereResult
D=D+M
@SP
M=D
@SP
M=M+1

Can I declare whereX, whereY and whereResult?
Are they nothing to do with segments like LCL, ARG, etc?

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

WBahn
Administrator
kawakami wrote
Can I declare whereX, whereY and whereResult?
Are they nothing to do with segments like LCL, ARG, etc?
No, you can't do this, because these will pollute the region reserved for static variables.

The VM Translator was THREE registers, R13, R14, and R15, available for its use. But most commands, including add, do not need to us them at all.

Don't try to write ASM code that implements the VM operations directly. By that, I mean you don't have to pop two arguments off the stack to "someplace", add them together in that "someplace" and then push the result back onto the stack.

Draw a memory map of what things look like before the command executes and what it needs to look like after the command executes. All you have to do is write ASM code that turns the first into the second.

Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

kawakami
Thank you very much for your support.
According to your advice, I got these chunk of code for "add".

      @SP
      D=M
      @SP
      M=M-1
      @SP
      M=M+D

Am I on the same page?
Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

WBahn
Administrator
kawakami wrote
Thank you very much for your support.
According to your advice, I got these chunk of code for "add".

      @SP
      D=M
      @SP
      M=M-1
      @SP
      M=M+D

Am I on the same page?
So let's desk check it and see what happens.

Let's say that SP is 503. That means that the two values to be added are in RAM[501] and RAM[502]. Let's just put some junk values in there.

RAM[0] = 503
RAM[501] = 42
RAM[502] = 58

When we are done, we want

RAM[0] = 502
RAM[501] = 100

We don't care what ends up in RAM[502] since it is no longer on the stack.

So now let's execute your code line by line:

@SP        // A = 0, D =   ?  , M = 502, RAM[0] = 502, RAM[501] = 42, RAM[502] = 58
D=M       // A = 0, D = 502, M = 502, RAM[0] =  502, RAM[501] = 42, RAM[502] = 58
@SP       // A = 0, D = 502, M = 502, RAM[0] =  502, RAM[501] = 42, RAM[502] = 58
M=M-1   // A = 0, D = 502, M = 502, RAM[0] =   501, RAM[501] = 42, RAM[502] = 58
@SP       // A = 0, D = 502, M = 502, RAM[0] =   501, RAM[501] = 42, RAM[502] = 58
M=M+D  // A = 0, D = 502, M = 502, RAM[0] = 1003, RAM[501] = 42, RAM[502] = 58

Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

kawakami
This post was updated on .
Thank you very much for your advice.
My answer is completely destroyed...
After reviewing Chap4 to 6 and playing with CPU Emulator, let me update my answer.

MOD NOTE: Code snipped after serving its purpose.

Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

WBahn
Administrator
Yes, that should work. I'm going to remove your code so that we don't leave solutions lying around the forum.

Your solution uses 10 instructions. It can be done in 5. As you can imagine, the add command is going to be calls LOTS of times, so even trimming a single instruction from it is worth quite a bit of effort -- trimming five is very significant.

But, for now, what is important is that you have something that will work and that's the first critical step. Get everything working and worry about performance later.
Reply | Threaded
Open this post in threaded view
|

Re: how to implement arithmetic?

kawakami
Great thanks.
Your advice is awesome!!