Assembly command for "return" vm command is too long.

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

Assembly command for "return" vm command is too long.

Henoktes722
I'm trying to do the hack assembly part of the return command. But before I did on the computer, I wrote in a paper, It has many assembly instruction. So is that expected or Am I missing a concept. Here is the assembly.

// FRAME = LCL
@LCL
D=M    // push LCL at temp
@TEMP
M=D

// RET = *(FRAME - 5)
@5
D=A
@TEMP
A=M
A=A-D
D=M      // Return address

@R14 // Save return address
M=D

@TEMP
D=A    // push the return address to  temp + 1
@1
D=A+D

@R14 // Restore return address
A=M

@R15
M=D
D=A    // change D and A
@R15
A=M

M=D // finally save the return address at temp + 1

// pop and get top of the stack
@SP
M=M-1
@SP
A=M
D=M

// save top stack value at arg0
@ARG
A=M
M=D

// set SP to ARG + 1
@ARG
D=M
@1
D=A+D
@SP
M=D

// *(FRAME-1)
@1
D=A
@TEMP
A=M
A=A-D
D=M

// set THAT to *(FRAME-1)
@THAT
M=D

.
.
.

The same applies to THIS, ARG and LCL segments by changing the segment and the index, which is 1 and THAT in the above example


// goto RET
@1
D=A
@TEMP
A=A+D
A=M
0;JMP
Reply | Threaded
Open this post in threaded view
|

Re: Assembly command for "return" vm command is too long.

WBahn
Administrator
Henoktes722 wrote
I'm trying to do the hack assembly part of the return command. But before I did on the computer, I wrote in a paper, It has many assembly instruction. So is that expected or Am I missing a concept. Here is the assembly.
I don't have time to look at it in detail, but I suspect that you are implementing asm code to do each step as a separate entity (for instance, performing each pop operation separately).

Instead, make a map of your machine state before the return command starts and what you need to make it look like by the end of return command. Now just figure out assembly code to do what needs to be done.