Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I'm converting the c routine below into mainly ASM code, can anyone see what I might have done wrong when translating it to ASM?
- PLAIN C VERSION
- void plexSort() {
- char nxt_idx;
- char nxt_y;
- char s;
- char sortedIDX;
- char m;
- for(char m: 0..PLEX_COUNT-2) {
- nxt_idx = PLEX_SORTED_IDX[m+1];
- nxt_y = PLEX_YPOS[nxt_idx];
- sortedIDX = PLEX_SORTED_IDX[m];
- if(nxt_y < PLEX_YPOS[sortedIDX]) {
- //Shift values until we encounter a value smaller than nxt_y
- s = m;
- while (true) {
- PLEX_SORTED_IDX[s+1] = PLEX_SORTED_IDX[s];
- s--;
- sortedIDX = PLEX_SORTED_IDX[s];
- if (s == 0xff) {break;}
- if (nxt_y >= PLEX_YPOS[sortedIDX]) {break;}
- }
- // store the mark at the found position
- s++;
- PLEX_SORTED_IDX[s] = nxt_idx;
- }
- }
- //Prepare for showing the sprites
- plex_show_idx = 0;
- plex_sprite_idx = 0;
- plex_sprite_msb = 1;
- plex_sprite_mask = 0xff^plex_sprite_msb;
- plexFreePrepare();
- }
- -------------------------------------------------------------------
- ASSEMBLY CODE VERSION...
- void plexSort() {
- char nxt_idx;
- char nxt_y;
- char s;
- char sortedIDX;
- char m;
- asm {
- lda #0
- sta m
- !forLoop:
- // nxt_idx = PLEX_SORTED_IDX[m+1];
- lda m
- clc
- adc #1
- tax
- lda PLEX_SORTED_IDX,x
- sta nxt_idx
- // nxt_y = PLEX_YPOS[nxt_idx];
- tax
- lda PLEX_YPOS,x
- sta nxt_y
- // sortedIDX = PLEX_SORTED_IDX[m];
- ldx m
- lda PLEX_SORTED_IDX,x
- sta sortedIDX
- // if nxt_y >= PLEX_YPOS[sortedIDX]; if true, skip while loop
- tax
- lda nxt_y
- cmp PLEX_YPOS,x
- bcs !whileEnd+ // skip while loop
- // Shift values until we encounter a value smaller than nxt_y
- // s = m;
- lda m
- sta s
- !whileLoop:
- // PLEX_SORTED_IDX[s+1] = PLEX_SORTED_IDX[s];
- lda s
- tay
- clc
- adc #1
- tax
- lda PLEX_SORTED_IDX,y
- sta PLEX_SORTED_IDX,x
- // s--;
- dec s
- // sortedIDX = PLEX_SORTED_IDX[s]
- ldx s
- lda PLEX_SORTED_IDX,x
- sta sortedIDX
- // if (s == 0xff) {break;}
- txa
- cmp #$ff
- beq !whileEnd+
- // if (nxt_y >= PLEX_YPOS[sortedIDX]) {break;}
- ldx sortedIDX
- lda nxt_y
- cmp PLEX_YPOS,x
- bcs !whileEnd+ // >= exit loop
- jmp !whileLoop-
- !whileEnd:
- // end of while loop
- // store the mark at the found position
- // s++;
- inc s
- // PLEX_SORTED_IDX[s] = nxt_idx;
- ldx s
- lda nxt_idx
- sta PLEX_SORTED_IDX,x
- // loop if m <= PLEX_COUNT - 2
- inc m
- lda m
- cmp #PLEX_COUNT-1
- beq !+
- // not finished for loop yet...
- jmp !forLoop-
- !:
- }
- // Prepare for showing the sprites
- plex_show_idx = 0;
- plex_sprite_idx = 0;
- plex_sprite_msb = 1;
- plex_sprite_mask = 0xff^plex_sprite_msb;
- plexFreePrepare();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement