Making a Compiler?

Programming, for all ages and all languages.
Schol-R-LEA

Re:Making a Compiler?

Post by Schol-R-LEA »

elias wrote: it is a Z80, and Ticalc.org dosnt help. theres an assembler for teh PC, and you can transfer your programs to the calculator form teh PC, btu i wanted to write an assembler to work on the calc. so i need a built in table of every possibnle isntruciton and its opcode, take in a text file, and replace every opcode with its binary value, and output to a file?
More or less, yes, but there are some complications, even in addition to the issues of handling the labels as described earlier. Because of the various addressing modes, the mnemonics don't quite have a one-to-one correspondence with the opcodes; Also, some of the opcodes are two bytes in length, not one, even when they have the same mnemonic. For example, here are a few of the transitions for "Load to register A":
[tt]
LD A, B ;load A from register B == 78
LD A, C ;load A from register C == 79
LD A, D ;load A from register B == 7A
LD A, E ;load A from register C == 7B
LD A, H ;load A from register H == 7C
LD A, L ;load A from register L == 7D

LD A, (BC) ;load A from the address pointed to by the combined B and C registers = 0A

LD A, (DE) ;load A from the address pointed to by the combined D and E registers = 1A

LD A, (HL) ;load A from the address pointed to by the combined H and L registers = 7E

LD A, n ;load A from the byte following the opcode == 3E xx
LD A, (nn) ;load A from the address pointed to by the two bytes following the opcode == 3A xx xx

LD A, (IX+nn) ;load A from the address pointed to in index register IX, offset by the value of the two bytes following the opcode == DD 7E xx xx

LD A, (IY+nn) ;load A from the address pointed to in index register IY, offset by the value of the two bytes following the opcode == FD 7E xx xx
[/tt]
(example data taken from the Z80 Op-Code Listing on ticalc.org; all values given in hex.)

The assembler also has to produce the appropriate executable foramt for the system it is on, and check for any errors in syntax or usage. As you can see, there are some patterns to it, but they are not consistent throughout the instruction set, partly because it was extended after the original design (the Z80 was based on the Intel 8080, but the instruction set was substantially extended by the Zilog designers).

Now, it is not really all that hard, but it can be a lot of work. It would pay to think out the design thoroughly before starting to code. HTH.
elias

Re:Making a Compiler?

Post by elias »

so makign an ssembler is basicalyl busy work? i was going to include a minimal set of opcodes, and maybe expand on it.
Berserk

Re:Making a Compiler?

Post by Berserk »

Does assembler compile into machine code??

and if it does how does machine code look???

Also, is there a decompiler, so i can look at machine code of programs??

Ciao
Schol-R-LEA

Re:Making a Compiler?

Post by Schol-R-LEA »

Berserk wrote: Does assembler compile into machine code??

and if it does how does machine code look???

Also, is there a decompiler, so i can look at machine code of programs??
<yoda>
If listen you do not, learn nothing you shall! (wacks Berserk with gimer stick)
</yoda>

Seriously, Berserk, this has been addressed numerous times already; I've personally explained it in this thread once already, as well as one other (here, reply #26) recently. In the "Where do I start?" thread (reply #42) in the OS Development message board I explained it to you specifically. Please search the message boards, re-read the existing threads, and try to understand what we've said already. While these specific questions were never asked before, the answers were implicit in the previous explanations.

I truly do want to help, and have no wish to be rude, but I cannot learn these things for you. Aristotle is said to have told the young Alexander, "There is no royal road to Geometry"; this is even more true of programming. You must make an effort to understand, or our efforts to teach will be in vain.

But to go over it once again: Assembly instructions are near-exact analogs of machine opcodes. An assembler produces binary object code, which may be raw machine code or a linkable object format such as ELF (the term 'object code' has nothing to do with object-oriented programming, BTW). This is the entire purpose of an assembler - turning the assembly language program into object code.

Most object file formats are not pure machine code per se, but usually have additional information about how to link the object code to external references, how to relocate it in memory, and so on; the object code gets turned into the final machine code when the OS loads into into memory and runs it. Machine code is a raw binary, but most debuggers and dumps will display it either as hex, as the corrosponding ASCII characters, or as a disassembly (or some combination of the three). Try experimenting with DEBUG (for DOS and Windows) or gdb (for Linux) to get a feel for it.

Disassemblers do exist (most debuggers will disassemble machine code), and allow you to review the programs symbolically, but without the symbol table from when the program was assembled or compiled, they cannot recreate the original identifiers, making it harder to read than the original source code. Trying to read someone else's program from a disassembly is... trying, but it can be done.
Tom

Re:Making a Compiler?

Post by Tom »

Making a assmbler....you could try forming NASM's C/(mabe C++? ) code to make your own NASM. And then just slowly form it to your NASM ( I don't know...mabe "BerserkASM"? )
Berserk

Re:Making a Compiler?

Post by Berserk »

lol,

That's just what i was thinking, BUT, what does nasm compile with??

Ciao 8)
Tom

Re:Making a Compiler?

Post by Tom »

I thought C.
Berserk

Re:Making a Compiler?

Post by Berserk »

I know, BUT what compiler??
damonbrinkley

Re:Making a Compiler?

Post by damonbrinkley »

If you're installing from source on a Linux machine then it's compiled with gcc. I don't know what the binaries are compiled with in the Win32 ports.
Berserk

Re:Making a Compiler?

Post by Berserk »

I compiled it for Win32 (on Windows XP)

I used GCC, but when i compile it, the exe file appears to be a dos program?? how come, (NOTE: i used the DJGPP version of gcc)

When i right click it - it has all the dos program options.

I use this directive:

Code: Select all

gcc -c nasm.c -o nasm.exe
Is there something wrong with this??

Ciao :D
Berserk

Re:Making a Compiler?

Post by Berserk »

Hey,

Does anybody know how to do what i asked b4, i can't find it in the DJGPP manuals??
Tom

Re:Making a Compiler?

Post by Tom »

I belive there is a command line that makes a PE file.

Also...to make programs other than command line is hard!
Schol-R-LEA

Re:Making a Compiler?

Post by Schol-R-LEA »

By default, DJGPP gcc builds COFF files for it's object files, and links them as DOS .EXE executables designed to run using the DOS Protected Mode Interface (DPMI).

However, according to the DJGPP v2.0 FAQ, it cannot build PE executables directly. However, since PE is based on COFF, it is fairly easy to convert between the two, and a utility called RXNTDJ can be used to do so.
Berserk

Re:Making a Compiler?

Post by Berserk »

Hey,

I was just wondering: I am going to install Mandrake Linux 9. And it comes with a version of gcc. Can i use it to make windows programs??

And here's an off topic question about linux which i need BIG help with:

How do i make Mandrake Linux detect an internal Windows Fax Modem. And is it possible to use it with linux??

Ciao ;)
Post Reply