Traverse from the beginning of a singly-linked list to the end.



See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


 
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program afficheList64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ NBELEMENTS,      100              // list size
 
/*******************************************/
/* Structures                               */
/********************************************/
/* structure linkedlist*/
    .struct  0
llist_next:                            // next element
    .struct  llist_next + 8
llist_value:                           // element value
    .struct  llist_value + 8
llist_fin:
/* Initialized data */
.data
szMessInitListe:         .asciz "List initialized.\n"
szCarriageReturn:        .asciz "\n"
/* datas error display */
szMessErreur:            .asciz "Error detected.\n"
/* datas message display */
szMessResult:            .asciz "Element No : @ value @ \n"
 
/* UnInitialized data */
.bss 
lList1:              .skip llist_fin * NBELEMENTS    // list memory place
sZoneConv:           .skip 100
/*  code section */
.text
.global main 
main: 
    ldr x0,qAdrlList1
    mov x1,#0                           // list init
    str x1,[x0,#llist_next]
    ldr x0,qAdrszMessInitListe
    bl affichageMess
    ldr x0,qAdrlList1
    mov x1,#2
    bl insertElement                    // add element value 2
    ldr x0,qAdrlList1
    mov x1,#5
    bl insertElement                    // add element value 5
    //                                   // display elements of list
    ldr x3,qAdrlList1
    mov x2,#0                           // ident element
1:
    ldr x0,[x3,#llist_next]             // end list ?
    cmp x0,#0
    beq 100f                            // yes
    add x2,x2,#1
    mov x0,x2                           // display No element and value
    ldr x1,qAdrsZoneConv
    bl conversion10S
    ldr x0,qAdrszMessResult
    ldr x1,qAdrsZoneConv
    bl strInsertAtCharInc
    mov x5,x0                          // address of new string
    ldr x0,[x3,#llist_value]
    ldr x1,qAdrsZoneConv
    bl conversion10S
    mov x0,x5                          // new address of message
    ldr x1,qAdrsZoneConv
    bl strInsertAtCharInc
    bl affichageMess
    ldr x3,[x3,#llist_next]             // next element
    b 1b                                // and loop
100:                                    // standard end of the program
    mov x8, #EXIT                       // request to exit program
    svc 0                               // perform system call
qAdrszMessInitListe:       .quad szMessInitListe
qAdrszMessErreur:          .quad szMessErreur
qAdrszCarriageReturn:      .quad szCarriageReturn
qAdrlList1:                .quad lList1
qAdrszMessResult:          .quad szMessResult
qAdrsZoneConv:             .quad sZoneConv
 
/******************************************************************/
/*     insert element at end of list                          */ 
/******************************************************************/
/* x0 contains the address of the list */
/* x1 contains the value of element  */
/* x0 returns address of element or - 1 if error */
insertElement:
    stp x1,lr,[sp,-16]!                   // save  registers
    stp x2,x3,[sp,-16]!                   // save  registers
    mov x2,#llist_fin * NBELEMENTS
    add x2,x2,x0                          // compute address end list
1:                                        // start loop 
    ldr x3,[x0,#llist_next]               // load next pointer
    cmp x3,#0                             // = zero
    csel  x0,x3,x0,ne
    bne 1b                                // no -> loop with pointer
    add x3,x0,#llist_fin                  // yes -> compute next free address
    cmp x3,x2                             // > list end 
    bge 99f                               // yes -> error
    str x3,[x0,#llist_next]               // store next address in current pointer
    str x1,[x0,#llist_value]              // store element value
    mov x1,#0
    str x1,[x3,#llist_next]               // init next pointer in next address
    b 100f
99:                                       // error
    mov x0,-1
100:
    ldp x2,x3,[sp],16                     // restaur  2 registers
    ldp x1,lr,[sp],16                     // restaur  2 registers
    ret                                   // return to address lr x30
/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
 