[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'Hello world' for monitor mode and an assembler?



> 300: ldy #0  ; y register (index) set equal to 0

Hi,

Think of 300: LDY #0 as "Let Y register = 0"  the # sign indicates
it's an actual value, as opposed to a memory location's contents.

For example:

300: LDY #0
is a different 'addressing mode' than
300: LDY $0700

The # sign indicates actual value (0) and the $ indicates contents
of memory location 0700.

> 302: lda 320,y ; place contents of $320 into the y register

Actually, no, this is LDA or "LoaD Accumulator" with the contents
of memory location $320, Indexed by the Y register (which we
just loaded with a zero a line ago) so it'll start at $320
What this will do is LoaD the Accumulator with the contents of
memory location $320 + Y  (in this program, the ASCII "hello
world" in Hexadecimal stored at $320+)

> 305: jsr fded ; jump to subroutine at $fded (for outputting text)

Char out routine as you mentioned ...

> 308: iny ; increment y register (index)

Exactly, INcrement Y  so we can look at the next memory location
($320, y) which once we INcreaseY will now be $320,1 or $321.

> 309: cpy #d ; compare y register -has loop gone on for #d ?

Exactly, but it connects with the BNE in the next statement to
'finish' the loop work ...  ComPareY with (actual) # value $D
This line above merely equates the contents of theY Register
and the actual (#) value $D .. if they are equal, it returns a zero
if not, it returns a condition based upon the numbers being
compared - i.e. carry flag set, negative flag set, etc.

> 30B: bne 302 ; branch not equal. Branch to $302

Now if the Y-register has been INcrementY 'ed up to $D from the
earlier INY statement, this BNE comparison will 'zero' out or
in essence it will be equal so it will not "Branch back to $302"

BNE = Branch if (compare) is not equal to (value) returning a zero.

> Here are the things which I can't quite understand yet:
> 30B: bne 302 ; branch not equal. Branch to $302

See above... if the compare (Y register in this program) and the
(#) actual value $D (in this program) are equal to one another,
it returns a Zero as meaning equal.  Therefore in a Branch Not
Equal, it will continue with the next line of code, because the
returned value IS equal to zero.  If any other value is returned
(i.e. Y reg = $FF and you compare it to $D) it will Branch back
to $302 because the results of the comparison (CPY #D) is
not equal to zero.

> Next up is the final line of the main code - what subroutine are we
> returning from here? Is it the one from 30D above? (I would have logically
> thought a return would be issued up near FD8E).

The final line of the main code returns control to whichever program
"Called" or JSR'd to this section of memory (program) ...

So if a basic program did this:

10 Print "watch this"
20 call 768 : Rem Hello world ML program at $0300
30 end

and you didn't have the final RTS in at $30D, the basic program would
never get returned to and the computer would halt execution with a beep
and a dump of the register contents leaving you in the monitor.
Now with the RTS at $30D, the control is returned to the basic program
and it can succesfully continue running into the 'end' statement in line 30

Hopefully this clarifies some of the sketchy stuff!

Here's an excellent reference for 6502 Assembly, indicating all the opcodes
and addressing methods each of them uses:

http://www.obelisk.demon.co.uk/6502/

I highly recommend these sections:

http://www.obelisk.demon.co.uk/6502/reference.html

and

http://www.obelisk.demon.co.uk/6502/addressing.html

It mentions BBC computers, don't let that scare you ... a 6502 is a 6502 is 
a 6502! :)

later,
Craig