# C code to machine code question

#### Joey_K

Joined Dec 10, 2006
2
I'm trying to figure out how to write C programs instead of assembler so after compile it'll run in a Z80 microprocessor. I'm using the SDCC compiler but what do you need to write in C so that it'll become the Z80 machine code? Let's say the Z80 assembler LD A, 33h(load register A with 33h) which is 3E, 33 in machine code, what would a C program looks like to get this same machine code after compilation?

I know this sounds like a stupid question but I just can't figure out the connection between the two. Any help will be greatly appreciated.

JK

#### sci-3d

Joined Aug 22, 2006
51
Briefly.

When you compile C source by a C compiler (*.c), it is translated to Assembly (*.asm) and finally assembler and linker will convert all to machine code (*.ihx/*.hex). You can check the Asm file (.asm) after you have compiled C file in the same directory for SDCC.

I also use SDCC for my 80c51 programming.

#### Papabravo

Joined Feb 24, 2006
18,453
I'm trying to figure out how to write C programs instead of assembler so after compile it'll run in a Z80 microprocessor. I'm using the SDCC compiler but what do you need to write in C so that it'll become the Z80 machine code? Let's say the Z80 assembler LD A, 33h(load register A with 33h) which is 3E, 33 in machine code, what would a C program looks like to get this same machine code after compilation?

I know this sounds like a stupid question but I just can't figure out the connection between the two. Any help will be greatly appreciated.

JK
You can't get the compiler to generate that single instruction in isolation. The C language implements a memory paradigm with a large linear address space. The language tends to ignore hardware registers as named variables. Their use as local variables is permitted but it can lead to non-portable code.

The following assignment statement in C will contain the instruction you asked for.
Rich (BB code):
unsigned char memvar;
.
.
.
memvar = 0x33 ;
.
.<would compile to something like>
.
LD  HL,memvar  ; Address of memvar to HL
LD  A,033H     ; Immediate constant 0x33 = 033H to Accumulator
LD  (HL),A     ; Store Accumulator where HL points
It has been a while since I wrote Z80 Assembler, but I think you get the idea.

#### Joey_K

Joined Dec 10, 2006
2
Thanks for the help, I got some idea now but I can't find any Z80 header files in SDCC. For me to access the Z80's internal hardware, I need a .h header file but all I can find was a z180.h for the Z180 processor in SDCC, so does that mean I have to write one myself? And how would one looks like?

Thanks.

JK

#### Papabravo

Joined Feb 24, 2006
18,453
Thanks for the help, I got some idea now but I can't find any Z80 header files in SDCC. For me to access the Z80's internal hardware, I need a .h header file but all I can find was a z180.h for the Z180 processor in SDCC, so does that mean I have to write one myself? And how would one looks like?

Thanks.

JK
I can't remeber if the Z180 was an architectural advancement or an embedded version of the Z80. If you have to create one it will look like the one for the Z180. You will have to understand both devices for this approach to be successful. Why not ask the SDCC author what the situation is?