C code to machine code question

Discussion in 'Programmer's Corner' started by Joey_K, Dec 10, 2006.

  1. Joey_K

    Thread Starter New Member

    Dec 10, 2006
    2
    0
    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
     
  2. sci-3d

    Well-Known Member

    Aug 22, 2006
    51
    0
    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.
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    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.
    Code ( (Unknown Language)):
    1.  
    2. unsigned char memvar;
    3. .
    4. .
    5. .
    6.   memvar = 0x33 ;
    7. .
    8. .[I]<would compile to something like>[/I]
    9. .
    10.   LD  HL,memvar  ; Address of memvar to HL
    11.   LD  A,033H     ; Immediate constant 0x33 = 033H to Accumulator
    12.   LD  (HL),A     ; Store Accumulator where HL points
    13.  
    It has been a while since I wrote Z80 Assembler, but I think you get the idea.
     
  4. Joey_K

    Thread Starter New Member

    Dec 10, 2006
    2
    0
    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
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    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?
     
Loading...