; Programme qui manipule une liste chaînée ; Jean Privat (c) 2010 ; et qui affiche les nombres lus en ordre de grandeur du plus grand au plus petit ; ; ; // Lit la liste (à l'envers) LDA heap,i ; ADDRSS heap STA hpPtr,d ;lecture de tous les nombres (le nombre 0 indique la fin de lecture) ; loop_in: NOP0 LDA mLength,i CALL new ; X = new Maillon(); #mVal #mNext DECI mVal,x ; X.val = getInt(); BREQ out LDA head,d STA mNext,x ; X.next = head; STX head,d ; head = X; LDA cpt,d ; compteur de nombres ADDA 1,i STA cpt,d BR loop_in ; } // fin for ; ; ; // Affiche la liste à partir du plus grand nombre out: CHARO "\n",i ; while10: LDA cpt,d BREQ termine ; LDA -32767,i ; initialise le plus grand nombre à -32767 LDX head,d ; premier nombre STX posgrand,d ; position du plus grand nombre loop_out:CPX 0,i BREQ fin ; for (X=head; X!=null; X=X.next) { CPA mVal,x ; plus grand nombre ? BRGE oubli LDA mVal,x ; on le trtient comme le plus grand STX posgrand,d ; avec sa position oubli: LDX mNext,x BR loop_out ; } // fin for ; fin: STA temp,d ; plus grand nombre LDA -32767,i ; remis à -32767 pour l'éviter LDX posgrand,d STA mVal,x DECO temp,d ; affichage du plus grand nombre CHARO "\n",i LDA cpt,d ; plus grand nombre suivant SUBA 1,i STA cpt,d endwhi10:BR while10 ; termine: STOP head: .BLOCK 2 ; #2h tête de liste (null (aka 0) si liste vide) cpt: .BLOCK 2 ; #2d compteur de nombres temp: .BLOCK 2 ; #2d posgrand:.BLOCK 2 ; #2h position du plus grand nombre ; ;******* Structure de liste d'entiers ; Une liste est constituée d'une chaîne de maillons. ; Chaque maillon contient une valeur et l'adresse du maillon suivant ; La fin de la liste est marquée arbitrairement par l'adresse 0 mVal: .EQUATE 0 ; #2d valeur de l'élément dans le maillon mNext: .EQUATE 2 ; #2h maillon suivant (null (aka 0) pour fin de liste) mLength: .EQUATE 4 ; taille d'un maillon en octets ; ; ;******* operator new ; Precondition: A contains number of bytes ; Postcondition: X contains pointer to bytes new: LDX hpPtr,d ;returned pointer ADDA hpPtr,d ;allocate from heap STA hpPtr,d ;update hpPtr RET0 hpPtr: .BLOCK 2 ;#2h ADDRSS heap (address of next free byte) heap: .BLOCK 1 ;first byte in the heap .END