[A86] SDCC v2.3.0 (fib.c)
[Prev][Next][Index][Thread]
[A86] SDCC v2.3.0 (fib.c)
> > I could send some SDCC compiled examples to this list to convince
> > people.
>
> Please do. Some loops, control structures, etc., that are used in a
> normal calc program would be good. Possibly write a simple game like
> Nibbles (basic features) in a straight forward manner, and see how well
> it compares to a straight forward hand coded version.
At the moment I've got a fibonacci calculator, it works on the z88dk,
haven't/couldn't/whatever test it for SDCC.
----fib.c----
/* Small recursive program that calculates Fibonacci numbers.
*
* This program was originally featured in the "Embedded Systems Magazine"
* April 1989
*/
#include <stdio.h>
unsigned int fib(unsigned int n);
void main(void)
{
unsigned char loop;
for (loop = 1 ; loop < 10 ; loop++)
{
printf("fib(%d) = %d\n",loop,fib(loop));
}
}
unsigned int fib(unsigned int n)
{
if (n <= 2) return 1;
return fib(n-1) + fib(n-2);
}
-----------
I haven't taken a very good look at it but from what I've seen it could be
that the recursiveness doesn't work...
Yes I do know that some JP's could be JR's (already made some
peephole-rules for that). And that the register allocator doesn't look to
torougly if some regs still hold the right value (it renews the 'lifecycle'
even if there is still register with the right value 'alife').
It also does some not-so-quite-optimized push/pops around calling routines.
----fib.asm----
;--------------------------------------------------------
; File Created by SDCC : FreeWare ANSI-C Compiler
; Version 2.3.0 Sat Oct 13 16:50:45 2001
;--------------------------------------------------------
.module fib
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
.globl _main
.globl _fib
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
;--------------------------------------------------------
; special function bits
;--------------------------------------------------------
;--------------------------------------------------------
; internal ram data
;--------------------------------------------------------
.area _DATA
;--------------------------------------------------------
; overlayable items in internal ram
;--------------------------------------------------------
.area _OVERLAY
;--------------------------------------------------------
; indirectly addressable internal ram data
;--------------------------------------------------------
.area _ISEG
;--------------------------------------------------------
; bit data
;--------------------------------------------------------
.area _BSEG
;--------------------------------------------------------
; external ram data
;--------------------------------------------------------
.area _XSEG
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
.area _GSINIT
.area _GSFINAL
.area _GSINIT
;--------------------------------------------------------
; Home
;--------------------------------------------------------
.area _HOME
.area _CODE
;--------------------------------------------------------
; code
;--------------------------------------------------------
.area _CODE
; fib.c 11
; genLabel
; genFunction
; ---------------------------------
; Function main
; ---------------------------------
___main_start:
_main:
push ix
ld ix,#0
add ix,sp
ld hl,#-2
add hl,sp
ld sp,hl
; fib.c 14
; genAssign
ld c,#0x01
; genLabel
00101$:
; genCmpLt
ld a,c
cp #0x0A
jp nc,00105$
; fib.c 16
; genCast
ld e,c
ld d,#0x00
; genIpush
; _saveRegsForCall: sendSetSize: 0 deInUse: 0 bcInUse: 1 deSending: 0
push bc
push de
; genCall
call _fib
ld d,h
ld e,l
pop hl
pop hl
ld c,l
; genCast
; AOP_STK for _main_sloc0_1_0
ld -2(ix),c
ld a,c
rla
sbc a,a
ld -1(ix),a
; genIpush
; _saveRegsForCall: sendSetSize: 0 deInUse: 0 bcInUse: 1 deSending: 0
push bc
push de
; genIpush
; AOP_STK for _main_sloc0_1_0
ld l,-2(ix)
ld h,-1(ix)
push hl
; genIpush
ld hl,#__str_0
push hl
; genCall
call _printf
pop hl
pop hl
pop hl
pop hl
ld c,l
; fib.c 14
; genPlus
; genPlusIncr
inc c
; genGoto
jp 00101$
; genLabel
00105$:
; genEndFunction
ld sp,ix
pop ix
ret
___main_end:
__str_0:
.ascii "fib(%d) = %d"
.db 0x0A
.db 0x00
; fib.c 20
; genLabel
; genFunction
; ---------------------------------
; Function fib
; ---------------------------------
___fib_start:
_fib:
push ix
ld ix,#0
add ix,sp
; fib.c 22
; genCmpGt
; AOP_STK for
ld a,#0x02
sbc a,4(ix)
ld a,#0x00
sbc a,5(ix)
jp c,00102$
; genRet
ld hl,#0x0001
jp 00103$
; genLabel
00102$:
; fib.c 23
; genMinus
; AOP_STK for
ld c,4(ix)
ld b,5(ix)
dec bc
; genIpush
; _saveRegsForCall: sendSetSize: 0 deInUse: 0 bcInUse: 0 deSending: 0
push bc
; genCall
call _fib
ld b,h
ld c,l
pop hl
; genMinus
; AOP_STK for
ld e,4(ix)
ld d,5(ix)
dec de
dec de
; genIpush
; _saveRegsForCall: sendSetSize: 0 deInUse: 0 bcInUse: 1 deSending: 0
push bc
push de
; genCall
call _fib
ld d,h
ld e,l
pop hl
pop bc
; genPlus
; Can't optimise plus by inc, falling back to the normal way
ld a,c
add a,e
ld c,a
ld a,b
adc a,d
ld b,a
; genRet
ld l,c
ld h,b
; genLabel
00103$:
; genEndFunction
pop ix
ret
___fib_end:
.area _CODE
------------------
Do you want iCode too? Don't think so, just asking. You could also just
download some binaries/source from the sdcc website.
Henk Poley <><
Follow-Ups: