Input loop is part of Short Circuit's Console Program Basics selection.
Task

Read from a text stream either word-by-word or line-by-line until the stream runs out of data.

The stream will have an unknown amount of data on it.




 
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program inputLoop64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ BUFSIZE,  10000
.equ LINESIZE, 100
 
/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessErreur:     .asciz "Erreur ouverture fichier input.\n"
szMessErreur1:    .asciz "Erreur fermeture fichier.\n"
szMessErreur2:    .asciz "Erreur lecture fichier.\n"
szCarriageReturn: .asciz "\n"
szMessEndLine:    .asciz "<<<<<< End line.\n"
 
szMessCodeErr: .asciz "Error code décimal :  @ \n"
 
szNameFileInput:    .asciz "input.txt"
 
/*******************************************/
/* DONNEES NON INITIALISEES                */
/*******************************************/ 
.bss
sZoneConv:    .skip 24
sBuffer:      .skip BUFSIZE 
sBufferWord:  .skip LINESIZE
/**********************************************/
/* -- Code section                            */
/**********************************************/
.text            
.global main    
main:
    /* open file */
    mov x0,AT_FDCWD
    ldr x1,qAdrszNameFileInput     // file name
    mov x2,O_RDONLY                //  flags   
    mov x3,0                       // mode 
    mov x8,OPEN                    // call system OPEN
    svc 0 
    cmp x0,0                       // open error ?
    ble erreur
    /* read file  */
    mov x9,x0                      // save File Descriptor
    ldr x1,qAdrsBuffer             // buffer address 
    mov x2,BUFSIZE                 // buffer size
    mov x8,READ                    // call system  READ
    svc 0 
    cmp x0,0                       // read error ?
    ble erreur2
    mov x2,x0                      // length read characters
    /* buffer analyze   */
    ldr x3,qAdrsBuffer             // buffer address
    ldr x5,qAdrsBufferWord         // buffer address
    mov x7,0                       // word byte counter
    mov x4,0                       // byte counter
    mov x10,1
1:
    ldrb w6,[x3,x4]                // load byte buffer
    cmp x6,' '                     // space ?
    csel x8,xzr,x10,eq             // yes  0-> x8
    beq 2f
    cmp x6,0xA                     // end line ?
    csel x8,x10,xzr,eq             // yes 1 -> x8
    beq 2f
    cmp x6,0xD                     // end line ?
    csel x8,x10,xzr,eq
    beq 3f
    strb w6,[x5,x7]                // store byte
    add x7,x7,1                    // increment word byte counter
    b 4f
2:                                 // word end
    cmp x7,0
    beq 3f
    mov x6,0                       // store 0 final
    strb w6,[x5,x7]
    mov x0,x5                      // display word
    bl affichageMess
    ldr x0,qAdrszCarriageReturn
    bl affichageMess
    mov x7,0                       // raz word byte counter 
3:
    cmp x8,1                       // end line ?
    bne 4f
    ldr x0,qAdrszMessEndLine
    bl affichageMess
4:       
    add x4,x4,1                    // increment read buffer counter
    cmp x4,x2                      // end bytes ?
    blt 1b                         // no -> loop 
 
4:
    /* close imput file */
    mov x0,x9                      // Fd
    mov x8,CLOSE                   // call system CLOSE
    svc 0 
    cmp x0,0                       // close error ?
    blt erreur1
 
    mov x0,0                       // return code OK
    b 100f
erreur:
    ldr x1,qAdrszMessErreur 
    bl displayError
    mov x0,1                       // error return code
    b 100f
erreur1:    
    ldr x1,qAdrszMessErreur1   
    bl displayError
    mov x0,1                      // error return code
    b 100f
erreur2:
    ldr x1,qAdrszMessErreur2   
    bl displayError
    mov x0,1                      // error return code
    b 100f
 
100:                              // end program
    mov x8,EXIT
    svc 0 
qAdrszNameFileInput:      .quad szNameFileInput
qAdrszMessErreur:         .quad szMessErreur
qAdrszMessErreur1:        .quad szMessErreur1
qAdrszMessErreur2:        .quad szMessErreur2
qAdrszCarriageReturn:     .quad szCarriageReturn
qAdrszMessEndLine:        .quad szMessEndLine
qAdrsBuffer:              .quad sBuffer
qAdrsBufferWord:          .quad sBufferWord
/******************************************************************/
/*     display error message                                      */ 
/******************************************************************/
/* x0 contains error code */
/* x1 contains address error message    */
displayError:
    stp x2,lr,[sp,-16]!            // save  registers
    mov x2,x0                      // save error code
    mov x0,x1                      // display message error
    bl affichageMess
    mov x0,x2
    ldr x1,qAdrsZoneConv           // conversion error code
    bl conversion10S               // decimal conversion
    ldr x0,qAdrszMessCodeErr
    ldr x1,qAdrsZoneConv
    bl strInsertAtCharInc          // insert result at @ character
    bl affichageMess               // display message final
    ldp x2,lr,[sp],16              // restaur  2 registers
    ret                            // return to address lr x30
qAdrsZoneConv:        .quad sZoneConv
qAdrszMessCodeErr:    .quad szMessCodeErr
/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 