Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of an u8 and a func returning u8 -> wrong result #268

Closed
clbr opened this issue Feb 24, 2016 · 3 comments
Closed

Addition of an u8 and a func returning u8 -> wrong result #268

clbr opened this issue Feb 24, 2016 · 3 comments

Comments

@clbr
Copy link
Contributor

@clbr clbr commented Feb 24, 2016

u8 rand8();

void play() {
    static u16 nextpow;
    nextpow = 180 + rand8();
    // print it on the screen
}

After this, var contains 9003, which is no way possible. rand8 returns 119 in this case. This happens both with and without -O.

If I write it in two lines, "var = 180; var += rand8();" the result is correct.

Asm with -O:

; ---------------------------------------------------------------
; void __near__ play (void)
; ---------------------------------------------------------------

.segment        "CODE"

.proc   _play: near

.segment        "BSS"

L0024:
        .res    2,$00
L0026:
        .res    5,$00

.segment        "CODE"

;
; nextpow = 180 + rand8();
;
        jsr     _rand8
        clc
        adc     #$B4
        bcc     L0029
        inx
L0029:  sta     L0024
        stx     L0024+1
;
; utoa4(scorebuf, nextpow);
;
...
@clbr
Copy link
Contributor Author

@clbr clbr commented Feb 24, 2016

Sorry, typo: var contains 9003.

Loading

@greg-king5
Copy link
Contributor

@greg-king5 greg-king5 commented Feb 25, 2016

Sometimes, cc65 expects functions to return (at least) 16 bits -- even when they are declared to return eight bits! _rand8 must put a zero into the X register before it returns.

Loading

@oliverschmidt
Copy link
Contributor

@oliverschmidt oliverschmidt commented Feb 26, 2016

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants