Re: mixing C and assembly



Walter Banks wrote:

CBFalconer wrote:

cbarn24050@xxxxxxx wrote:

No you haven't!!
Yes he has. In addition, the assembly programmer always has some
extra tricks available, that result in shorter and faster
programs. For example, consider a program than needs functions
foo() and bar(). It turns out that a foo call is always followed
by a bar call, but that bar needs to be separately callable. As
an example, write space to stdout, and write char to stdout. The
assembly programmer can write:

foo: /* foo code */
ret

bar: /* bar code */
ret

foobar: call foo
call bar
ret

but he can combine these, saving a 'ret' execution, some stack
space, and a call. The result is:

foobar: /* foo code */
; /* fall through */
bar: /* bar code */
ret

eliminating two calls and two rets from the earlier code. The C
programmer doesn't have this capability. Believe me, it adds up
over a medium complicated system.

You mean something like this?

void bar (void);

void foo (void)
{
0100 9D NOP NOP();
bar();
}

void bar (void)
{
0101 9D NOP NOP();
0102 81 RTS }


void main (void)
{
0103 AD FB BSR $0100 foo();
0105 20 FA BRA $0101 bar();
}

It does add up..


That's just tail call elimination (changing a "call X; ret" into a "jmp X"), which is a standard optimisation technique (some assemblers will do that for you).

A better example would be:

WriteSpace:
ld a, #' '
WriteChar:
st a, outputCharacter
ret

with C code:

extern volatile char outputCharacter;
void WriteChar(char c) {
outputCharacter = c;
}
void WriteSpace(void) {
WriteChar(' ');
}
.



Relevant Pages