Re: mixing C and assembly
- From: David Brown <david.brown@xxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 27 Apr 2008 11:56:29 +0200
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(' ');
}
.
- Follow-Ups:
- Re: mixing C and assembly
- From: CBFalconer
- Re: mixing C and assembly
- References:
- mixing C and assembly
- From: Lax
- Re: mixing C and assembly
- From: Hans-Bernhard Bröker
- Re: mixing C and assembly
- From: Chris H
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: Mark Borgerson
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: Stefan Reuther
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: cbarn24050
- Re: mixing C and assembly
- From: David Brown
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: CBFalconer
- Re: mixing C and assembly
- From: cbarn24050
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: cbarn24050
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: cbarn24050
- Re: mixing C and assembly
- From: Walter Banks
- Re: mixing C and assembly
- From: cbarn24050
- Re: mixing C and assembly
- From: CBFalconer
- Re: mixing C and assembly
- From: Walter Banks
- mixing C and assembly
- Prev by Date: Re: mixing C and assembly
- Next by Date: Re: mixing C and assembly
- Previous by thread: Re: mixing C and assembly
- Next by thread: Re: mixing C and assembly
- Index(es):
Relevant Pages
|