Wenhao Yang wrote
i translate this pesudo code to assemble code by myself, but it doesn't work. so i compare with code provided by website,i find that there are a little difference at line 30
i directly translate the pesudo code RAM[addr] = -1 into
@addr
M = -1
but it doesn't correctly run, what happend
why does we need to append extra instruction A = M
It looks like
addr, is the first variable used in your program so we'll assume that it was given address 16 by the assembler, and that
addr has been initialized to 16384.
The pseudocode
RAM[addr] = -1
needs to use the value of
addr as the destination in RAM that needs to be set.
It helps to write out exactly what is happening for each instruction.
Your code does this:
@addr // A = address of variable 'addr' = 16
M=-1 // RAM[A] = RAM[16] = -1
So the result is that the value of variable
addr is set to -1.
The correct code works like this:
@addr // A = address of variable 'addr' = 16
A=M // A = RAM[A] = value of variable 'addr' = 16384
M=-1 // RAM[A] = RAM[16384] = -1
--Mark
Please edit your post to remove the ASM code. We don't want complete solutions to remain in the forum. (It's OK to temporarily post like this to get help.)