SDCC Compiler Z80 - Strange use of registers

Discussion in 'Embedded Systems and Microcontrollers' started by Robin Mitchell, Apr 17, 2016.

  1. Robin Mitchell

    Thread Starter Well-Known Member

    Oct 25, 2009
    732
    200
    Hi all,

    I am using the SDCC for my ZBM project and have begun writing a library so the C program can interface with system calls (RST). I am having some trouble with this and the manual is not very helpful at all. When I generate the output assembly and go through it the first variation of C code produces a sensible output. The second variation does not.

    Here is the first C program:
    Code (Text):
    1.  
    2.     printChar('a');
    3.     userinput = getline();
    4.     printString(userinput);
    5.  
    And this is the assembler it produces:
    Code (Text):
    1.  
    2. ;Source\main.c:11: printChar('a');
    3.     ld    a,#0x61
    4.     push    af
    5.     inc    sp
    6.     call    _printChar
    7.     inc    sp
    8. ;Source\main.c:12: userinput = getline();
    9.     call    _getline
    10.     ld    (_userinput),hl
    11. ;Source\main.c:13: printString(userinput);
    12.     ld    hl,(_userinput)
    13.     push    hl
    14.     call    _printString
    15.     pop    af
    16.  
    This makes sense and I like it. Register A holds the character (not sure why it is pushing af then increment the sp though).

    Here is the second variation that DOES NOT make sense.
    Code (Text):
    1.  
    2.     userinput = getline();
    3.     printString(userinput);
    4.     printChar('a');
    5.  
    This is the generated output
    Code (Text):
    1.  
    2. ;Source\main.c:12: userinput = getline();
    3.     call    _getline
    4.     ld    (_userinput),hl
    5. ;Source\main.c:13: printString(userinput);
    6.     ld    hl,(_userinput)
    7.     push    hl
    8.     call    _printString
    9. ;Source\main.c:14: printChar('a');
    10.     ld    h,#0x61
    11.     ex    (sp),hl
    12.     inc    sp
    13.     call    _printChar
    14.     inc    sp
    15.  
    Why is it exchanging the variable found in the stack pointer with hl? And why is it now using register l instead of a like before? My printChar function relies on the same register being used every time. I dont care if it is a or l but I cant have SDCC keep changing its mind :S

    Any help?
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    It is pushing the parameters onto the stack for passing to the functions. In the second one, HL is loaded to the address of userinput and pushed so it is on the top of the stack. _printString expects the address of the string in the stack.
    ld h,#0x61 ex (sp),hl loads H with 'a' and swaps top stack with HL to get that parameter onto the stack. In this case inc sp is used instead of push/pop ops - don't know why. But that's what its doing, passing params on the stack and adjusting the SP to keep things aligned.
     
    Last edited: Apr 18, 2016
    Robin Mitchell likes this.
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,809
    362
    My experience with the sdcc is trying to use it on stm8. For how I program chips, it is essentially useless.
     
    Robin Mitchell likes this.
Loading...