6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 17, 2024 9:48 am

All times are UTC




Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 3:20 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
whartung wrote:
May as well switch to the Intel 6502 mnemonics as well.

Well, let's see ...
Code:
        PUBLIC _main
_main   PROC NEAR
        push x
        mov  x,s
        dec  s
        call _chkstk
        push y
        mov  BYTE PTR [s+1],0
        mov  y,ffh
loop    mov  a,[y+2000h]
        mov  [y+4000h],a
        dec  y
        jnz  loop
        pop  y
        mov  s,x
        pop  x
        ret


Ah ... I give up! First I started giggling, then I threw up a little in my mouth. I need to get back to work on my 65m32 specs, and this might have just broken my writer's block!

Mike


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 5:04 am 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
scotws wrote:
Just out of idle curiosity, is there any text that explains how to write an assembler? Something like the Dragon Book for compilers? Not that I'm planning another project, it would just be interesting to learn how they work internally.

Simple one are reasonably simple, but not trivial.

As with anything else, you have the Lexing and Parsing phases. Lexing breaks the individual assembly source lines in to tokens for use by the parser. Parsing takes those tokens, ensures that they actually make sense, and then acts upon them in some way. The most difficult component for the parser, at the statement level, is the expression parser. Things like: 1+2*(3+4). Folks get hung up on expressions. The rest of it is pretty simple, simple pattern matching really.

Mine is a simple Recursive Descent parser.

Assemblers are simpler that compilers because the semantics, especially with a simple instruction set like the 6502, are very straight forward. You don't have to worry as much about scopes and call stacks, etc. You can take advantage of the fact that your statements are pretty much all contained on a single line, that simplifies things as well.

For the basic case, you need to look up the mnemonic, determine the addressing mode and arguments, and then append the correct instruction sequence to your output. That's what a Forth assembler does.

Forward references complicate things a bit, and that where the "second pass" kicks in.

For example:
Code:
LABEL1:  LDA $1234
         BEQ LABEL2
         JMP LABEL1
LABEL2:  ...

When you encounter the JMP statement, it's destination is already defined, so the JMP is readily assembled.

However, for the earlier BEQ, you don't know at that point where, or even if, LABEL2 is defined, so you simply don't have the information necessary to properly assemble the instruction. This statement may even be in error, if LABEL2 never show up or is out of range.

So, the second pass can be done different ways. It could be as simple as literally reassembling the file, but now you have a better idea from the first run what it all looks like. In my assembler, I simply capture instructions that I don't quite know enough yet and at the end of the first phase, run through those few and fill in the blanks. With a modern machine, loading the entire assembly file in to memory is completely doable, this can make multiple passes quick.

My assembler effectively does that. It loaded each instruction into memory, if the instruction is "known" (like JMP above), it captures it, including it's destination address, and keeps moving. If it's not, it flags the instruction for later. At the end, it simply dumps the assembled instructions out to a buffer, that's finally written to the disk. My stuff is simple enough that I don't get "phase errors", as other have mentioned.

For example, if the instruction knows it's talking to ZP, it compiles ZP. If it doesn't know, it assumes it won't be ZP.

If you do something like:
Code:
        .ORG $0200
        LDA LABEL1
        .ORG $0000
LABEL1: .BYTE $01

You'll get AC 00 00 assembled instead of A5 00 assembled. Since all my ZP is defined first, this isn't a problem.

Then there's macros and other stuff, but in the end, the fundamentals are the same: converting an instruction stream in to the binary (or not, mine produces HEX files).

But, that's the basics of it.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 5:27 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8494
Location: Midwestern USA
whartung wrote:
banedon wrote:
One other (very small) point is that many assmeblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D

May as well switch to the Intel 6502 mnemonics as well.

Not familiar with those. :shock:

Quote:
As they say, "Do what you want, you will anyway." Assembly is "less portable" at the source level than many language, mostly for reason of the pseudo ops and macros.

But a lot of the 6502 stuff is "ok" portable.

Assembly language of any kind has limited portability because it works at the bare metal. Chip registers, memory maps, etc., all differ, even if the MPU is the same.

Quote:
If you have no expectation of sharing your source, no reason to follow convention of any kind.

Also true if he has no expectation of anyone adopting his work.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 5:29 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8494
Location: Midwestern USA
barrym95838 wrote:
whartung wrote:
May as well switch to the Intel 6502 mnemonics as well.

Well, let's see ...
Code:
        PUBLIC _main
_main   PROC NEAR
        push x
        mov  x,s
        dec  s
        call _chkstk
        push y
        mov  BYTE PTR [s+1],0
        mov  y,ffh
loop    mov  a,[y+2000h]
        mov  [y+4000h],a
        dec  y
        jnz  loop
        pop  y
        mov  s,x
        pop  x
        ret


Ah ... I give up! First I started giggling, then I threw up a little in my mouth. I need to get back to work on my 65m32 specs, and this might have just broken my writer's block!

Mike

What a horrid mess! I almost didn't make it to the puke pan in time. :shock:

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 6:11 am 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
whartung wrote:
BitWise wrote:
My assembler is Open Source and written in Java so it runs on pretty much anything (including Windows 7), supports the 6501/6502/65C02/65SC02/65C816/65C832, has macros/conditional assembly/repeats/includes, can generate absolute or relocatable code, supports structured programming (with no limits on code block sizes), is pretty MOS syntax compliant, and could be modified to allow ampersand in front of hex constants (change the scanToken routine).

But it doesn't produce a listing file, correct?

Like:
Code:
0200           .ORG $0200
0200 A9 00     LDA #$00   ; Clear ACC


style file? or a label dump or cross reference? Does it provide any of those? I didn't see any options.

It produces a listing which contains an alphabetically sorted symbol dump.

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 4:19 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10981
Location: England
barrym95838 wrote:
Well put, BDD.

What happened to your larger font-size? I almost didn't recognize you, because your custom font-size is an integral part of makes your voice "yours". I hope that you just forgot this time, and didn't let BigEd bully you into doing it like everyone else. Celebrate and preserve your uniqueness, octothorpes and all!!! Conformity equals boredom.

Mike
I hope I'm more of a nag than a bully. And I do try to stop myself. As I can't expect to change anyone's behaviour, I try to settle for stating an alternate view. Silly habits such as font size tricks are much less important to me than behaviour likely to make the forum a worse place.
Cheers
Ed


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 4:54 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1949
Location: Sacramento, CA, USA
BigEd,

You are correct, of course. In my neck of the woods, it's much less offensive to call someone a 'bully' rather than a 'nag', especially between dudes. Forgive me for having a tiny bit of fun at your expense ... I was feeling a bit goofy this weekend. Not everyone gets my strange, nerdy sense of humor. I'll tone it down a notch.

Mike


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Mon Sep 09, 2013 5:23 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10981
Location: England
No problem!


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Tue Sep 10, 2013 9:05 am 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
whartung wrote:
As with anything else, you have the Lexing and Parsing phases.


Thanks for the backgrounder. It actually sounds like it would deepen your understanding of assembler code to write an assembler program. Still, I think I'll pass for now :D ...


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Tue Sep 24, 2013 3:00 am 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 411
Location: Minnesota
Garth Wilson wrote:

Quote:
I absolutely detest C's "0x1A0" for example, as "x" in any other context means that either that digit is unknown or it doesn't matter, so it could be $001A0 or 011A0 or $021A0 etc.. And why put a leading 0 on it.


In C a leading zero on a numeric literal indicates a radix other than decimal. '0' by itself indicates octal, '0x' indicates hexadecimal. I believe it's non-standard, but some implementations even recognize '0b' as indicating binary.


Top
 Profile  
Reply with quote  
 Post subject: Re: 6502 BBC Compiler
PostPosted: Sat Nov 23, 2013 4:07 pm 
Offline

Joined: Sun Feb 22, 2004 9:01 pm
Posts: 105
banedon wrote:
One other (very small) point is that many assemblers use the $ sign to represent a hex number. I prefer the & sign. I know, I know: it's extremely minor - but it's another reason :D
So, allow both & and $ as a hex prefix. You're not writing a compiler or interpreter, so &DEF won't get confused for address-of DEF and $DEF won't get confused for string-at DEF.
Coming from the same background, I also allow .label and label: in assemblers that I have written (eg AsmPDP.

_________________
--
JGH - http://mdfs.net


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron