Discussion:
[Sdcc-user] Local variable in mixed asm/c vs compiler warning
Marco Peereboom
2017-04-28 12:36:49 UTC
Permalink
Consider the following snippet:

// override getchar from stdlib
int
getchar(void)
{
__asm
l1: in a,(I8251_STATUS) // get status
and #I8251_S_RXRDY // check RxRDY bit
jp z,l1 // not ready, loop
in a,(I8251_DATA) // get char
ld h,#0 // reset high return value
ld l,a // set low return value
__endasm;
// we eat the warning to save 3 bytes.
}

Get’s compiled into:
; Function getchar
; ---------------------------------
_getchar::
;i8251.c:28: __endasm;
l1:
in a,(((0x02) + 1))
and #(1<<1)
jp z,l1
in a,(((0x02) ))
ld h,#0
ld l,a
ret

Which is exactly what I want but the compiler complains. I have seen
some solutions where it is suggested to put the code in an asm file
instead but I was wondering if there is a #pragma or something else
I can use to in this specific case to shut the compiler warning up?

Loading a into a local variable works too but it is more code as well.
I am just wondering what the best practices are for these types of
mixed asm/c are with sdcc.

/marco
Maarten Brock
2017-04-30 10:41:21 UTC
Permalink
Post by Marco Peereboom
// override getchar from stdlib
int
getchar(void)
{
__asm
l1: in a,(I8251_STATUS) // get status
and #I8251_S_RXRDY // check RxRDY bit
jp z,l1 // not ready, loop
in a,(I8251_DATA) // get char
ld h,#0 // reset high return value
ld l,a // set low return value
__endasm;
// we eat the warning to save 3 bytes.
}
; Function getchar
; ---------------------------------
;i8251.c:28: __endasm;
in a,(((0x02) + 1))
and #(1<<1)
jp z,l1
in a,(((0x02) ))
ld h,#0
ld l,a
ret
Which is exactly what I want but the compiler complains. I have seen
some solutions where it is suggested to put the code in an asm file
instead but I was wondering if there is a #pragma or something else
I can use to in this specific case to shut the compiler warning up?
Loading a into a local variable works too but it is more code as well.
I am just wondering what the best practices are for these types of
mixed asm/c are with sdcc.
/marco
Maybe you can tell us what warning you get that you want to get rid of?

That being said, when the complete function is written in asm you probably
want to use the __naked keyword to suppress the red tape.

Maarten
Marco Peereboom
2017-04-30 12:58:56 UTC
Permalink
Post by Maarten Brock
Maybe you can tell us what warning you get that you want to get rid of?
I am sorry about.
i8251.c:28: warning 59: function 'getchar' must return value
Post by Maarten Brock
That being said, when the complete function is written in asm you probably
want to use the __naked keyword to suppress the red tape.
And that does exactly what I wanted! Thank you.

Did I miss this in the documentation?

Loading...